1

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);
Alex G
  • 595
  • 6
  • 21
  • As I understand it, Newtonsoft has built in serializer and deserializer. Why not build your object graph and then serialize it to a json object? You are trying to reinvent something that is readily available to you. Also, what you are attempting here will be much harder to read/maintain/update. Good luck, but I recommend using the tools that are already available. – user7396598 Apr 10 '18 at 16:34
  • 1
    According to the [standard](http://www.json.org/) A JSON object is an *unordered set of name/value pairs* so when writing an object you need to do [`WritePropertyName()`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonWriter_WritePropertyName.htm) to before writing a property value. Similarly when adding an object to an object you need to nest it in a [`JProperty`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JProperty.htm) to specify the name. – dbc Apr 10 '18 at 16:37
  • user7396598 this is a part of much broader technical issue. you can read my other question on the entire topic in here https://stackoverflow.com/questions/49756595/custom-json-converter-for-a-collection-of-a-container-with-polymorphic-propertie – Alex G Apr 10 '18 at 16:38
  • *Can anyone recommend decent guide for writing Custom Converters?* -- That's off-topic for stack overflow I think. – dbc Apr 10 '18 at 16:55
  • 1
    However, if you are looking to add a synthetic property `"type"` to all your objects, you might look at [Json.net Add property to every class containing of a certain type](https://stackoverflow.com/q/46549680/3744182) which suggests using a custom contract resolver. – dbc Apr 10 '18 at 17:00

1 Answers1

1

According to the JSON standard a JSON object is an unordered set of name/value pairs so when writing an object you need to do WritePropertyName() before writing a property value:

writer.WritePropertyName("PropertyA");
o.WriteTo(writer);

Similarly when adding a JObject to a JObject you need to nest it in a JProperty to specify the name:

container.Add(new JProperty("PropertyA", o));

The exceptions you are getting reflect the fact that you are trying to add your PropertyA value to a JSON object without first specifying its name.

Working .Net fiddle showing both.

dbc
  • 104,963
  • 20
  • 228
  • 340