What JSON library would you recommend for my use case?

Currently I’m using the JsonUtility that comes with Unity, and while it works, I’m wondering if there would be a better option out there for me.

“Problem”: Right now I have a wrapper class for reading in JSON, which I then use to construct the class I actually want. This class has private variables that are set through the constructor, and Unity’s implementation can only use public variables. So while this wrapper solution works, it isn’t ideal, as I’d rather just put that data straight into the constructor of my class.

Bonus: Unity’s implementation enforces a fixed structure. Every variable in a class that matches a field in the JSON file is filled in with that field’s value, and every other field is discarded. The loose structure of JSON can be a benefit in some cases.

As always, thank you for your time!

This is what the ‘SerializeFieldAttribute’ is for. Just tested this one:

        [System.Serializable]
        public class Blargh
        {
            public string Name;
            public int Value;
            [SerializeField]
            private float _otherValue;

            public float OtherValue
            {
                get { return _otherValue; }
                set { _otherValue = value; }
            }
        }

Worked just fine.

[qutoe]Unity’s implementation enforces a fixed structure. Every variable in a class that matches a field in the JSON file is filled in with that field’s value, and every other field is discarded. The loose structure of JSON can be a benefit in some cases.[/quote]

I’m not understanding what you’re getting at… what else could you do with values in JSON that don’t have a corresponding field in the object type it’s deserialized to? There’s no field/variable to put it in, it has to be discarded.

Are you asking for a dynamic type. Like it deserializes into a named dictionary so you can access fields by name?

There are 3rd party libraries that support such things. Like Json.Net:
Json.NET - Newtonsoft

Of which some people have forked to get better support in unity (since unity is on an older version of mono/.net still):
https://www.google.com/search?q=json.net+unity&oq=json.net+unity&aqs=chrome.0.0l6.2240j0j4&sourceid=chrome&ie=UTF-8

But I’m not sure if this is what you need… you’re description here is vague to me.

Also, you can get similar functionality out of the unity serializer for dynamic properties. Just like how people serialize dictionaries, you can serialize a name/value pairing into 2 arrays with the ISerializationCallbackReceiver.

I don’t know. My main beef with the unity serializer is that it doesn’t support polymorphic serialization. If you have a field/var of a base type, and an object of some child type in it, it’ll be serialized/deserialized as the base type.

This is my only true gripe with the thing.

(this fact is also why you can’t use interfaces as a field type and let it be serialized… grrrrr)

Oh and my other gripe is that their ISerializationCallbackReceiver forces you to bloat your class/struct with fields for the data to be serialized. A system like .Net’s SerializationInfo would be so much nicer.

Perfect, just what I needed! Right now I have

int variableName { get; private set; }

But I suppose I will have to change it to this format

int variableName
{
    get { return _variableName; }
    private set { _variableName = value; }
}
private int _variableName;

Not really a big deal, I suppose. At least I can set default values this way.

This is exactly what I am looking to do.