Class JsonObject<T>
Represents a generic JSON object that can be serialized and deserialized using Unity's JsonUtility. The derived class is required to have the [Serializable] attribute.
Inheritance
System.Object
JsonObject<T>
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: UniUtils.Data
Assembly: cs.temp.dll.dll
Syntax
public abstract class JsonObject<T>
Type Parameters
Name | Description |
---|---|
T | The type of the object to be deserialized. |
Methods
FromJson(String)
Declaration
public static T FromJson(string json)
Parameters
Type | Name | Description |
---|---|---|
System.String | json |
Returns
Type | Description |
---|---|
T |
Examples
// Define a serializable data model
[Serializable]
public class PlayerData : JsonObject<PlayerData>
{
public string playerName;
public int score;
}
// Create an instance and serialize it
PlayerData data = new PlayerData
{
playerName = "Alex",
score = 150
};
string json = data.ToJson();
Debug.Log(json);
// Output: {"playerName":"Alex","score":150}
// You can also deserialize it back:
PlayerData deserialized = PlayerData.FromJson(json);
Debug.Log(deserialized.playerName);
// Output: Alex
ToJson()
Serializes the current object to a JSON string.
Declaration
public string ToJson()
Returns
Type | Description |
---|---|
System.String | A JSON string representation of the current object. |
Examples
[Serializable]
public class PlayerData : JSONObject<PlayerData>
{
public string playerName;
public int score;
}
PlayerData data = new PlayerData { playerName = "Alex", score = 150 };
string json = data.ToJson();
Debug.Log(json); // Output: {"playerName":"Alex","score":150}