- Home /
Is it possible to executing object serialization into JSON asynchronously?
Hello, in the game my team currently developing, we need to serialize an user data object in C# into JSON format. We're currently using JSON.NET for the serialization process.
We've got it working, but the serialization process, this line
string serialized = JsonConvert.SerializeObject(playerData, Formatting.None);
can take about 300ms, which is visible as a stutter from the player perspective. Is there anyway to make the process ran asynchronously and notify when it's finished so it does not causing frame spikes?
Thank you and have a good day.
Can you just put it in a coroutine?
// Use this for initialization void Start () { StartCoroutine(SerializeJSON()); } private IEnumerator SerializeJSON() { serialized = JsonConvert.SerializeObject(playerData, Formatting.None); }private string serialized;
Depending on what PlayerData is, it should be possible. Have you tried it?
What does your object tree look like? How many instances and classes are involved? 300ms is just crazy.
I usually perfer a semi-manual serialization process since i have much more control over what is serialized and how it is serialized. I usually use my SimpleJSON framework. Each class that should be serialized can have a simple instance method that returns a "JSONNode". In addition it might have a constructor that takes a JSONNode or provide an Initialize / Deserialize method that takes one.
I've successfully serialized / deserialized JSON files of several $$anonymous$$Bs that way.
Of course there would be no "automatic" support for your custom classes, but that's generally makes serialization slow since JSON.NET has to use reflection to exa$$anonymous$$e your classes. Using a semi-manual approach also allows you to directly serialize / deserialize GameObjects and $$anonymous$$onoBehaviours. In addition you can easily add extension methods for common Unity classes / structs.
Answer by tMahon · Jun 12, 2017 at 06:37 AM
Oh right! I just forgot to call the Async Operation.
private string serialized;
Start(){
StartCoroutine(SerializeJSONStart());
}
private IEnumerator SerializeJSONStart(){
AsyncOperation async = SerializeJSON();
while(!async.isDone){
yield return null;
}
}
private void SerializeJOSN(){
serialized = JsonConvert.SerializeObject(playerData, Formatting.None);
}
That doesn't work either. AsyncOperation is a special class that is used by internal classes of the Unity engine. $$anonymous$$ainly by the scene loader.
Also a line like this:
AsyncOperation async = SerializeJSON();
Will always first execute the "SerializeJSON" method and when it's finished you would assign the return value of that method to the "async" variable. Since "SerializeJSON" doesn't return anything your code would not even compile (Can't convert "void" to "AsyncOperation").
Even if this construct would magically start a seperate thread, it would be extremely dangerous without knowing who accesses the date from where. If you want to do background stuff in seperate threads you need to use proper synchronisation / locking unless you are aware of all sideeffects.
Your answer
Follow this Question
Related Questions
How to serialize parameters from JSON? 1 Answer
How to protect JSON file game data? 3 Answers
Footprint of System.Web on a project 0 Answers
JsonUtility array not supported? 7 Answers