0

My situation:

I've got a class from a library that has a bunch of fields I don't need and causes issues on deserialization, I only need one of the fields to serialize and deserialize. I've got it! JsonConverter will do the trick, right? So I want to serialize and deserialize the object using JsonConverter but my issue is I want to keep the "header" stuff Json.Net generates, like "$id" and "$type" because it's being serialized similar to other objects. I don't know where to find the $id of the current spot in serialization so I could use some help!

    public override void WriteJson(JsonWriter writer, object v, JsonSerializer serializer)
    {
        MyObject myObject = (MyObject)v;
        string myCoolString = myObject.myString;

        MyObjectSerialized myObjectSerialized = new MyObjectSerialized()
        {
            myString = myCoolString 
        };

        JObject jo = JObject.FromObject(myObjectSerialized);

        jo.WriteTo(writer);
    }

Output:

{
    "myString":"test"
}

Expected output:

{
    "$id":"34",
    "$type":"MyObject, Assembly-CSharp",
    "myString":"test"
}
Occlusion
  • 11
  • 1
  • When you establish the JsonSerializer , use serializer settings with `TypeNameHandling = TypeNameHandling.Objects`... –  Sep 07 '18 at 21:07
  • Yep! That's already set! That doesn't seem to work by default in custom JsonConverters with a custom WriteJson – Occlusion Sep 07 '18 at 21:07
  • Ah, i see. You might try an approach like in the answer to this question: https://stackoverflow.com/questions/29810004/using-custom-jsonconverter-and-typenamehandling-in-json-net. Note that it avoids using `JObject.FromObject` or similar in the overriden WriteJson method, but rather creates a second JsonSerializer with the appropriate settings (and making sure to remove itself from the converter list of that serializer to avoid infinite recursion) to serialize/write the object to the JsonWriter. (Not sure about the $id field, though...) –  Sep 07 '18 at 21:13
  • Yeah unfortunately that's the really important bit as this is a list of all the same subclass of component, all of which serializes/deserializes fine, except this one Component subclass is giving me an issue and I think the solution lies in JsonConverter. I don't want to lose the $id field because that would break any referencing and I don't want to treat this class any different than other Component subclasses. – Occlusion Sep 07 '18 at 21:17
  • The interesting thing is there's a private member of JsonWriter called "_writeBuffer" that if I could get the length to, would solve the issue. I could reflect to it but that makes me slightly nervous. – Occlusion Sep 07 '18 at 21:24

1 Answers1

1

I figured it out. If you get the JsonSerializer's ReferenceResolver that's passed into the WriteJson method, you can extract the reference ID to the object you're serializing with this:

string id = serializer.ReferenceResolver.GetReference(serializer, v);

Since you're passing in "serializer" as the context, that's the relative point it's getting the id from. I'll be using a similar method to the one posted in the comment to my initial post to get the type.

Occlusion
  • 11
  • 1