Search Unity

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,568
    lol, "TypeNameHandling.All" stores the type name in the JSON along with the other data so that when it deserializes it knows what kind of actual object to construct.
     
  2. Domino-Studios

    Domino-Studios

    Joined:
    Oct 26, 2012
    Posts:
    31
    thx for the explanations :)
     
  3. MisterCollision

    MisterCollision

    Joined:
    Oct 1, 2013
    Posts:
    1
    Do you have an example for Loading a *.json file (would it be in Resources?) and then parsing it for use as a dictionary of values? Thanks!
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I don't but I could probably write one up. It's a bit outside of the scope of the asset in that it's not limited to data stored in files on disk (commonly used with network transmission). Loading the file may differ between platforms. It's not possible to load a file off of disk with webplayer and the other platforms would use Application.persistentDataPath. The file would just be loaded with System.IO.File to get a filestream and you can use ReadAllText to get the json data from the file, then just deserialize it as normal. For example, if you had a Dictionary<string, int> you would deserialize it as:

    Code (csharp):
    1.  
    2. var dictValues = JsonConvert.DeserializeObject<Dictionary<string, int>(jsonString);
    3.  
    ...where jsonString is the text you read from your file.
     
  5. noanoa

    noanoa

    Joined:
    Apr 17, 2014
    Posts:
    225
    Can you use custom type name with TypeNameHandling introduced here?
    http://james.newtonking.com/archive/2011/11/19/json-net-4-0-release-4-bug-fixes

    I want to make type names shorter but when I make the above TypeNameSerializationBinder class, it gives me an error saying
    error CS0115: `TypeNameSerializationBinder.BindToName(System.Type, out string, out string)' is marked as an override but no suitable method found to override

    Is it not supported in c#2.0?
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Unfortunately not. BindToName is an override from the base SerializationBinder class. This is a .NET 4 and above feature. If you were targeting Windows Phone or Windows Store then you'd actually be able to use it since they utilize .NET 4.5 but you wouldn't be able to verify it in the editor.
     
  7. noanoa

    noanoa

    Joined:
    Apr 17, 2014
    Posts:
    225
    I see, that's unfortunate indeed. Thanks for the reply!
     
  8. FelixW

    FelixW

    Joined:
    Jan 7, 2014
    Posts:
    4
    Hi, still having the Color serialization issue described in post #341 - we're using v 1.4.0
     
    Last edited: Oct 16, 2014
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'm going to take a look this evening. What is the actual error message you're seeing?
     
  10. anfemaPeter

    anfemaPeter

    Joined:
    May 29, 2013
    Posts:
    14
    I'm facing an issue in Unity 5.0.0b9 + WebGL Export + JsonDotNet v 1.4.0:

    Reading json in the Editor / iOS / Webplayer works perfectly fine - but when I export the project to WebGL - I get this error:
    JsonSerializationException: Unexpected token when deserializing multidimensional array: StartObject

    I tried appending the UNITY_WEBGL precompiler flag when UNITY_WEBPLAYER is used but that doesn't seem to fix the issue.

    I know this is not yet supported - but it would be really nice if there is a way to get this working. :)
     
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I am supporting Unity 5 beta, however not yet WebGL. WebGL itself is very early and there are a lot of things that aren't working with it yet. IL2CPP is still in development and right now there's no way to have JSON .NET working with WebGL. I am watching it closely though and retesting with each beta release so I'll let you know when it's functional at least to some degree.

    On that note, I also have a couple of things going on in parallel. I have a couple of bug fixes that I'm working on for a dot release (will be 1.5.0). These are primarily one offs so don't affect most people but I'm trying to get them in as quickly as possible.

    The second project, now that Unity 5 is getting into more hands, is to update the entire asset to use JSON .NET version 6 which has a lot of bug fixes and some new features. It should be very exciting! This will be a 2.0 release and not a new asset so everyone who previously purchased will be able to update for free.
     
  12. anfemaPeter

    anfemaPeter

    Joined:
    May 29, 2013
    Posts:
    14
    Thx for your answer, Dustin!
    These are great news and I'm really looking forward to the next releases :)
     
    Dustin-Horne likes this.
  13. jinjer

    jinjer

    Joined:
    Oct 12, 2014
    Posts:
    3
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That looks like it's caused by stripping. / corelib. Because of differences with iOS and the AOT compiler I can't support micro mscorlib. I don't recall what the exact issue is, but I think it had to do with a combination of stripping level and having Use Micro Mscorlib selected. What do you have set for those settings?
     
  15. tkoknordic

    tkoknordic

    Joined:
    Jan 31, 2013
    Posts:
    93
    Hi,I just purchased your Json converter. Is there any tutorials or examples of how to use those Vector3Converter etc. special converters? I have my class that have a public field of Vector3 and the json string comes out with all those magnitude and sqrmagnitudes in it. How do I use a special converters?
     
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    There are some in the official documentation. The easiest way is to just use the attribute on your property. So:

    Code (csharp):
    1.  
    2.  
    3. [JsonConverter(typeof(Vector3Converter))]
    4. public Vector3 MyVector { get; set; }
    5.  
    6.  
     
  17. tkoknordic

    tkoknordic

    Joined:
    Jan 31, 2013
    Posts:
    93
    I still got the result with magnitudes. There are the "Keys" now but no more chances:
    Code (csharp):
    1. {"name":"Vechiles","year":"1","month":"1","week":"4","nameOfBackGround":"week4","objects":[{"spriteName":"taxi","name":"taxi","position":{"Keys":["x","y"],"x":-1.5099999904632568,"y":-2.3350000381469727,"normalized":{"x":-0.54302757978439331,"y":-0.83971482515335083,"normalized":{"x":-0.54302763938903809,"y":-0.83971488475799561,"magnitude":1.0,"sqrMagnitude":1.0000001192092896},"magnitude":0.99999994039535522,"sqrMagnitude":0.99999994039535522},"magnitude":2.7807059288024902,"sqrMagnitude":7.7323250770568848}}]}
    My code:
    Code (csharp):
    1.  
    2. //In serializer code
    3. jsonData = JsonConvert.SerializeObject(_go.excercise);
    4.  
    5. //Serialized classes:
    6.  
    7. [System.Serializable]
    8. public class Excercise {
    9.     public string name;
    10.     public string year;
    11.     public string month;
    12.     public string week;
    13.     public string nameOfBackGround;
    14.     public List<ExcerciseObject> objects;
    15. }
    16.  
    17. [System.Serializable]
    18. public class ExcerciseObject
    19. {
    20.     public string spriteName;
    21.     public string name;
    22.     [JsonConverter(typeof(Vector2Converter))]
    23.     public Vector2 position;
    24. }
    25.  
    Any tips, what I should try next?
     
  18. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    oh, that's vector2. I'll take a look at that converter and see if I made a typo but it should work. I'm out at the moment so it'll be another 12:hrs or so before I can take a look at it.
     
  19. tkoknordic

    tkoknordic

    Joined:
    Jan 31, 2013
    Posts:
    93
    The same thing happens with Vector3:
    Code (csharp):
    1.  
    2.     [JsonConverter(typeof(Vector3Converter))]
    3.     public Vector3 position;
    4.  
    5. "position":{"Keys":["x","y","z"],"x":-1.5099999904632568,"y":-2.3350000381469727,"z":0.0,"normalized":{"x":-0.54302757978439331,"y":-0.83971482515335083,"z":0.0,"normalized":{"x":-0.54302763938903809,"y":-0.83971488475799561,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0000001192092896},"magnitude":0.99999994039535522,"sqrMagnitude":0.99999994039535522},"magnitude":2.7807059288024902,"sqrMagnitude":7.7323250770568848}
    EDIT1: I currently use the Unity 5 beta.
     
    Last edited: Oct 30, 2014
  20. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'll certainly take a look. The only thing I can think is that since it's a property of an object in a list that for some reason the converter is not being used so I'll have to debug it and see what's going on. When I built those converters I tested them on Unity 3.5 up through Unity 5 Beta, though I haven't tested it in the newest released and didn't test it within the context of a collection so I'll see what's going on with it.
     
  21. Krasi

    Krasi

    Joined:
    Dec 29, 2013
    Posts:
    4
    I have problem when deserialize string to array of objects.
    When I try to deserialize:
    {
    "road" : "[{\"x": "40\",\"y\": \"175\"},{\"x\": \"360\",\"y\": \"175\"}]"​
    }​
    I receive exception.
    When try to deserialize:
    "[{\"x": "40\",\"y\": \"175\"},{\"x\": \"360\",\"y\": \"175\"}]"
    everything is ok.
    I solve my problem with this solution, but I think that this behaviour can be changed.
     
    Last edited: Nov 10, 2014
  22. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    your first string is not an array if objects. It represents an object with a property called roads that is an array of objects. You would need:

    Code (csharp):
    1.  
    2. public class SomeObject
    3. {
    4. [JsonPropery(name="road")]
    5. public Vector2[] Road { get; set; }
    6. }
    7.  
    The JsonProperty attribute isn't required, I just used it to demonstrate how you could have a lower case property in your json and have it upper case in your class. Also, replace Vector2[] with whatever your actual array or list type is.
     
  23. Krasi

    Krasi

    Joined:
    Dec 29, 2013
    Posts:
    4
    Thank you for that answer. I should apologize because I didn't explain very well. I will give more details now.
    We build API that returns serialized information from database. Actually information is more complicated and everything except de-serialize of string to array of object is working well.
    So I have the next json:
    {
    "status": "success",
    "data": {
    ..............................................................................

    "id": "14",
    "road": "[{\"x\": \"40\",\"y\": \"175\"},{\"x\": \"360\",\"y\": \"175\"},{\"x\": \"360\",\"y\": \"325\"},{\"x\": \"520\",\"y\": \"325\"},{\"x\": \"520\",\"y\": \"75\"},{\"x\": \"680\",\"y\": \"75\"},{\"x\": \"680\",\"y\": \"275\"}]",
    ........................................................

    }
    }
    I have the next classes:
    Code (CSharp):
    1.  
    2. public class Response
    3.     {
    4.         [JsonProperty("status")]
    5.         public string Status { get; set; }
    6.  
    7.         [JsonProperty("data")]
    8.         public Data Data { get; set; }
    9.  
    10.     }
    Code (CSharp):
    1.  
    2.  
    3.     public class Data
    4.     {
    5.         [JsonProperty("id")]
    6.         public int Id { get; set; }
    7.  
    8.         [JsonProperty("road")]  
    9.         public Vector2 Road { get; set; }
    10.  
    11.     }
    This is my de-serialize code:
    Code (CSharp):
    1.  
    2.                     Response responseInfo =
    3.                         JsonConvert.DeserializeObject<Response>(response.DataAsText);
    4.  
    And here I receive exception.
    I solve the problem with little modification:
    Code (CSharp):
    1.  
    2.  
    3.     public class Data
    4.     {
    5.         [JsonProperty("id")]
    6.         public int Id { get; set; }
    7.  
    8.         [JsonProperty("road")]  
    9.         public string RoadString { get; set; }
    10.  
    11. public string Road { get; set; }
    12.     }
    Code (CSharp):
    1.  
    2. Response responseInfo =
    3.                   JsonConvert.DeserializeObject<Response>(response.DataAsText);
    4. responseInfo.Data.Road = JsonConvert.DeserializeObject<Vector2[]>(responseInfo.Data.RoadString);
    5.  
    Problem for me is that I should do 2 times de-serialize.
     
  24. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Your Data class is wrong. Your JSON contains an array of Vector2's, so the Road property of your Data class should either be:
    Vector2[] or List<Vector2>
     
  25. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
  26. Xylph

    Xylph

    Joined:
    Dec 11, 2013
    Posts:
    30
    Hi, I have a problem when simulating/building it on Web Player.


    Code (CSharp):
    1. StackOverflowException: The requested operation caused a stack overflow.
    2. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    3. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    4. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    5. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    6. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    7. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    8. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    9. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    10. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    11. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    12. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    13. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    14. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    15. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    16. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    17. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    18. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    19. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    20. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    21. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    22. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    23. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    24. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    25. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    26. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    27. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    28. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    29. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    30. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    31. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    32. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    33. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    34. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    35. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    36. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    37. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    38. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    39. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    40. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    41. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    42. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    43. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    44. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    45. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    46. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    47. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    48. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    49. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    50. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    51. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    52. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    53. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    54. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    55. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    56. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    57. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    58. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    59. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    60. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    61. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    62. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    63. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    64. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    65. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    66. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    67. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    68. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    69. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    70. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    71. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    72. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    73. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    74. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    75. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    76. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    77. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    78. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    79. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    80. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    81. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    82. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    83. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    84. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    85. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs:107)
    86. Newtonsoft.Json.Linq.JObject+JPropertKeyedCollection.InsertItem (Int32 index, Newtonsoft.Json.Linq.JToken item) (at Assets/Externals/JsonDotNet/Source/Linq/JObject.cs:81)
    87. System.Collections.ObjectModel.Collection`1[T].Insert (Int32 index, .T item) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.
    It works perfectly when built on PC. This bug happens when I try to serialize an object (with child objects) and copy it over to a new object through deserialization. I'm doing the serialization code manually via JToken so I have full control.

    EDIT:
    With a few debugging, it crashes immediately on this simple code.

    Code (CSharp):
    1. JObject json = new JObject();
    2. json["go_name"] = go.name;
     
    Last edited: Nov 12, 2014
  27. AndyGFX

    AndyGFX

    Joined:
    Jan 13, 2012
    Posts:
    98
    Hi,

    any news about magnitude and sqrMagnitude properties serialized from Vector3 and Vector2 with Unitu 4.5+ ?

    Some-time I see in JSON file, other strange structure, where same Vector3 field looks like this:

    Code (CSharp):
    1.    "worldXYZ": {
    2.         "x": 0.0,
    3.         "y": 0.0,
    4.         "z": 0.0,
    5.         "magnitude": 0.0,
    6.         "sqrMagnitude": 0.0
    7.       },
    8.  
    or

    Code (CSharp):
    1.       "worldXYZ": {
    2.         "x": 20.0,
    3.         "y": 0.0,
    4.         "z": 0.0,
    5.         "normalized": {
    6.           "x": 1.0,
    7.           "y": 0.0,
    8.           "z": 0.0,
    9.           "magnitude": 1.0,
    10.           "sqrMagnitude": 1.0
    11.         },
    12.         "magnitude": 20.0,
    13.         "sqrMagnitude": 400.0
    14.       },
    and because this is clean game export (default), I don't understand, why same class Vector3 field, has different output structure.
     
  28. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I think I know where this problem is so I'll add it to my list. WebPlayer has some implementation differences with KeyedCollection that I had to work around.
    This one is next on my list. There are a couple possible causes and one might be related to what I'm working on now. I'm adding in ItemTypeConverter to work with collections. It's possible that for some reason the Converter is being ignored because the class is included in a collection (though it shouldn't be).

    The other possible cause is the inclusion of the System.Serializable attribute. It's very strange that you're getting different results here. It's almost like you're not actually using Unity's Vector3 type but actually using another Vector3 type that mirrors it.

    The biggest reason I say this is because I see the "normalized" property being included. This property should always be ignored by JSON .NET because it returns another vector, which in and of itself would have another Normalized property, so, while it's not a circular reference, it would result in a stack overflow because it would be endlessly serializing that Normalized property.
     
  29. AndyGFX

    AndyGFX

    Joined:
    Jan 13, 2012
    Posts:
    98
    This point looks real, because I have used in project NGUI, and this asset has extended Vector3 class for reset values from Inspector, but this is only idea and not tested.
     
  30. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That's a possibility... One thing to try... I don't have access to my source code at the moment, but in the Vector3 (and Vector2) converters there is a CanConvert method that probably looks like this:

    Code (csharp):
    1.  
    2. public override bool CanConvert(Type objectType)
    3. {
    4. return objectType == typeof (Vector3);
    5. }
    6.  
    You could try changing it to:
    Code (csharp):
    1.  
    2. public override bool CanConvert(Type objectType)
    3. {
    4. return typeof(Vector3).IsAssignableFrom(objectType);
    5. }
    6.  
    Instead of a direct comparison, this will check to see if Vector3 is in the inheritance chain for objectType (meaning that objectType or one of its parents inherit from Vector3). I didn't do this because there may be times when you want to create custom classes that inherit from Vector3 and have their properties included... so if this works you'll probably want to just copy the Vector3 converter... change it back to == in the original, and make the new one use the NGUI Vector3 type (assuming that's what the problem is).

    This is actually the second time I've seen this issue come up with Vector3 but the first time it was in a Dictionary and I wasn't able to repro it and the user fixed it themselves by implementing custom serialization with Serializable rather than using the JsonConverter.
     
  31. AndyGFX

    AndyGFX

    Joined:
    Jan 13, 2012
    Posts:
    98
    Hi,
    tested now, but with same result.

    My second test was, check your exaples in clean Unity project without NGUI and I see same "wrong" result from your script too [script: JsonTestScript ]

    Code (CSharp):
    1.  
    2. #### Serialized Object: {"x":2.0,"y":4.0,"z":6.0,"normalized":{"x":0.267261237,"y":0.5345225,"z":0.8017837,"normalized":{"x":0.267261267,"y":0.534522533,"z":0.801783741,"magnitude":1.0,"sqrMagnitude":1.00000012},"magnitude":0.99999994,"sqrMagnitude":0.99999994},"magnitude":7.483315,"sqrMagnitude":56.0}
    3.  
     
  32. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That result is different still though. :/ Multiple nesting of normalized. Ok, I'll have access to it again when I get home this evening and I'm going to dig into it deeper because now I'm not so sure the issue is what I thought it was. Out of curiosity, what platform(s) are you targeting?
     
  33. AndyGFX

    AndyGFX

    Joined:
    Jan 13, 2012
    Posts:
    98
    For now I'm on windows platform only (tried under Win7 x32 and Vista x64).
    Finished project will beon Android, iPhone, PC, MAC, WebPlayer.

    Thank for your support time ;)

    Best reagrds,
    AndyGFX
     
  34. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Ok good, Platform won't make a difference in this case... or shouldn't, but I wanted to be sure that I'm setting up my reproduction with the same parameters.
     
  35. Xylph

    Xylph

    Joined:
    Dec 11, 2013
    Posts:
    30
    Thanks for the quick reply. Can you give us when will you release the fix?

    If it'll take long, I think I need to change my code to something that will wok for web. I plan to release the build by early December. I guess tagging my fields with json attributes/properties is the best alternative?
     
  36. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    tagging the lm with jsonproperty attributes is the best but I hate for you to have to change your code. Give me a couple of days.
     
  37. Xylph

    Xylph

    Joined:
    Dec 11, 2013
    Posts:
    30
    I guess I can wait then. I still have other stuff to code anyway. Thanks!
     
  38. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    @AndyGFX -

    I've got something for you to try here. I think the way I understood the converter working was not what it was actually doing. I rewrote it in Notepad++ as I don't have access to my codebase here at the office, but could you give this converter a try? Just replace the code in the Vector3Converter with it. If this works I'll rewrite the Vector2 and Matrix4x4 Converters as well:

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6. using Newtonsoft.Json;
    7. using Newtonsoft.Json.Linq;
    8. using UnityEngine;
    9. namespace JsonDotNet.Extras.CustomConverters
    10. {
    11.  public class Vector3Converter : JsonConverter
    12.  {
    13.  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    14.  {
    15.  var obj = (Vector3)value;
    16.  writer.WriteStartObject();
    17.  writer.WritePropertyName("x");
    18.  serializer.Serialize(writer, obj.x);
    19.  writer.WritePropertyName("y");
    20.  serializer.Serialize(writer, obj.y);
    21.  writer.WritePropertyName("z");
    22.  serializer.Serialize(writer, obj.z);
    23.  writer.WriteEndObject();
    24.  }
    25.  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    26.  {
    27.  throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
    28.  }
    29.  public override bool CanRead
    30.  {
    31.  get { return false; }
    32.  }
    33.  public override bool CanConvert(Type objectType)
    34.  {
    35.  return objectType == typeof(Vector3);
    36.  }
    37.  }
    38. }
    39.  
     
  39. Krasi

    Krasi

    Joined:
    Dec 29, 2013
    Posts:
    4
    Actually it is Vector[] in my source. I make mistake only here in unity forum.
    I will try to give you simple example.
    When de-serialize "[[1,2,5,3],[3,5,7]]" this to array everything is ok.
    When de-serialize
    "{
    'array' : "[[1,2,5,3],[3,5,7]]"
    }"
    to object with property value array it throw exception.
     
  40. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    what is the exception you receive?
     
  41. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'm finishing up some bug fixes that I've been working on the last couple of weeks, including those listed above. I wanted to add an additional note. I don't currently support WebGL as it is beta, Xbox One, PS4, Ouya or Blackberry as I don't have a way to test. However, a customer reached out to me about Xbox One and I was able to suggest a change and I'm happy to report that it worked.

    Unofficial Xbox One support will be upcoming in the next release as well and possibly PS4. If there are any users currently doing PS4 development or users doing Xbox One that need instructions on what to change to make the current release work drop me an email, contact me through my blog, or drop me a PM here and I can send those over.
     
  42. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Hi Dustin,

    Thanks for the asset. I'm using it to provide modding functionality in my game, and it's working well so far.

    In the tutorial you've written which is linked from the asset store page, you state that enum's are serialized to their string names by default. However, when I try serializing an enum, it saves it to an int. Did the default setting change in the course of updates to the asset?
     
  43. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks Korindian. I need to fix that blog post. It actually serializes to int by default unless it is used as a dictionary key. However, if you want to serialize to a string, there is a StringEnumConverter that is included with json .net that will handle treating the enum values as strings.
     
  44. rejwan1

    rejwan1

    Joined:
    Jul 3, 2012
    Posts:
    40
    Hey,

    We're having a very strange issue with the JSON serializer trying to serialize an object with a simple "string[]" array on iOS - when we try to do this we'll get the following exception (with a game crash):
    We're running on Unity 4.2.2f and unfortunately can't upgrade our Unity at the moment due to legacy problems.

    I've read this thread, but I don't see how I can use the suggestions there to fix this issue.

    Thanks
     
  45. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    what version of json .net for unity are you using? This was caused by a unity bug that they fixed in 4.5 but I have a workaround in there that should work around it.
     
  46. rejwan1

    rejwan1

    Joined:
    Jul 3, 2012
    Posts:
    40
    Hey Dustin,

    We use the latest version 1.4.0 - btw, changing the string[] from a property to a member fixed this issue for us
     
  47. Krasi

    Krasi

    Joined:
    Dec 29, 2013
    Posts:
    4
    This is exception.

    Error converting value "[{"x": "-1","y": "7"},{"x": "7","y": "7"},{"x": "4","y": "11"},{"x": "9","y": "11"},{"x": "9","y": "2"},{"x": "15","y": "2"},{"x": "11","y": "7"},{"x": "16","y": "7"}]" to type 'UnityEngine.Vector2[]'.: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (System.Object value, System.Globalization.CultureInfo culture, System.Type targetType) [0x0002a] in D:\Projects\TowerAttack\Assets\JsonDotNet\Source\Serialization\JsonSerializerInternalReader.cs:495
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue) [0x000e5] in D:\Projects\TowerAttack\Assets\JsonDotNet\Source\Serialization\JsonSerializerInternalReader.cs:245
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueProperty (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonProperty property, System.Object target, Boolean gottenCurrentValue, System.Object currentValue) [0x0006c] in D:\Projects\TowerAttack\Assets\JsonDotNet\Source\Serialization\JsonSerializerInternalReader.cs:200
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonReader reader, System.Object target) [0x00162] in D:\Projects\TowerAttack\Assets\JsonDotNet\Source\Serialization\JsonSerializerInternalReader.cs:564
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, System.String id) [0x00148] in D:\Projects\TowerAttack\Assets\JsonDotNet\Source\Serialization\JsonSerializerInternalReader.cs:1090
    UnityEngine.Debug:LogError(Object)
    BestHTTP.HTTPRequest:CallCallback() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPRequest.cs:753)
    BestHTTP.HTTPConnection:HandleCallback() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPConnection.cs:499)
    BestHTTP.HTTPManager:OnUpdate() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPManager.cs:294)
    BestHTTP.HTTPUpdateDelegator:LateUpdate() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPUpdateDelegator.cs:49)
     
  48. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Hmm... I'm going to try to repro this but I have an idea. I wonder if it doesn't know how to convert strings to floats. Where you have: {"x":"15", "y":"2"} probably should be {"x":15, "y":2} etc.
     
  49. layola

    layola

    Joined:
    Aug 6, 2013
    Posts:
    94
    please add utf-8 support ,current version not suppot utf8 encode.
     
  50. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    what are you missing? Utf8 works just fine. Only time you'll have trouble is if you encode it with the BOM (byte order mark) at the start of your string.