skrilmps
skrilmps

Reputation: 695

Custom JSON de/serializer in C# / Unity

I am working in Unity and trying to deserialize JSON.

The JSON was serialized (using Java code) from a data structure that looks like this:

Array of objects of Class A, where

Class A contains an array of objects of Class B, and

Class B contains an array of objects of Class C, and

Class C contains zero or one objects of Class D

The built-in Unity deserializer, JsonUtility, works fine for deserializing this data structure except for Class D, because class D uses polymorphism. To be more specific, Class D is actually an Interface, and the object that's being deserialized is either of subclass D1 or subclass D2.

Is there a way I can write a custom deserializer for Class D and only for Class D? I am happy to let the built-in library handle deserializing Classes A through C, but I need to do something special to deserialize Class D.

In Java this was simple because I was able to register a custom deserializer that is called whenever objects of Class D are de/serialized. Within that deserializer I was able to do some introspection to see what type of subclass we're dealing with (e.g., is it D1 or D2?) and deserialize it appropriately.

I'm not sure how to do this in Unity (or C#).

I have looked into MiniJSON, LitJSON, SimpleJSON, and NewtonSoft's JSON.NET but I haven't found anything in those (maybe I missed it?) that allows you to write a custom deserializer for a class.

The closest I've found is JSON.NET's CustomCreationConvertor:

http://www.newtonsoft.com/json/help/html/CustomCreationConverter.htm

which lets you call the constructor for the Class you wish to deserialize into (and the built-in deserializer does the rest) but I'm not sure where I'd be able to do any introspection in order to decide which subclass to construct.

Thanks for any suggestions!

Update: In the comments, Abion47 posted this: How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

That thread shows me how to write the deserializer for Class D. But a question still remains: how do I invoke the custom deserializer? In the examples given, the custom deserializer is called like this:

D sampleD = JsonConvert.DeserializeObject<D>(json, new CustomDConverter());

The custom deserializer is passed in. But how can I do this if I am also wanting to use the built-in deserializer for Class C, within which Class D is nested?

Is there any way to tell JSON.NET that I want it to use CustomDConverter any time it encounters an object of Class D (and otherwise, just proceed normally)?

Upvotes: 2

Views: 5131

Answers (1)

skrilmps
skrilmps

Reputation: 695

I've found the solution to my question.

Use the instructions here to write a custom serializer: How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

Suppose your serializer is called CustomConverter (it must exted JsonConverter ... see the link above).

Add this serializer to your Converters and then invoke Deserialize<>() as normal:

JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new CustomConverter());

YourClass deserializedObject = serializer.Deserialize<YourClass>(jsonReader);

Upvotes: 1

Related Questions