The Unity 5.3 JSON serialization

Source: Internet
Author: User
Tags tojson unity 5

Sun Guangdong 2015.12.23


The JSON serialization feature and the conversion from JSON format to an object. It can be very useful to interact with a Web service, or just to package and split the data into a text-based format.

Easy to use
The JSON serialization feature is a JSON that surrounds a ' structured ', meaning that you describe the concept of a variable that will be stored in your JSON data by creating a class or struct. For example:


[Serializable]public class myclass{public    int level;    public float timeelapsed;    public string PlayerName;}

This defines a common C # class that contains three variables-level, timeelapsed, and playername-and marks it as serializable, which is necessary for it to work with the JSON serializer. You can then create an instance of it:


MyClass myObject = new MyClass (); myobject.level = 1;myobject.timeelapsed = 47.5f;myobject.playername = "Dr Charles Francis ";

By using Jsonutility.tojson. and serialize it into JSON format:


String json = Jsonutility.tojson (myObject);

This causes the variables to be included in the JSON string:

{"Level": 1, "timeelapsed": 47.5, "playername": "Dr Charles Francis"}

To convert the JSON back to an object, use Jsonutility.fromjson:


MyObject = jsonutility.fromjson<myclass> (JSON);


This creates a new MyClass instance and sets the value using the JSON data. If the JSON data contains values that do not map to fields in MyClass, these values will simply be ignored, and if the JSON data is missing the values of the fields in MyClass, then only those fields will be left in the object values that are returned in their construction.

The JSON serializer currently does not support unstructured ' JSON ' work (that is, navigating and editing JSON as a key-value pair for any tree). If you need to do this, you should look for a more complete JSON library.

Overwrite objects with JSON

It is also possible to take the JSON data and deserialize the ' in ' object that has already been created, overwriting the existing data:


Jsonutility.fromjsonoverwrite (JSON, myObject);



Leave any fields on the remaining JSON that do not contain a value object unchanged. This method allows you to overwrite them by deliberately using a JSON that contains only a small subset of the fields by reusing the objects you created earlier, and also the ' patch ' object remains assigned to the minimum.

Note that the JSON serializer API supports subclasses of Monobehaviour and Scriptableobject, as well as ordinary struct classes.  However, when the inverse JSON is serialized to a subclass of Monobehaviour or scriptableobject, you must use Fromjsonoverwrite; Fromjson is not supported and throws an exception.


Supported types

The API supports any monobehaviour subclass, scriptableobject-subclass, or generic class/struct that uses the [Serializable] attribute. The objects you replace are handled by the Unity Standard serializer, so the same rules and restrictions apply, also in the Inspector; only fields are serialized and are not supported like dictionary types.

Currently unsupported APIs, such as primitive types or arrays, pass other types. Now you will need to wrap the class in some sort of class or struct.

In the editor, there is also a parallel api-editorjsonutility-that allows you to JSON serialize any unityengine.object-derived type. This produces the JSON that contains the same data that YAML represents for the object.



Performance

Testing shows that jsonutility is a general workaround that is significantly faster than. NET JSON (although some features are less).

GC memory usage is at a minimum:
? ToJson () Allocates GC memory only for the returned string.
? Fromjson () allocates GC memory only for the returned object, and any child objects that are required (for example, if you deserialize an object that contains an array, the array will be allocated GC memory).
? Fromjsonoverwrite () allocates GC memory only as a requirement for written fields, such as strings and arrays. If all the fields that are covered by JSON are value types, it should not allocate any GC memory.

Use the jsonutility API from a background thread to be allowed. With any multithreaded code, you have to be careful not to access or change the object on one thread, rather than serialize/deserialize on another thread.

Control the output of the ToJson ()

ToJson support Beautiful-print JSON output. It is turned off by default, but you can turn it on by passing true as the second argument.
You can omit fields in the output by using the [NonSerialized] property.

The type is not known beforehand when using Fromjson ()

Deserialize the JSON into a class or struct that contains the ' common ' field, and then use the values of those fields to work out what kind of actual type you want. It then deserializes the second time to that type.

??

The Unity 5.3 JSON serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.