Use the JsonUtility class to convert Unity objects to and from the JSON format. For example, you can use JSON Serialization to interact with web services, or to easily pack and unpack data to a text-based format.
JSON Serialization uses a notion of “structured” JSON: you create a class or structure to describe what variables you want to store in your JSON data. For example:
[Serializable]
public class MyClass
{
public int level;
public float timeElapsed;
public string playerName;
}
This defines a plain C# class containing three variables (level, timeElapsed, and playerName) and marks it with the Serializable
attribute, in order to work with the JSON serializer. To create an instance of your class, you can use something like this:
MyClass myObject = new MyClass();
myObject.level = 1;
myObject.timeElapsed = 47.5f;
myObject.playerName = "Dr Charles Francis";
Then use the JsonUtility.ToJson method to serialize it (convert it) to the JSON format:
string json = JsonUtility.ToJson(myObject);
// json now contains: '{"level":1,"timeElapsed":47.5,"playerName":"Dr Charles Francis"}'
To convert the JSON back into an object, use JsonUtility.FromJson:
myObject = JsonUtility.FromJson<MyClass>(json);
This creates a new instance of MyClass
and sets the values on it using the JSON data. If the JSON data contains values that do not map to fields in MyClass
, then the serializer ignores those values. If the JSON data is missing values for fields in MyClass
, then the serializer leaves the constructed values for those fields in the returned object.
The JSON Serializer does not currently support working with unstructured JSON. That is, navigating and editing the JSON as an arbitrary tree of key-value pairs. If you need to do this, you should look for a more fully-featured JSON library.
You can also deserialize JSON data over an existing object, which overwrites any existing data:
JsonUtility.FromJsonOverwrite(json, myObject);
If the JSON data does not contain a value for a field, the serializer does not change that field’s value. This method allows you to keep allocations to a minimum by reusing objects that you created previously. It also allows you to “patch” objects by deliberately overwriting them with JSON that only contains a small subset of fields.
Warning: The JSON Serializer API supports MonoBehaviour and ScriptableObject subclasses as well as plain structs and classes. However, when deserializing JSON into subclasses of MonoBehaviour
or ScriptableObject
, you must use the FromJsonOverwrite method. If you try to use FromJson, Unity throws an exception because this beahvior is not supported.
The JSON Serializer API supports any MonoBehaviour
subclass, ScriptableObject
subclass, or plain class or struct with the [Serializable]
attribute. When you pass in an object to the standard Unity serializer for processing, the same rules and limitations apply as they do in the Inspector: Unity serializes fields only; and types like Dictionary<>
are not supported.
Unity does not support passing other types directly to the API, such as primitive types or arrays. If you need to convert those, wrap them in a class
or struct
of some sort.
In the Editor only, there is a parallel API, EditorJsonUtility, which allows you to serialize any object derived from UnityEngine.Object both to and from JSON. This produces JSON that contains the same data as the YAML representation of the object.
Benchmark tests indicate that JsonUtility is significantly faster than popular .NET JSON solutions, even though this class provides fewer features in some cases.
Memory usage for garbage collection (GC) is at a minimum:
You can use the JsonUtility API from a background thread. However, as with any multithreaded code, be careful not to access or alter an object on one thread while another thread is serializing or deserializing it.
The ToJson method supports pretty-printing the JSON output. It is off by default but you can turn it on by passing true
as the second parameter.
You can omit fields from the output by using the [NonSerialized]
attribute.
If you don’t know the type of an object ahead of time, deserialize the JSON into a class or struct that contains “common” fields, and then use the values of those fields to work out what actual type you want. Then deserialize a second time into that type.