Serialize list to an existing .Json file, using a datagridview

Viewed 121

I have a deserialized json file output directly to a DGV, and visible to the end user in a DGV. when they are done using the software and close out, I would like to serialize the updated DGV to my json file.

when I deserialized this json file the code is as follows:

CODE

    var v = JsonConvert.DeserializeObject<ToolJson> 
    (File.ReadAllText(@"\testLibrary.json"));

        //set DGV source to our datums 
        toolDataGridView.DataSource = v.Datums.Cast<IInteresting> 
        ().ToList();

and when the form is closed i want to read the DGV back into my json file, in the exact same format it is already. below is snippet of the json file. all i am doing with the DGV is keeping track of Quantities, last employee used and if the tool needs returned. basically 5 new entries to the json file. top level properties.

Json File

{
"data": [
    {
        "BMC": "carbide",
        "Cost": 0,
        "Employee": "",
        "GRADE": "Mill Generic",
        "Location": "",
        "description": "5/8-11\"",
        "geometry": {
            "CSP": false,
            "DC": 0.433,
            "HAND": true,
            "LB": 2,
            "LCF": 0.5,
            "NOF": 4,
            "NT": 1,
            "OAL": 5,
            "SFDM": 0.625,
            "TP": 0.0909091,
            "shoulder-length": 1.969,
            "thread-profile-angle": 60
        },
        "guid": "0112c196-8a79-421d-8dda-d4aa964aa6d7",
        "holder": {
            "description": "Maritool CAT40-ER32-2.35",
            "guid": "e800051b-e2d6-4699-a2b6-dad6466a0a0c",
            "last_modified": 1485790626152,
            "product-id": "CAT40-ER32-2.35",
            "product-link": "",
            "segments": [
                {
                    "height": 0.148,
                    "lower-diameter": 1.5,
                    "upper-diameter": 1.97
                },
                {
                    "height": 0.836,
                    "lower-diameter": 1.97,
                    "upper-diameter": 1.97
                },
                {
                    "height": 0.176,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.57
                },
                {
                    "height": 0.09,
                    "lower-diameter": 1.57,
                    "upper-diameter": 1.75
                },
                {
                    "height": 0.425,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                },
                {
                    "height": 0.05,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.85
                },
                {
                    "height": 0.03,
                    "lower-diameter": 2.442,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.145,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.079,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.215
                },
                {
                    "height": 0.118,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.215
                },
                {
                    "height": 0.079,
                    "lower-diameter": 2.215,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.143,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.502
                },
                {
                    "height": 0.03,
                    "lower-diameter": 2.502,
                    "upper-diameter": 2.442
                },
                {
                    "height": 0.125,
                    "lower-diameter": 1.75,
                    "upper-diameter": 1.75
                }
            ],
            "type": "holder",
            "unit": "inches",
            "vendor": "Maritool"
        },
        "onHandQty": 0,
        "OrderQty": 5,
        "post-process": {
            "break-control": false,
            "comment": "",
            "diameter-offset": 17,
            "length-offset": 17,
            "live": true,
            "manual-tool-change": false,
            "number": 17,
            "turret": 0
        },
        "product-id": "GMDTTM58-11UN4FL",
        "product-link": "6010",
        "start-values": {
            "presets": [
                {
                    "description": "",
                    "f_n": 0.012242786571039,
                    "f_z": 0.0031,
                    "guid": "eac33213-4083-35b8-afe3-17bd42a249f0",
                    "n": 4410.76054758139,
                    "n_ramp": 44110,
                    "name": "1018",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 499.9999999999994,
                    "v_f": 54.6934307900093,
                    "v_f_leadIn": 54,
                    "v_f_leadOut": 54,
                    "v_f_plunge": 54,
                    "v_f_ramp": 54
                },
                {
                    "description": "",
                    "f_n": 0.010769117817118,
                    "f_z": 0.0028,
                    "guid": "f35f5601-68a9-477b-b4b0-2ffeedb3bef1",
                    "n": 1764.3042190325598,
                    "n_ramp": 1764,
                    "name": "4140",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 200,
                    "v_f": 19.7602072531647,
                    "v_f_leadIn": 19,
                    "v_f_leadOut": 19,
                    "v_f_plunge": 19,
                    "v_f_ramp": 19
                },
                {
                    "description": "",
                    "f_n": 0.012091641057817,
                    "f_z": 0.0031,
                    "guid": "b118ce46-da35-4ed6-9806-b98e05ffe077",
                    "n": 2646.45632854884,
                    "n_ramp": 2646,
                    "name": "Tool Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 300,
                    "v_f": 32.8160584740056,
                    "v_f_leadIn": 32,
                    "v_f_leadOut": 32,
                    "v_f_plunge": 32,
                    "v_f_ramp": 32
                },
                {
                    "description": "",
                    "f_n": 0.01118476797848,
                    "f_z": 0.0028,
                    "guid": "0e1767f5-b0ef-422f-b49d-6cb8c3eb06ed",
                    "n": 3308.0704106860494,
                    "n_ramp": 3308,
                    "name": "Stainless Steel",
                    "tool-coolant": "flood",
                    "use-stepdown": false,
                    "use-stepover": false,
                    "v_c": 375,
                    "v_f": 37.0503885996837,
                    "v_f_leadIn": 37,
                    "v_f_leadOut": 37,
                    "v_f_plunge": 37,
                    "v_f_ramp": 37
                }
            ]
        },
        "stockQty": 5,
        "type": "thread mill",
        "unit": "inches",
        "vendor": "Gorilla Mill"
    },
1 Answers

Clearing accompanying text, i think i understand where the difficulty lied and indeed it is an exotic datatype we need to Use, unless there Is only one list in the json, in which case you can simply add table to named dataset "Tool" and serialize that, I've included both

public partial class frmWithGrid : Form
{                 
    public frmWithGrid()
    {
        InitializeComponent();

        string myDynamicJSON = "Imagine your testLibraryJson loaded here or later but before next line";
        DataTable convertedToTableToolData = ConvertToTableToolData(myDynamicJSON);

        dataGridView1.DataSource = convertedToTableToolData;
        //dataGridView1.DataMember = we're not storing it in a dataset so we can ignore this one
        dataGridView1.AutoGenerateColumns = true;   
        
        //TODO: Add bla bla to setup UI of cause, suppose you're able to navigate rows etc.
    }

    DataTable ConvertToTableToolData(string jsonSource)
    {
        //TODO: Insert own convertion code here, which seems to be in place
        return new DataTable(); // instead of this
    }

    private void btnUserIsDone_Click(object sender, EventArgs e)
    {
        //Obviously this shouldn't be directly in an event handler

        //Either you have many lists
        dynamic x = new ExpandoObject();
        var editedTable = (DataTable)dataGridView1.DataSource;
        //Will ignore that your OrderQty maybe wasn't originally part of data structure, because it may have been
        //and it doesn't change anything with the principle, and adds needless complexity
        var propertiesDictionary = (IDictionary<string, object>)x;
        propertiesDictionary.Add("Tool", editedTable);

        //TODO: Add other sets to json if applicable

        string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(x);



        //or just one
        var ds = new DataSet("Tool");
        ds.Tables.Add(editedTable.Clone());
        serialized = Newtonsoft.Json.JsonConvert.SerializeObject(ds);
    }
}
Related