Search code examples
c#jsonserializationobject-class

Serialize/Deserialize Json Object class structure


Thank you in advance. I am really stuck on this one, but practicing. Thank you in advance for any help.

I am able to successfully build this Json object in my code, but de-serializing it with the same Dictionary I used to build it, is not working because the string type defined is anonymous and I cannot access specifically the sub items (contains, equals, startsWith).

I have my json being built successfully but again, does not work when deserializing it.

{
"shortDescription": {
  "contains": false,
  "equals": false,
  "startsWith": true,
  "value": "some text"
},
"description": {
  "contains": true,
  "equals": false,
  "startsWith": false,
  "value": "some more text"
},
"createdAfter": {
  "contains": false,
  "equals": false,
  "startsWith": false,
  "value": "2021-08-17"
},
"notes": "Something bad happened",
"group": "some group",
"assigned": "to me"
}

These objects like shortDescription may not even exist depending on user selection which I why I built an anonymous type Dictionary using "String" public Dictionary<string, filter_properties> properties { get; set; } I can apply this format to any object that needs these properties.

public class filter_keys
{
    public string notes { get; set; }
    public string group { get; set; }
    public string assigned { get; set; }
    public Dictionary<string, filter_properties> properties { get; set; }
}
public class filter_properties
{
    public bool contains { get; set; }
    public bool equals { get; set; }
    public bool startsWith { get; set; }
    public string value { get; set; }
}

I would really appreciate some help to figure out how to set a simple property only for this description and shortDescription fields, not just to serialize the data but also to de-serialize the data, so I can check if these objects even exist in the json.

When I am setting the json I use

Dictionary<string, filter_properties> keys = new Dictionary<string, filter_properties>();

keys.Add("anonymous", new filter_properties { value="can't find it" });

and/or

keys.Add("shortDescription", new filter_properties { 
                        contains = true, 
                        value = "blah blah blah"
                    });

Solution

  • Like I mention in my comment, you have to build your json with those properties under a properties node.

    using System;
    using System.Collections.Generic;
    using Newtonsoft.Json;
    
    public class Program
    {
        public static void Main()
        {
            var inJson = @"{
            ""properties"": {
                ""shortDescription"": {
                  ""contains"": false,
                  ""equals"": false,
                  ""startsWith"": true,
                  ""value"": ""some text""
                },
                ""description"": {
                  ""contains"": true,
                  ""equals"": false,
                  ""startsWith"": false,
                  ""value"": ""some more text""
                },
                ""createdAfter"": {
                  ""contains"": false,
                  ""equals"": false,
                  ""startsWith"": false,
                  ""value"": ""2021-08-17""
                }
            },
            ""notes"": ""Something bad happened"",
            ""group"": ""some group"",
            ""assigned"": ""to me""
            }";
            
            var deserialized = JsonConvert.DeserializeObject<filter_keys>(inJson);
            Console.WriteLine(deserialized.notes);
            foreach(var prop in deserialized.properties)
            {
                Console.WriteLine(prop.Key);
                Console.WriteLine(prop.Value.value);
            }
        }
    
        public class filter_keys
        {
            public string notes { get; set; }
            public string group { get; set; }
            public string assigned { get; set; }
            public Dictionary<string, filter_properties> properties { get; set; }
        }
        public class filter_properties
        {
            public bool contains { get; set; }
            public bool equals { get; set; }
            public bool startsWith { get; set; }
            public string value { get; set; }
        }
    }
    

    See:

    https://dotnetfiddle.net/bdGTmf