Why I got empty JSON string returned while using setter and getter in the object class in Unity 5.3?
Unity can not serialize properties.
http://docs.unity3d.com/ScriptReference/SerializeField.html
The serialization system used can do the following:
- CAN serialize public nonstatic fields (of serializable types)
- CAN serialize nonpublic nonstatic fields marked with the [SerializeField] attribute.
- CANNOT serialize static fields.
- CANNOT serialize properties.
Your field will only serialize if it is of a type that Unity can serialize:
Serializable types are:
- All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.
- All basic data types like int, string, float, bool.
- Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
- Arrays of a serializable type
- List of a serializable type
- Enums
- Structs
- List item
EDIT: Only plain classes and structures are supported; classes derived from UnityEngine.Object (such as MonoBehaviour or ScriptableObject) are not.
https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html
Comments
-
Ares Li almost 2 years
I tried to use the new JSON serialization feature in Unity 5.3, and I wrote the following code by reference the usage example provided On Unity website. The only different part that I made was creating the variables of the object class (FruitItem class in my case) by using setter and getter instead of making them pure public variables. By doing this, I only got a pair of braces without any contents inside. However, if I delete the getter and setter and make the class variables to be pure public variables, I will be able to get the correct result. Can anybody provide any hints to me why that happened? Thanks in advance for your help.
Code that works properly:
using UnityEngine; using UnityEditor; using System.Collections; using System; public class testJson : MonoBehaviour { // Use this for initialization void Start () { FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 }; string jsonString = JsonUtility.ToJson (myFruit); Debug.Log (jsonString); } // Update is called once per frame void Update () { } } [Serializable] public class FruitItem{ //using the pure public variables and the output will be: //{"name":"apple","quantity":53,"price":52} public string name; public int quantity; public int price; }
Code that doesn't work properly:
using UnityEngine; using UnityEditor; using System.Collections; using System; public class testJson : MonoBehaviour { // Use this for initialization void Start () { FruitItem myFruit = new FruitItem (){ name = "apple", price = 52, quantity = 53 }; string jsonString = JsonUtility.ToJson (myFruit); Debug.Log (jsonString); } // Update is called once per frame void Update () { } } [Serializable] public class FruitItem{ //using the pure public variables and the output will be: //{} public string name{ get; set;} public int quantity{ get; set;} public int price{ get; set;} }
-
Ares Li over 8 yearsThanks for your response and the reference you provided, Mattias. I didn't realize there are limitations for types supported by unity serialization. However, the keyword "[Serializable]" was referenced by "using System". It seems like a .Net stuff. I am wondering if the serialization limitations set on Unity will impact that far. Thanks again.
-
tim almost 8 yearsThanks for your answer
-
Brent McFerrin over 7 yearsI'm not sure this is still accurate. According to the latest documentation: docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html "Only plain classes and structures are supported; classes derived from UnityEngine.Object (such as MonoBehaviour or ScriptableObject) are not." It seems like the first bullet in your list of Serializable types is now incorrect.