Is there a way to use JsonUtility without requiring public members?

I personally prefer to have private/protected members with Properties for exposing or modification to them. The JsonUtility: Unity - Scripting API: JsonUtility requires the members to be public and named as they would appear in Json which leads to some non-C# or non-json standard names.

If anyone has suggestions as to have this work without requiring public member variables I’d be very happy.

Bonus points if there is a way to tell it the JSON name for the member such that you can stick to conflicting standards.

As @talyh mentioned, you would have to use the almighty Json .Net , which offers a bundle of features that the primitive unity JsonUtlity doesn’t offer.

When you do download Json.Net. Net For Unity, the usage is pretty similar to unity’s JsonUtility for example

JsonUtility.ToJson becomes JsonConvert.SerializeObject
JsonUtility.FromJson<T> becomes JsonConvert.DeserializeObject<T>

To tell Json.Net to serialize a non public property or even a field, simply do this

[JsonProperty]
private int myfield = 10;

You can even tell Json.Net to deserialize or serialize the property using another name other than the one defined in your class for example

[JsonProperty("name")]
private int myfield = 10;

So now the plugin deserializes the field called name value into ‘myfield’ and serializes ‘myfield’ value to ‘name’ in json.

I’ve been using this, which I find offers a better JSON functionality than Unity’s JSON utility.