Performance: JSON serialization
This affects how quickly data is converted to and from JSON format, impacting load times and responsiveness when saving or loading game data.
string json = JsonUtility.ToJson(largeComplexObject, false);
string json = JsonUtility.ToJson(largeComplexObject, true);
| Pattern | CPU Usage | Memory Allocations | Frame Impact | Verdict |
|---|---|---|---|---|
| Pretty print enabled | High | High | Causes frame drops | [X] Bad |
| Pretty print disabled | Medium | Medium | Smooth frame rate | [OK] Good |
| Repeated deserialization every frame | Very High | High | Severe frame jank | [X] Bad |
| Cache deserialized data | Low | Low | Stable frames | [OK] Good |
What is the main purpose of JSON serialization in Unity?
Which of the following is the correct way to serialize an object player to JSON in Unity?
Player player = new Player();
Given the following code, what will be the output?
[System.Serializable]
public class Player {
public string name = "Alex";
public int score = 10;
}
Player player = new Player();
string json = JsonUtility.ToJson(player);
Debug.Log(json);What is wrong with this code snippet for deserializing JSON into a Player object?
string json = "{\"name\":\"Sam\", \"score\":15}";
Player player = JsonUtility.ToJson(json);You want to save a list of Player objects to JSON and load them back later. Which approach correctly handles this in Unity?
Pass the test and this pattern moves unknown