I am trying to create structure of nested objects on the flight inside of JsonConverter.WriteObject function.
Data structure:
Container
Object A
Object B
What am I doing wrong?
Can anyone recommend decent guide for writing Custom Converters?
var container = new JObject();
var fi = e.PropertyA;
JObject o = JObject.FromObject(fi);
o.AddFirst(new JProperty("type", new JValue(fi.GetType().Name)));
container.Add(o);
this approach failed with exception
Can not add Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JObject
if I do
writer.WriteStartObject();
var fi = e.PropertyA;
JObject o = JObject.FromObject(fi);
o.AddFirst(new JProperty("type", new JValue(fi.GetType().Name)));
o.WriteTo(writer);
writer.WriteEndObject();
'Token StartObject in state ObjectStart would result in an invalid JSON object. Path 'sources[0]'.'
Answer
Thanks to dbc I was able to make it trough with following code
var container = new JObject();
var fi = e.PropertyA;
JObject o = JObject.FromObject(fi);
o.AddFirst(new JProperty("type", new JValue(fi.GetType().Name)));
container.Add(new JProperty("ObjectA", o));
container.WriteTo(writer);