- Home /
Best way to handle Deserializing a large JSON file from a website?
I am trying to get some data from a website in Unity and create objects based on that data. The data is easily retrieved using the WWW class but I am having trouble finding an easy to way create an easy to work with object to get the data I need. The JSON Utility class I have heard is not the best because I will need to create a template class for all of the data. The data is updated and is dynamic so it does not make sense to me to create a huge template class to fit a dynamic database.
JSON Object, which I found on the asset store, does exactly what I want. It allows me to create a JSON Object from a string and I am able to access data directly by index. But now I am finding out that it was coded very poorly and the garbage collection being done after I use it is so large that it causes huge lag spikes.
I thought if my lag spikes were because the asset was not coded correctly then it could be fixed with another JSON asset for Unity but I cannot find any others that work as simply as JSON Object.
Maybe there is another problem I am not seeing but right now I think the only solutions are
1. A better JSON plugin
2. A way to fix the large garbage collection spike
3. Retrieve data on a seperate thread (Which also seems to not be easy with Unity)
Ya Im stuck, any help would be appreciated.
Answer by Bunny83 · Jun 22, 2018 at 11:23 PM
Well the amount of data isn't that much. The main issue is that no matter which object representation you actually use on the C# side you will always allocate about the same amount of memory which need to be collected once you're done. Almost all JSON parsers have to completely parse all the data in order to access any of the individual values. While it would be possible to implement something like a SAX reader for JSON, i don't know any implementation and the usage would be way different than using any "normal" JSON parser.
I've written the SimpleJSON parser which tries to minimize the allocated memory. It uses stongly typed classes for the different json values and provides an easy to use interface. I've used it to store quite complex data of several MB. It also allows easy data addition or manipulation at runtime.
Within the same repository i also have some Unity specific extensions for easy read and write support of the basic Unity types (vectors, quaterion, rect, etc...)
I've used Bunny83's SimpleJSON parser extensively, and it is certainly up to the task of efficiently parsing the example data from that link.
Agreed, SimpleJSON is my Unity JSON parser of choice, and it's served me well on many projects.
Your answer
Follow this Question
Related Questions
Very Bad Lag from ~20 Large Particles 0 Answers
force and delay Garbage Collection 7 Answers
Zero Garbage Policy 1 Answer
OnTriggerEnter causing lag spike? 0 Answers