Home > database >  Newtonsoft Json - Serialize Deep nested object based on given selected properties
Newtonsoft Json - Serialize Deep nested object based on given selected properties

Time:11-26

Does Newtonsoft Json have any way to serialize nested objects with only selected properties ? The model looks like:

class Node
{
int id{ get; set; };
string name{ get; set; };
IList<Node>children{ get; set; };
}

I am creating a tree structure afterwards which looks something like this:

{
    "id": 1,
    "name": "A",
    "children": [
        {
            "id": 2,
            "name": "B",
            "children": null
        },
        {
            "id": 3,
            "name": "C",
            "children": [
                {
                    "id": 10,
                    "name": "D",
                    "children": null
                }
            ]
        }
    ]
}

Wrote my own serializer. I passed Children property only .It shows as:

{
    "children": [
        {
            "children": null
        },
        {
            "children": [
                {
                    "children": null
                }
            ]
        }
    ]
}

I want to show all properties and not just children under 1st/root's children.Similar to below.

{
    "children": [
        {
            "id": 2,
            "name": "B",
            "children": null
        },
        {
            "id": 3,
            "name": "C",
            "children": [
                {
                    "id": 10,
                    "name": "D",
                    "children": null
                }
            ]
        }
    ] }

Also how do I do the same for children.Id where I want to serialize all children.Id of root node,Similar to below

{
"children": [
    {
        "id": 2
    },
    {
        "id": 3
    }
]

}

CodePudding user response:

they have done the same result you want to achieve on this page.

https://weblog.west-wind.com/posts/2012/aug/30/using-jsonnet-for-dynamic-json-parsing

Also, see Here,

https://entityframeworkcore.com/knowledge-base/58283745/self-reference-when-serializing-nested-object

I hope that will be useful.

CodePudding user response:

What do you neeed your own serializer for? try the standart one

var jsonDeserialized = JsonConvert.DeserializeObject<Child>(json);

json=JsonConvert.SerializeObject(jsonDeserialized);

output

{
  "id": 1,
  "name": "A",
  "children": [
    {
      "id": 2,
      "name": "B",
      "children": null
    },
    {
      "id": 3,
      "name": "C",
      "children": [
        {
          "id": 10,
          "name": "D",
          "children": null
        }
      ]
    }
  ]
}

classes

     public partial class Child
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("children")]
        public List<Child> Children { get; set; }
    }

for this

{
    "children": [
        {
            "id": 2,
            "name": "B",
            "children": null
        },
        {
            "id": 3,
            "name": "C",
            "children": [
                {
                    "id": 10,
                    "name": "D",
                    "children": null
                }
            ]
        }
    ] }

you can use

var jsonDeserialized = JsonConvert.DeserializeObject<Data>(json);

public partial class Data
  {
 
    [JsonProperty("children")]
    public List<Child> Children { get; set; }
  }
  • Related