FromJsonOverwrite(string, object)
Overwrite data in an object by reading from its JSON representation.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.JSONSerializeModule
FromJsonOverwrite(string, Object)
public static void FromJsonOverwrite(string json, object objectToOverwrite)
Parameters
Remarks
This method is very similar to JsonUtility.FromJson, except that instead of creating a new object and loading the JSON data into it, it loads the JSON data into an existing object. This allows you to update the values stored in classes or objects without any allocations.
Internally, this method uses the Unity serializer. The object you're creating and all its fields must meet the requirements for serialization by the Unity serializer. For the full list of these requirements, refer to Serialization rules in the manual.
FromJsonOverwriteNote: Since takes a reference type, passing a struct (a value type) to it requires careful boxing and unboxing and is not recommended. Instead, use JsonUtility.FromJson for structs as follows:
FromJsonOverwriteJsonUtility.FromJson<MyStruct>(json);If a field of the object is not present in the JSON representation, that field will be left unchanged.
FromJsonOverwriteExamples
using UnityEngine;public class PlayerState : MonoBehaviour{ public string playerName; public int lives; public float health; public void Load(string savedData) { JsonUtility.FromJsonOverwrite(savedData, this); } // Given JSON input: // {"lives":3, "health":0.8} // the Load function will change the object on which it is called such that // lives == 3 and health == 0.8 // the 'playerName' field will be left unchanged}