Possible to customize the serialization behaviour of JsonUtility.ToJson and JsonUtility.FromJson?


We are using jsonUtility to serialize some of our objects to JSON and back again. I would like to know if it is possible to customise the way in which it deals with certain types?

In particular, I would like Vector3 to serialize to [0,0,0] instead of {x:0, y:0, z:0}.

Any ideas?


What you could try is to use the ISerializationCallbackReceiver interface, which gives you callbacks for packing/unpacking data ‘for serialisation.’ You’d have your Vector3 field as normal, but you’d mark it with [NonSerialized], and you’d add a [SerializeField] private float field. That field would be serialised instead of the vector, and in OnBeforeSerialize/OnAfterDeserialize you’d transfer the values between the fields.