Search Unity

  1. Going to #UniteBerlin? Bring a buddy & get 25% off the 2nd ticket! More info here.
    Dismiss Notice
  2. Famed game designer and creator of The Sims, SimCIty, and Spore is looking for a 3D artist to join their team. Join the challenge.
    Dismiss Notice
  3. Unity 2017.4 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. We've opened a new World Building sub-forum to faciltate all your world building discussions! Drop in and ask any ProBuilder, ProGrids, or PolyBrush questions.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

JSON .NET for Unity

Discussion in 'Assets and Asset Store' started by Dustin-Horne, Sep 13, 2013.

  1. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Ahh sorry, it's a little bit different... the settings are on the serializer itself. I'll test it with the converters but here is an example of how to do it in a console application. In this particular sample I created a class with circular references and serialized it by setting up the properties on the JsonSerializer:

    Code (csharp):
    1.  
    2. public class Foo
    3. {
    4.     public int Value { get; set; }
    5.     public Foo Sibling { get; set; }
    6. }
    7.  
    8. static void Main(string[] args)
    9. {
    10.     var foo1 = new Foo
    11.     {
    12.         Value = 4
    13.     };
    14.  
    15.     var foo2 = new Foo
    16.     {
    17.         Value = 1,
    18.         Sibling = foo1
    19.     };
    20.  
    21.     foo1.Sibling = foo2;
    22.  
    23.     byte[] data;
    24.  
    25.     using (var ms = new MemoryStream())
    26.     {
    27.         using (var writer = new BsonWriter(ms))
    28.         {
    29.             var serializer = JsonSerializer.Create();
    30.                  
    31.             serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    32.             serializer.Serialize(writer, foo1);
    33.  
    34.             data = ms.ToArray();
    35.         }
    36.              
    37.     }
    38.  
    39.     using (var ms = new MemoryStream(data))
    40.     {
    41.         using (BsonReader reader = new BsonReader(ms))
    42.         {
    43.             JsonSerializer serializer = new JsonSerializer();
    44.             var newFoo = serializer.Deserialize<Foo>(reader);
    45.  
    46.             Console.WriteLine(newFoo.Value); //4
    47.             Console.WriteLine(newFoo.Sibling.Value); //1
    48.             Console.WriteLine(ReferenceEquals(null, newFoo.Sibling.Sibling)); //true (ignored)
    49.         }
    50.     }
    51.  
    52.     Console.ReadKey();
    53.  
    54.  
    55. }
    56.  
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Here is the same example, but this time using ReferenceLoopHandling.Serialize and PreserveReferencesHandling.All which keeps track of references. You'll see that newFoo.Sibling.Sibling is no longer null because it keeps track of the references.. but I need to test this with structs.

    Code (csharp):
    1.  
    2. static void Main(string[] args)
    3. {
    4.     var foo1 = new Foo
    5.     {
    6.         Value = 4
    7.     };
    8.  
    9.     var foo2 = new Foo
    10.     {
    11.         Value = 1,
    12.         Sibling = foo1
    13.     };
    14.  
    15.     foo1.Sibling = foo2;
    16.  
    17.     byte[] data;
    18.  
    19.     using (var ms = new MemoryStream())
    20.     {
    21.         using (var writer = new BsonWriter(ms))
    22.         {
    23.             var serializer = JsonSerializer.Create();
    24.  
    25.             serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
    26.             serializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
    27.  
    28.             serializer.Serialize(writer, foo1);
    29.  
    30.             data = ms.ToArray();
    31.         }
    32.  
    33.     }
    34.  
    35.     using (var ms = new MemoryStream(data))
    36.     {
    37.         using (BsonReader reader = new BsonReader(ms))
    38.         {
    39.             JsonSerializer serializer = new JsonSerializer();
    40.             var newFoo = serializer.Deserialize<Foo>(reader);
    41.             serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
    42.             serializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
    43.  
    44.             Console.WriteLine(newFoo.Value); //4
    45.             Console.WriteLine(newFoo.Sibling.Value); //1
    46.             Console.WriteLine(ReferenceEquals(null, newFoo.Sibling.Sibling)); //false
    47.         }
    48.     }
    49.  
    50.     Console.ReadKey();
    51.  
    52.  
    53. }
    54.  
    55. public class Foo
    56. {
    57.     public int Value { get; set; }
    58.     public Foo Sibling { get; set; }
    59. }
    60.  
     
  3. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Here's an example... setting ReferenceLoopHandling to Ignore and using it with Structs instead of Classes (which mirrors Vectors.

    Code (csharp):
    1.  
    2.         static void Main(string[] args)
    3.         {
    4.             var foo1 = new Foo
    5.             {
    6.                 Value = 4
    7.             };
    8.  
    9.             byte[] data;
    10.  
    11.             using (var ms = new MemoryStream())
    12.             {
    13.                 using (var writer = new BsonWriter(ms))
    14.                 {
    15.                     var serializer = JsonSerializer.Create();
    16.  
    17.                     serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    18.                     serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
    19.  
    20.                     serializer.Serialize(writer, foo1);
    21.  
    22.                     data = ms.ToArray();
    23.                 }
    24.  
    25.             }
    26.  
    27.             using (var ms = new MemoryStream(data))
    28.             {
    29.                 using (BsonReader reader = new BsonReader(ms))
    30.                 {
    31.                     JsonSerializer serializer = new JsonSerializer();
    32.                     var newFoo = serializer.Deserialize<Foo>(reader);
    33.  
    34.                     Console.WriteLine(newFoo.Value); //4
    35.                     Console.WriteLine(newFoo.Sibling.Value); //1
    36.                 }
    37.             }
    38.  
    39.             Console.ReadKey();
    40.         }
    41.  
    42.         public struct Foo
    43.         {
    44.             public int Value { get; set; }
    45.  
    46.             public Foo Sibling { get { return new Foo { Value = 1 }; } }
    47.         }
    48.  
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    One final example for your @SureSight and I think this is the path you want to take. Since you have to create a new instance of Serializer for BSON it doesn't use the "default converters". Instead you'll need to populate the Converters on the Serializer object. The below example just adds a new Vector3 converter and shows serialization of a Vector3. In your case you'll want to do this:

    Code (csharp):
    1.  
    2. serializer.Converters.AddRange(JsonConvert.DefaultSettings.Converters);
    3.  
    The above will take all of the default converters and put them into your converter list. But the below example shows it working with the Vector3 converter:

    Code (csharp):
    1.  
    2.             var vect = new Vector3(20.0f, 35.3f, 127.2f);
    3.  
    4.             byte[] data;
    5.  
    6.             using (var ms = new MemoryStream())
    7.             {
    8.                 using (var writer = new BsonWriter(ms))
    9.                 {
    10.                     var serializer = JsonSerializer.Create();
    11.  
    12.                     serializer.Converters.Add(new VectorConverter());
    13.  
    14.                     serializer.Serialize(writer, vect);
    15.  
    16.                     data = ms.ToArray();
    17.                 }
    18.  
    19.             }
    20.  
    21.             using (var ms = new MemoryStream(data))
    22.             {
    23.                 using (BsonReader reader = new BsonReader(ms))
    24.                 {
    25.                     JsonSerializer serializer = new JsonSerializer();
    26.                     serializer.Converters.Add(new VectorConverter());
    27.  
    28.                     var newVect = serializer.Deserialize<Vector3>(reader);
    29.  
    30.                     Console.WriteLine(newVect.x); //20
    31.                     Console.WriteLine(newVect.y); //35.3
    32.                     Console.WriteLine(newVect.z); //127.2
    33.                 }
    34.             }
    35.  
    36.             Console.ReadKey();
    37.  
     
    SureSight likes this.
  5. mihakinova

    mihakinova

    Joined:
    Jan 6, 2015
    Posts:
    82
    Found a temporary solution. This only happens when trying to serialize / deserialize properties. The fix is to convert your properties to regular fields (remove {get;set;}). After that it works. Though we'd probably need a better, more long-term solution.
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Are the properties that are erroring out all collections? Also could you please reproduce and send a bug report to Unity. This is a bug that they seem to fix and then it pops back up again in a later version.
     
  7. mihakinova

    mihakinova

    Joined:
    Jan 6, 2015
    Posts:
    82
    Hmm, not sure if it was just collections, I converted all my models to regular fields and it worked. Will test it out and send a bug report to Unity & post it here.
     
    Dustin-Horne likes this.
  8. SureSight

    SureSight

    Joined:
    Aug 2, 2013
    Posts:
    45
    @Dustin-Horne The code for the default converters won't compile. See attachments for compile errors.
    Everything else is bang on.

    Many thanks.

    Code (csharp):
    1.  
    2. serializer.Converters.AddRange(JsonConvert.DefaultSettings.Converters);
    3.  
    [/QUOTE]
     

    Attached Files:

  9. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    73
  10. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    What you want is JsonConvert.PopulateObject(jsonString, objectToOverwrite);

    There's also a second overload that accepts a JsonSerializerSettings instance as a third parameter.

    https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_PopulateObject.htm
     
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    [/QUOTE]

    Sorry, I forgot that's a Collection<T> and not a List<T>. You'll have to do something like:

    Code (csharp):
    1.  
    2. foreach(var converter in JsonConvert.DefaultSettings().Converters)
    3. {
    4.     serializer.Converters.Add(converter);
    5. }
    6.  
    Or you could just write an extension method to populate it for you.
     
    SureSight likes this.
  12. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    73
    Hello! Do you support product? why asset is free?)))
     
  13. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Yes I still support it. :) It's free now because Google approached me and wanted to include it as part of their Poly API for Unity. So, I worked with them and made it free.
     
  14. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    73
    That sounds good! Good luck ! Asset is very useful! Thank you!
     
    Dustin-Horne likes this.
  15. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    You're quite welcome. :)
     
  16. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    69
    An Extreme Issue:

    Let's the file gets corrupted & the string has (let's say) 3 newline characters or System.String.Empty.

    Now I do the following

    Code (CSharp):
    1.  
    2.     currentDirectoryInformation = JsonConvert.DeserializeObject<JSONEntities.DirectoryInformation> (_jsonBuffer);
    3.  
    4.                 Debug.Log (currentDirectoryInformation.FileNames.Count);
    5.  
    6.                 if (currentDirectoryInformation.FileNames.Contains (fileName) == true)
    7.                 {
    8.                     currentDirectoryInformation.FileNames.Remove (fileName);
    9.                     currentDirectoryInformation.Count.FileCount--;
    10.                 }
    11.  
    The above results in

    Object reference not set to an instance of an object


    on
    Debug.Log (currentDirectoryInformation.FileNames.Count);



    My Question is, let's say I roll this for production, what is the best way to handle it?

    Thanks,
    Karsnen

    Code (CSharp):
    1.     [System.Serializable]
    2.     public sealed class DirectoryInformation // from which currentDirectoryInformation is init
    3.     {
    4.         public ContentCount Count;
    5.         public System.Collections.Generic.List<string> FolderNames;
    6.         public System.Collections.Generic.List<string> FileNames;
    7.    
    8.         public string CreationTime;
    9.         public string LastTouchedTime;
    10.  
    11.         public DirectoryInformation()
    12.         {
    13.             Count = new ContentCount ();
    14.             FolderNames = new System.Collections.Generic.List<string> ();
    15.             FileNames = new System.Collections.Generic.List<string> ();
    16.         }
    17.     }
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    I'm not sure I follow. You mean you want to know what to do if your file is corrupt and invalid?
     
  18. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    69
    Hello Dustin,

    Let me elaborate.

    Scenario :
    1. I save JSON strings in a file and then use them on demand
    2. I check if that JSON file exists then extract the string from that file to

     
    currentDirectoryInformation = new JSONEntities.DirectoryInformation ();
    if (FileHelper.FileExists (relativePathToMetaFile) == true)
    {
    string _jsonBuffer = System.String.Empty;
    _jsonBuffer = FileHelper.ReadJsonFromFile (relativePathToMetaFile);
    currentDirectoryInformation = JsonConvert.DeserializeObject<JSONEntities.DirectoryInformation> (_jsonBuffer);
    Debug.Log (currentDirectoryInformation.FileNames.Count);

    if (currentDirectoryInformation.FileNames.Contains (fileName) == true)
    {
    currentDirectoryInformation.FileNames.Remove (fileName);
    currentDirectoryInformation.Count.FileCount--;
    }
    }


    At this time, now if the string (_jsonBuffer) has an empty string or is corrupted - then the statement
    Debug.Log (currentDirectoryInformation.FileNames.Count);


    throws the following error


    NullReferenceException: Object reference not set to an instance of an object

    The above error is thrown for
    Debug.Log (currentDirectoryInformation.FileNames.Count);

    statement.

    I'm curious as to why an error is thrown when the class is being initialized properly from the statement

    currentDirectoryInformation = new JSONEntities.DirectoryInformation ();



    The below class is the class, I intend to extract information.
    Code (CSharp):
    1.     [System.Serializable]
    2.     public sealed class DirectoryInformation
    3.     {
    4.         public ContentCount Count;
    5.         public System.Collections.Generic.List<string> FolderNames;
    6.         public System.Collections.Generic.List<string> FileNames;
    7.    
    8.         public string CreationTime;
    9.         public string LastTouchedTime;
    10.  
    11.         public DirectoryInformation()
    12.         {
    13.             Count = new ContentCount ();
    14.             FolderNames = new System.Collections.Generic.List<string> ();
    15.             FileNames = new System.Collections.Generic.List<string> ();
    16.         }
    17.     }

    I guess I'm clear this time.

    I highly appreciate your help. Thanks.
     
  19. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Oh, you're calling DeserializeObject but the Json string is empty so it's returning null, making your whole object null.

    To avoid that, use JsonConvert.PopulateObiect inetead which will attempt to Deserialize the string on top of your existing object.
     
  20. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    69
    Thank you Dustin.

    I think that helped but if this error is returned -


    JsonSerializationException: No JSON content found. Path '', line 0, position 0.


    What is the best approach? I'm just trying to create a failsafe if the file is corrupted. Please advise me, if I have to take another approach.

    I highly appreciate your time, Dustin.

    Thanks.
     
  21. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    Code (csharp):
    1.  
    2. if(!string.IsNullOrWhitespace(_jsonBuffer))
    3.     //populate object
    4.  
     
  22. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    69
    Thank you Dustin.
     
    Dustin-Horne likes this.
  23. AlbinZosimusJulius

    AlbinZosimusJulius

    Joined:
    Nov 21, 2015
    Posts:
    1
    Hi Dustin, first of all thank you for this great asset!
    I have a problem using JSON.NET in a WebGL build. I send small json data packets from the browser (multiple times per second) to Unity using 'SendMessage'. There I read the data manually by using the JsonTextReader:
    Code (CSharp):
    1.  
    2. using(var sr = new StringReader(json))
    3. using(var reader = new JsonTextReader(sr))
    4. {
    5.     while(reader.Read())
    6.     {
    7.         if((reader.TokenType == JsonToken.PropertyName) && (reader.Depth == 1))
    8.         {
    9.             switch(reader.Value.ToString())
    10.             {
    11.                 case "action":
    12.                     blabla = reader.ReadAsString();
    13.                     break;
    14.  
    15.                 case "uid":
    16.                     uid = reader.ReadAsString();
    17.                     break;
    18.  
    19.                 // four other cases...
    20.             }
    21.         }
    22.     }
    23. }
    But after some time I will eventually face a WebGL "Out of memory!" error. It seems that the instantiation of text readers or something will produce a lot of garbage that will overflow the allocated memory in the browser. Here's the stack trace from the browser console:
    Code (csharp):
    1. Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 268435456, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0
    2.  
    3. uncaught exception: abort("Cannot enlarge memory arrays. Either (1) compile with  -s TOTAL_MEMORY=X  with X higher than the current value 268435456, (2) compile with  -s ALLOW_MEMORY_GROWTH=1  which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with  -s ABORTING_MALLOC=0 ") at jsStackTrace@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:1154:12
    4. stackTrace@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:1168:11
    5. abort@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:19862:43
    6. abortOnCannotGrowMemory@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:1210:2
    7. enlargeMemory@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:1213:2
    8. _sbrk@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1613815:1
    9. _GC_unix_sbrk_get_mem@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1661116:1
    10. _GC_expand_hp_inner@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1140644:1
    11. _GC_collect_or_expand@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1059476:1
    12. _GC_alloc_large@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1444558:1
    13. _GC_generic_malloc@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1398371:1
    14. _GC_malloc_atomic@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1593790:1
    15. __ZN6il2cpp2vm6Object15AllocatePtrFreeEjP11Il2CppClass [il2cpp::vm::Object::AllocatePtrFree(unsigned int, Il2CppClass*)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1568980:1
    16. __ZN6il2cpp2vm5Array11NewSpecificEP11Il2CppClassj [il2cpp::vm::Array::NewSpecific(Il2CppClass*, unsigned int)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1234718:1
    17. __Z10SZArrayNewP11Il2CppClassj [SZArrayNew(Il2CppClass*, unsigned int)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1749523:1
    18. _BufferUtils_RentBuffer_m2229979349@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1598652:1
    19. _JsonTextReader_EnsureBuffer_m4043226489@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1687525:1
    20. _JsonTextReader_Read_m2461759731@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:848925:1
    21. dynCall_iii@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1789585:1
    22. invoke_iii@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:16480:10
    23. _APIData_PopulateFromJSON_m2325856188@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:231577:1
    24. _AirConsoleAgent_ProcessData_m3389771770@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:688831:1
    25. _VolplaneController_ProcessData_m1038396144@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1730861:1
    26. __Z45RuntimeInvoker_Void_t1185182177_RuntimeObjectPFvvEPK10MethodInfoPvPS4_ [RuntimeInvoker_Void_t1185182177_RuntimeObject(void (*)(), MethodInfo const*, void*, void**)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1725810:1
    27. dynCall_iiiii@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1752953:1
    28. invoke_iiiii@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:16048:10
    29. __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException [il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1424762:1
    30. _il2cpp_runtime_invoke@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1668541:1
    31. __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb [scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1534593:1
    32. __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb [ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1042699:1
    33. __ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedE18ScriptingMethodPtr18ScriptingObjectPtrP21ScriptingExceptionPtr [MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingExceptionPtr*)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:260511:1
    34. __ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedE18ScriptingMethodPtr18ScriptingObjectPtr [MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr, ScriptingObjectPtr)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1013842:1
    35. __ZN9Scripting20SendScriptingMessageER10GameObjectPKc18ScriptingObjectPtr [Scripting::SendScriptingMessage(GameObject&, char const*, ScriptingObjectPtr)]@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:982724:1
    36. _SendMessageString@blob:http://localhost:7860/21dcbdcf-0f46-4247-b4de-cbb54b245cbb:1370316:1
    37. UnityLoader["8b0b7c8bcbfc7a9b0673754cb4c9e96b"]/asm._SendMessageString@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:19199:9
    38. ccallFunc@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:740:13
    39. SendMessage@blob:http://localhost:7860/4bef3bd2-f4fe-43f7-b36f-ad3fbebaa529:343:144
    40. SendMessage@http://localhost:7860/build/Build/UnityLoader.js:275:18
    41. Agent.prototype.sendToUnity@http://localhost:7860/build/javascript/unity-airconsole-agent.js:353:17
    42.  
    (The VolplaneController / AirConsoleAgent objects don't do anything here but calling the method APIData.PopulateFromJSON() which reads the json)

    What is the best way of deserializing incoming json data packets that operates as memory friendly as possible? The data will sometimes be sent multiple times per second and control of this behavior is out of my hands. So there is no way for me of changing this frequency.
    Is it better to just use the deserializing method?
    I am using Unity 2017.3.1f1 and the latest JSON.NET version on the asset store.

    Some insights are greatly appreciated!
    Julian
     
  24. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,478
    I'm not sure if there will be a more efficient way. You may have to keep track of the number of times you've called this method and manually call a GC.Collect(true) every so many iterations to get the heap cleaned up. WebGL isn't as good at managing memory. I would start, though, by following those instructions at the top of the exception you're getting and play around with the memory values to see if you can find a larger one that works for you.