0

Similar question was asked here Serialize/Deserialize dynamic property name using JSON.NET. I am not able to make it work in my situation as below:

I have the following JSON

{
    "notes": {
        "data": [{
                "book": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            },
            {
                "journal": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            },
            {
                "magazine": {
                    "items": [{
                            "page": "page 1",
                            "comment": "Some notes"
                        },
                        {
                            "page": "page 1",
                            "comment": "Some notes"
                        }
                    ]
                }
            }
        ]
    }
}

Created the following classes for JSON serialization:

public class TestParse
  {
    public Note Notes { get; set; }
  }
  public class Note
  {
    public IList<Data> Data { get; set; }
  }

  public class Data
  {
    public Source Source { get; set; }
  }

  [JsonConverter(typeof(MyConverter))]
  public class Source
  {
    public IList<Items> Items { get; set; }
  }

  public class Items
  {
    public string Page { get; set; }
    public string Comment { get; set; }
  }

  public class MyConverter : JsonConverter
  {
    public override bool CanConvert(Type objectType)
    {
      return objectType == typeof(Source);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
        JsonSerializer serializer)
    {
      var jo = JObject.Load(reader);

      var req = new Data
      {
        Source = jo.ToObject<Source>()
      };
      return req;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      var req = (Source)value;
      var jo = new JObject(
          new JObject(req.Items));
      jo.WriteTo(writer);
    }
  }
}

I am still not able to deserialize "Source." Any pointer is appreciated.

Varin
  • 119
  • 2
  • 12
  • Which of the property names above are dynamic? The [linked question](https://stackoverflow.com/questions/47043028/serialize-deserialize-dynamic-property-name-using-json-net) deals specifically with JSON of the form `{"type" : "some-name", "some-name" : "value" }` which doesn't look to match your JSON at all. – dbc Dec 05 '18 at 22:51
  • @dbc. The dynamic properties/objects are book, journal, and magazine in the above example. I was equating bank, card in the https://stackoverflow.com/questions/47043028/serialize-deserialize-dynamic-property-name-using-json-net to these – Varin Dec 05 '18 at 23:02

1 Answers1

1

In the other question you referenced, the dynamic key was determined by the value of another property in the same object. Hence, a converter was needed.

In your situation, you have dynamic keys, but they are not dependent on anything. You don't really need a converter here; you can just use a Dictionary to handle the dynamic keys.

Change this line in your Note class:

public IList<Data> Data { get; set; }

to this:

public IList<IDictionary<string, Source>> Data { get; set; }

and remove the [JsonConverter] attribute from your Source class. Then it should work.
(You can also delete the Data class as it is not needed.)

Fiddle: https://dotnetfiddle.net/80lIRI

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300