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 furibaito · Jun 12, 2017 at 01:59 AM · c#performanceserializationjsonasynchronous

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.

Comment
Add comment · Show 4
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 tMahon · Jun 12, 2017 at 05:20 AM -1
Share

Can you just put it in a coroutine?

private string serialized;

// Use this for initialization void Start () { StartCoroutine(SerializeJSON()); } private IEnumerator SerializeJSON() { serialized = JsonConvert.SerializeObject(playerData, Formatting.None); }
avatar image hexagonius tMahon · Jun 12, 2017 at 05:39 AM 1
Share

A coroutine is not asynchronous.

avatar image hexagonius · Jun 12, 2017 at 08:06 AM 0
Share

Depending on what PlayerData is, it should be possible. Have you tried it?

avatar image Bunny83 · Jun 14, 2017 at 03:11 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }
Comment
Add comment · Show 1 · 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
avatar image Bunny83 · Jun 12, 2017 at 08:21 AM 0
Share

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

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

341 People are following this question.

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

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

JsonUtility doesn't serialize nested mixed var 1 Answer


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