Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ricardo-charf · Oct 27, 2016 at 06:48 PM · jsonissuethreadsmultithreadingdeserialization

Having issues when multithreading and deserializing JSON with JSONUtility

Hello everybody!

I am having a problem. There is a part of my code where i need do deserialize a very big JSON. Since it is a lot of effort to do that and I don't want to stop the UI and other important stuff i decided to execute the serialization in a separate thread. And then callback to the main thread when the deserialization is complete.

But differently than what i expected, the UI and the rest of execution stops and wait for the deserialization to complete as if there were no separate thread. Obs: The deserialization works fine. The problem is that the execution stops as if there were no threads.

My code is somewhat as follows:

 IEnumerator DeserializeObjectAsync(string json,Action<MyObject> callback){
     bool threadFinished = false;
     MyObject deserializedObject = null;
     Thread t = new Thread (delegate() {
         deserializedObject = JsonUtility.FromJson<MyObject>(json);
         threadFinished = true;
     });
     t.Start ();
     while(!threadFinished){
         yield return new WaitForEndOfFrame ();
     }
         callback(deserializedObject);
 }

Does someone know what may be the issue or how to solve it? Thank you!

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image altaf_navalur · Jan 24, 2017 at 01:13 PM 0
Share

Did you find any solution to this?, I am also running into the same issue.

avatar image HenryStrattonFW · Jan 24, 2017 at 01:51 PM 0
Share

Is the JsonUtility even thread safe? It was my understanding that you should avoid using most of the unity api on other threads as anything outside of the struct types are not thread safe. $$anonymous$$ay be completely unrelated to your issue, just curious is you can confirm that JsonUtility is threadsafe or not.

avatar image CesarNascimento HenryStrattonFW · Jan 24, 2017 at 02:19 PM 1
Share

Yes it is https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

The versions of this method that take strings can be called from background threads

avatar image HenryStrattonFW CesarNascimento · Jan 24, 2017 at 08:09 PM 0
Share

Ah cool, good to know.

avatar image altaf_navalur HenryStrattonFW · Jan 24, 2017 at 02:21 PM 0
Share

I don't use JsonUtility as it has limitations. I use JSONDeserializer,

avatar image ricardo-charf altaf_navalur · Jan 25, 2017 at 11:29 AM 0
Share

Did it solve the problem, changing to JSONDeserializer?

avatar image ricardo-charf · Jan 25, 2017 at 11:28 AM 0
Share

I did not find any solution at all, until now, no :-( $$anonymous$$aybe, changing the deserialization method could work... But I am not sure.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hippogames · Dec 05, 2021 at 11:02 AM

Hi, just use Newtonsoft JSON, it works fine.

 private IEnumerator DeserializeInThread(string json)
 {
     List<UserImage> images = null;
 
     var thread = new Thread(() => images = JsonConvert.DeserializeObject<List<UserImage>>(json));
 
     thread.Start();
 
     while (images == null)
     {
         yield return null;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ArgumentException: Cannot deserialize JSON to new instances of type 0 Answers

Parse JSON Data with JSON Utility to Dictionary object 1 Answer

Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer

Instantiating many objects during update lags 1 Answer

Application.ExternalCall Issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges