0

What is the difference between the following serialization methods?

  1. First Method

    JsonConvert.SerializeObject(list or datatable)

and the output is

i.e. (3) [Object, Object, Object]

  1. Second Method

Dim parent = Prtdata
                Dim lGridColumns = New With {
                    Key .data = parent
                        }
                Dim Setting = New JsonSerializerSettings
                Setting.PreserveReferencesHandling = PreserveReferencesHandling.Objects
                Dim jsonObject = JsonConvert.SerializeObject(lGridColumns, Formatting.Indented)
                Return jsonObject

and its output is

{
  "data": [
    {
      "RecID": 2383,
      "PrtStatus": 0,
      "PtFilenum": 15090248,
      "PrtFilenum": 13090701,
      "FullName": "asdasd",
      "DOB": "04 Oct 1985"
    },
    {
      "RecID": 3387,
      "PrtStatus": 1,
      "PtFilenum": 15090248,
      "PrtFilenum": 15120996,
      "FullName": "marwam mohmmad  saleem",
      "DOB": "24 May 2017"
    },
    {
      "RecID": 3388,
      "PrtStatus": 1,
      "PtFilenum": 15090248,
      "PrtFilenum": 170227111,
      "FullName": "asd dsf as a",
      "DOB": "27 Feb 2017"
    }
  ]
}

why the output looks different in the browser console?

4
  • 1
    For the first scenario, you are creating an array (since you are serializing a list or an array), the second one is creating a json object with the data in a separate property. This would be the same in many languages, so I don't really get the question you are asking here? Commented Jun 22, 2017 at 22:42
  • you just answered it, could you please post it as an answer Commented Jun 22, 2017 at 23:12
  • Alright, I did, tried to give a bit more info than in the comment though ;) Commented Jun 23, 2017 at 0:04
  • this is what exactly I was looking for indeed :) Commented Jun 23, 2017 at 0:06

1 Answer 1

1

As the first comment, you can find a Serialization Guide on the website of NewtonSoft.json, in my answer I just provide a more elaborate version of my comment earlier.

The first scenario, where you are serializing something implemented IEnumerable (eg: list, array), will be represented by an array in Json, eg:

[{ "property": "value", "id": 0 }, {"property": "value", "id": 1}]

For the second scenario, you are doing several things differently, for example you are providing the PreserveReferencesHandling in the JsonSerializerSettings which would also preveserve any references made in the objects you are serializing, eg:

[{"$id": 1, "title": "item1"}, {"$id": 2, "title": "item2", "previous": { "$ref": 1 }]

This would make sure that when deserialized, the second object would contain a reference to the first object, inside the property previous.

Another thing you are doing differently is providing the Formatting.Indented, which will create a more reader friendly json document, having line breaks and indentation. The previous Json would then become something similar to this:

[{
    "$id": 1,
    "title": "item1"
},
{
    "$id": 2,
    "title": "item2",
    "previous": {
        "$ref": 1
    }
}]

And, the last big difference is that in the last example, you are serializing a single object, cause it's public properties to be serialized, eg:

{
    "data": [
        ...
    ]
}

Where data is a property on the object you are serializing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.