Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

JSON .NET for Unity

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

  1. wkwan

    wkwan

    Joined:
    Sep 14, 2014
    Posts:
    27
    I'm having an issue serializing a 2d array. Just following the example on the Json.net website

    string[,] famousCouples = new string[,]
    {
    { "Adam", "Eve" },
    { "Bonnie", "Clyde" },
    { "Donald", "Daisy" },
    { "Han", "Leia" }
    };

    string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);

    I get

    NullReferenceException: Object reference not set to an instance of an object
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeMultidimensionalArray (Newtonsoft.Json.JsonWriter writer, System.Array values, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionContract) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:515)
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:151)
    Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) (at Assets/JsonDotNet/Source/Serialization/JsonSerializerInternalWriter.cs:72)
    Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) (at Assets/JsonDotNet/Source/JsonSerializer.cs:454)
    Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) (at Assets/JsonDotNet/Source/JsonSerializer.cs:446)
    Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings) (at Assets/JsonDotNet/Source/JsonConvert.cs:626)
    Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting) (at Assets/JsonDotNet/Source/JsonConvert.cs:576)
    LevelEditor.Start () (at Assets/_Scripts/LevelEditor.cs:58)

    Is this not supported in the Unity version of Json.net?
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    It should be... what version are you using? There was a null reference bug in the multidimensional array but I thought I had fixed it... I'll have to take a look. Also, what platform are you targeting?
     
  3. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Not sure if this was asked earlier, but is there a simple way to deserialize to a ScriptableObject? I get a message in the console that my object must be instantiated using the ScriptableObject.CreateInstance method instead.

    I'm able to instantiate the ScriptableObject using CreateInstance and I can then successfully serialize it out to a file. But trying to deserialize from the file back to the same instance gives me the above error.

    I did a quick search and someone mentioned deserializing to a JObject first and then copying the values over manually. Is that the only option? If so, if I have a lot of different ScriptableObjects, I'll have to forgo ScriptableObjects and just use regular classes.

    Thanks.
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You could instantiate the scriptable object first and then use JsonConvert.PopulateObject which will populate your existing object with the values from the json instead of creating a new one.

    After the unity 5 release and everything is stable in IL2CPP I'm going to be updating the port to a new version of json .net which will add another option called a custom creation converter which will allow you to dictate how the serializer instantiates your object.
     
  5. ColossalObelisk

    ColossalObelisk

    Joined:
    Oct 24, 2014
    Posts:
    3
    This is a great tool. Are there any plans to upgrade to the latest releases of Json.NET? Specifically, I want to use the new JObject Merge() function. Thanks!
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    There are. I'll be moving to the v6 series soon. I'm working on il2cpp compatibility right now so it will work with webgl and more importantly 64bit IOS. Once those are finalized and Unity 5 is ready for RTM or at least release candidate stage I'll start working on updating (its a pretty large undertaking).
     
  7. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Just reporting that JsonConvert.PopulateObject seems to do the job with populating an existing ScriptableObject with values.

    Thanks!
     
  8. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Hi Justin, I discovered an issue with this. This convertor does not handle nullable enums properly. I keep getting an enumType error when deserializing.

    I looked at the JSON default StringEnumConvertor, it has some strange nullable handling I can'tt quite understand what's trying to do. You have some insight?

     
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Hey Robin, which converter are you referring to? Or is it when you try to serialize / deserialize without using a converter? I haven't tried it with a nullable enum. If you could let me know which converter (if any) you are using, I could update it to work with the Nullables.
     
  10. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Oh that converter was a long time ago.. I had forgotten :). I'll see if i can update it or create another version to handle nullable enums
     
    imtrobin likes this.
  12. Xaurrien

    Xaurrien

    Joined:
    Jul 2, 2012
    Posts:
    20
    Hi,

    Did anyone tried to export to WebGL the test scene with the Unity 5 PreOrder beta 19 or 20?

    The Unity 5 Beta 19 and 20 release notes say:
    But when I try to run the WebGL export of the test scene I got this exception:
    Perhaps I am missing something in the PlayerSettings ?

    Edit: I am using the Unity 5 PreOrder beta 20
     
  13. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Send me a pm with your invoice number and email. I have a webgl update that adds the defines needed.
     
  14. Evgeny-Eliseev

    Evgeny-Eliseev

    Joined:
    Jan 21, 2015
    Posts:
    19
    Test scene WebGL export is works fine (after I replaced UNITY_IPHONE to UNITY_WEBGL) in Unity b20.

    But I stuck at next simple fragment that I can't serialize:

    int[] v = new int[] { 1, 2 };
    var serialized = JsonConvert.SerializeObject(v);

    I think there is endless loop or something else

    In logs I have only:
    "Not implemented: Il2CppTypeCompare::compare"

    Dustin Horne, is your WebGL update will fix this?
     
    Last edited: Jan 21, 2015
  15. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Not positive. That's why I haven't released the webgl update yet as its still being tested and bugs in il2cpp are being fixed. Try it with a List<int> but pm me your email address and invoice # and ill send the update.
     
    Last edited: Jan 21, 2015
    Evgeny-Eliseev likes this.
  16. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    There are compile errors in the latest version when defining JSONNET_XMLDISABLE in the editor in files JsonConvert.cs and DateTimeUtils.cs. Easily fixed by changing
    Code (csharp):
    1. #if !JSONNET_XMLDISABLE
    2. using System.Xml;
    3. #endif
    to
    Code (csharp):
    1.  
    2. #if !JSONNET_XMLDISABLE || UNITY_EDITOR
    3. using System.Xml;
    4. #endif
    5.  
    Just thought I'd let you know ;)
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Hmm I'll take a look. What platform and .NET (full or subset) were you targeting? The JSONNET_XMLDISABLE define was put in place as a client request so they could disable importing of XML for iOS and Android. I hadn't tested it across platforms which is why it wasn't ever documented... I'd like to take a look and see where the error is being thrown for sure.
     
  18. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    .NET Full and targeting Android. The error comes from further down in the files where the following code is, which allows the code to compile in the editor regardless if XML is disabled or not.
    Code (csharp):
    1. #if !(JSONNET_XMLDISABLE || (UNITY_WP8 || UNITY_WP_8_1) || UNITY_WINRT) || UNITY_EDITOR
     
  19. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Ahh that's where the root of the problem lies. That got changed a bit when adding WinRT and Universal support. I'll get it fixed for future versions. That line should actually I think be the following (but I need to look at it better and simplify the condition if possible):

    Code (csharp):
    1.  
    2. #if !JSONNET_XMLDISABLE && !(((UNITY_WP8 || UNITY_WP_8_1) || UNITY_WINRT) || UNITY_EDITOR)
    3.  
     
  20. Rom-

    Rom-

    Joined:
    Nov 26, 2008
    Posts:
    89
    I'm looking for an example of reading in a bson file. I've tried this with no success:

    Code (CSharp):
    1.             using (FileStream F = File.OpenRead(filename))
    2.             {
    3.                 using (BsonReader reader = new BsonReader(F))
    4.                 {
    5.                     try
    6.                     {
    7.                         JsonSerializer serializer = new JsonSerializer();
    8.                         spLocations = serializer.Deserialize<ShiftPlanningLocations>(reader);
    9.                     }
    10.                     catch (Exception ex)
    11.                     {
    12.                         Debug.Log(ex.Message);
    13.                     }
    14.                 }
    15.             }
    16.  
     
    Last edited: Jan 23, 2015
  21. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks for toning down your post. Are you getting an exception?
     
  22. Rom-

    Rom-

    Joined:
    Nov 26, 2008
    Posts:
    89
    Yes:

    Unexpected end when deserializing object.

    In JsonSerializerInternalReader.cs at this function:


    Code (CSharp):
    1.         private void CheckedRead(JsonReader reader)
    2.         {
    3.             if (!reader.Read())
    4.                 throw new JsonSerializationException("Unexpected end when deserializing object.");
    5.         }
    6.  
     
  23. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    How was the bson file created? Was it created with json .net? My email address is in the readme.txt file. Could you send me (or pm me) a repro project? I'll need to have the bson file and the object you're desrerializing to. It sounds like there is something wrong with the bson in your file so it would be good to see how you are creating the bson file. I'll also need you to send me your unity version, json .net version (I'm assuming 1.4), target platform and your invoice number so I can verify purchase.
     
  24. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I was wondering does the Serialization work with MonoBehaviours, or do we need to write our own data wrapper class? Currently when I try to serialize a MonoBehaviour derived class I get an error saying I'm trying to access a MonoBehaviour component that is not on my GameObject. Is there any way to put a check in the Serializer to not access a Component of a MonoBehaviour if the GameObject does not have that componenet, or is that too specific?
     
  25. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    It does not work directly with MonoBehaviours out of the box for a variety of reasons. The biggest reason is that Unity overrides the equality operator for some objects. rigidbody is a good example. In your code if you say:

    Code (csharp):
    1.  
    2. if(gameObject.rigidbody != null)
    3. {
    4.     //do something with it
    5. }
    6.  
    When doing automatic serialization, Reflection is used to resolve the values. So, Json .NET is using reflection to get the value of rigidbody (among other properties). The real problem is that rigidbody is not really null... it is just pretending to be and is in an uninitialized state (like a placeholder). So, by reflecting the value, the serializer says "Yep, there's an object there, serialize it" and it attempts to do the serialization. Internally, Unity says "Wait, you haven't really added a rigidbody component so we're going to throw an exception.".

    The only way around that is to explicitly code for those properties. It would be difficult to include in the asset because different versions of Unity keep adding and/or deprecating objects so you'd end up with warnings, and also the asset would suddenly break with new versions.

    Now, you could write a custom JsonConverter to do the serialization for you and ignore those properties. I've written sample ones and included them with the asset to ignore certain properties for Matrix's and Vectors. Or you could do the null check and serialize them if necessary.

    GameObjects pose a similar issue as you can't just get a list of the components that are attached... you can get them with method calls but not with a property. So, you would actually need to get tricky... you could attach a component called SerializerHelper for instance, and that helper could have a property with a method call in the get method that gets all the components on the gameobject and returns them as a collection. You'd need something similar in the setter that reattached those components to the gameobject.

    Just as big of problems would start to occur during deserialization though. Some objects (Texture2D is a good example), can't just be instantiated without supplying specialized parameters (no parameterless constructor). You could serialize the Texture2D but you'd have to convert it's byte array to base64 or store it as binary and then use it to reconstruct the Texture2D. Or, you could use a helper once again and then ignore the Texture2D and let the helper re-establish it. Texture2D is just one example.

    So... by far your best approach is to either use a "proxy" class (basically a wrapper that wraps everything up for you and mirrors the properties) that you can serialize... or thinking about your structure and see if there is a way you can put everything you need to serialize for a gameobject into a single component (or reference it there) so you can use that component to just serialize the objects necessary to repopulate the object.
     
    Ben-BearFish likes this.
  26. SimonW

    SimonW

    Joined:
    Feb 20, 2012
    Posts:
    13
    Hi Dustin. I just tried this in the latest version and it doesn't seem to work. The result I get is this:

    {"Keys":["x","y","z"],"x":2.0,"y":4.0,"z":6.0,"normalized":{"x":0.26726123690605164,"y":0.53452247381210327,"z":0.80178368091583252,"normalized":{"x":0.267261266708374,"y":0.534522533416748,"z":0.80178374052047729,"magnitude":1.0,"sqrMagnitude":1.0000001192092896},"magnitude":0.99999994039535522,"sqrMagnitude":0.99999994039535522},"magnitude":7.4833149909973145,"sqrMagnitude":56.0}

    So I wrote my own vector serializer - it's a lot dumber, but it seems to do the job:

    Code (csharp):
    1.  
    2. public class VectorSerializer : JsonConverter
    3. {
    4.     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    5.     {
    6.         if (value.GetType() == typeof(Vector2))
    7.         {
    8.             var vec = (Vector2)value;
    9.  
    10.             writer.WriteStartObject();
    11.             writer.WritePropertyName("x");
    12.             writer.WriteValue(vec.x);
    13.             writer.WritePropertyName("y");
    14.             writer.WriteValue(vec.y);
    15.             writer.WriteEndObject();
    16.         }
    17.         else
    18.         {
    19.             var vec = (Vector3)value;
    20.  
    21.             writer.WriteStartObject();
    22.             writer.WritePropertyName("x");
    23.             writer.WriteValue(vec.x);
    24.             writer.WritePropertyName("y");
    25.             writer.WriteValue(vec.y);
    26.             writer.WritePropertyName("z");
    27.             writer.WriteValue(vec.z);
    28.             writer.WriteEndObject();
    29.         }
    30.     }
    31.  
    32.     public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    33.     {
    34.         var obj = JToken.Load(reader);
    35.         if (obj.Type != JTokenType.Object) return null;
    36.  
    37.         var vec = (JObject)obj;
    38.  
    39.         if (vec["z"] != null)
    40.         {
    41.             return new Vector3(vec["x"].Value<float>(), vec["y"].Value<float>(), vec["z"].Value<float>());
    42.         }
    43.  
    44.         return new Vector2(vec["x"].Value<float>(), vec["y"].Value<float>());
    45.     }
    46.  
    47.     public override bool CanConvert(Type objectType)
    48.     {
    49.         return objectType == typeof(Vector2) || objectType == typeof(Vector3);
    50.     }
    51. }
    52.  
    Is there some trick to the example ones that I'm missing?
     
  27. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Shouldn't be. Did you try it with the updated serializer that I posted? I'm working on a couple bug fixes now and getting that merged in with the WebGL directives and I'll also be updating those converters for the next release.
     
  28. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Oh, a couple of things... You don't have to override ReadJson, just WriteJson and override the CanRead property and return false. The serializer will deserialize just fine. It's only skipping those properties during serialization you have to worry about.
     
  29. SimonW

    SimonW

    Joined:
    Feb 20, 2012
    Posts:
    13
    Ah, I didn't read far enough back in the thread, sorry about that. I have a ReadJson function because this code is shared with the server that uses Json.NET to do the decoding on the other end.
     
  30. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    Hi,

    I just bought JSON.NET and have used it to convert my project. Super-convenient and easy to use. It works on the Web build, but I get a fatal error when the app runs on iOS. It failed on my project, and it also failed when I created a new project with nothing but the Examples>Tests>JsonTestScene you provide.

    It does build and run on iOS, but when I bring the Test scene up, the game crashes.

    The app successfully decoded the first couple of tests, Vector3, etc. But failed with this error in XCode:

    [SOLVED] Thanks for the quick reply via email. I had set to use 'micro mscorlib' and by changing that to Strip Byte Code it fixed this problem.

    Great asset, and great support!
     
    Last edited: Jan 28, 2015
  31. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Dustin, according to a post in another thread, you have a working version for WebGL with proper directives that works with Unity 5 Beta 20+. Do you plan to release those changes now, or do you wait until Unity 5 is stable?
    I know we can pm you to get it, but as I won't need it within the next few days, I would just wait if you plan to release now.
     
  32. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Dustin Horne, so I just updated to Unity 4.6.2, and I'm getting the "Serialization depth limit exceeded" errors as mentioned here:
    http://forum.unity3d.com/threads/4-5-serialization-depth.248321/

    I receive the errors every time I load Unity, or press play. In the forum it seems as though using [Serializable] is one of the culprits, but I'm not entirely sure. The Unity guy says this is related to Unity's Serialization, but I'm using your JSON plugin for Serialization, not Unity's so I'm a bit unsure how to remove these errors.
     
  33. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    It will be included in my next release which should be in the next couple weeks. IL2CPP is finally stabilizing so I'm comfortable releasing it. I'll have a couple other minor bug fixes in as well such as handling of UTC date strings.

    Those errors are not caused by the serialization you or my asset are doing. It is the editor serializing metadata properties for your monobehaviours and/or editor extensions. That serialization depth spamming seems to ebb and flow with different unity versions. I don't think there is anything you can do about it.
     
    Dantus likes this.
  34. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I need an advice!

    How does JSON.NET handles loading an save from a old class after I changed the class definition? I'm having problems with old save game after I changed the class from the save game. How the correct way of handling this?
     
  35. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That depends on what you changed... Could you show me the definition for the old class and the definition for the new class? I just need to see the code for the fields and properties, not any methods you may have... then I can see if I can advise on how to work around serialization.
     
  36. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I added a new field to it!

    Code (csharp):
    1.  
    2. public class GameStatistics
    3. {
    4.     public int LevelsCompleted = 0;
    5.     public float TimePlayed = 0;
    6.     public int TotalMatchs = 0;
    7.     public int TotalTokens = 0;
    8. }
    9.  
    10. public class SaveData
    11. {
    12.     public GameStatistics Statistics = new GameStatistics(); // I added this one
    13. }
    14.  
    But when I load the old save, Statistics is always NULL, even if I add a constructor to SaveData when I create a new Statistics there.

    Any suggestion?
     
  37. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Not sure... Could I see your json? In the meantime, try creating a new save data object and use JsonConvert.PopulateObject instead of DeserializeObject and see if that works.
     
  38. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    Hi,

    couldn't actually find a clear information in the post before.
    When is IL2CPP compatible version expected? We are kinda ready to publish, so it's important to know if we should wait or try a different serialization approach.

    Thanks
     
  39. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Webgl or IOS? The current version is ready for 64bit IOS. If you need webgl send me an email with your invoice number and I'll send you the webgl update. I just don't officially support it yet because it is still beta. It does work though.
     
  40. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    I meant iOS.
    Interesting, I'm having some errors. I'll look into it to make sure it's not some other plugin or anything else. If it persist I'll let you know.
     
  41. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Please send me the errors as well. Seems the latest 4.6.x did fix some things but my users do occasionally ser weirdness such as classes not having constructors even though they do and stripping is disabled. It seems that il2cpp does have some random reflection issues still bit I'm awaiting confirmation as to whether the latest beta fixed the problems.
     
  42. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    Does this mean that every serialized class should have explicit constructor? I didn't have that before and everything worked.
     
  43. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    No, one is created for you implicitly. But, if you create a constructor that takes parameters then you will also need a public parameterless one. I'm the future, some time after unity 5 is released, ill be upgrading the port to a new version of json .net which will support custom creation converters and will simplify that process and allow you to use non parameterless constructors and supply default arguments. This can be done now with json converters but it is more work.
     
  44. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    Ok, I checked for other possible causes and couldn't find any. Just in case I added parameterless constructors to my data classes. I'm getting this error:
    Should I leave error in this post (it's rather long). I can remove and e-mail it as well.

    Any help would be greatly appreciated.

    Unable to find type [UnityEngine.dll]UnityEngine.ResourceRequest

    (Filename: /Users/builduser/buildslave/unity/build/Runtime/Scripting/CommonScriptingClasses.cpp Line: 34)

    Init new state data
    Replacements.Attribute:CheckParameters(Object, Type)
    UnityEngine.Debug:Internal_Log(Int32, String, Object)
    UnityEngine.Debug:Log(Object)
    PlayerState:ClearAllStateData()
    PlayerState:LoadData()
    PlayerState:.ctor()
    Session:InitOnce()
    Session:Awake()
    Replacements.Attribute:CheckParameters(Object, Type)

    (Filename: /Users/builduser/buildslave/unity/build/artifacts/iPhonePlayer-armv7-il2cppGenerated/UnityEngineDebug.cpp Line: 56)

    TypeLoadException: A type load exception has occurred.

    at <Module>.UnsupportedMember_String (System.Security.SecurityElement self, System.String signature) [0x00000] in <filename unknown>:0

    at System.Type.MakeGenericType (System.Type[] typeArguments) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Utilities.StringUtils+<Indent>c__AnonStorey4E.<>m__52 (System.IO.TextWriter tw, System.String line) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Utilities.ReflectionUtils.MakeGenericType (System.Type genericTypeDefinition, System.Type[] innerTypes) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonArrayContract.EnsureGenericWrapperCreator () [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty (Newtonsoft.Json.JsonWriter writer, System.Object memberValue, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContract collectionValueContract) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.Schema.JsonSchemaException.get_LinePosition () [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0

    at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) [0x00000] in <filename unknown>:0

    at PlayerState.Serialize () [0x00000] in <filename unknown>:0
     
  45. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    That exception is indeed being generated inside json .net. Could you submit a bug report to unity? Il2cpp is chucking a wobbly on MakeGenericType with one of your properties but no idea which one.
     
  46. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    So basically I'm out of luck for now?

    I do like Json.NET. Really simplified json in Unity :(
     
  47. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Nah we will see if we can work around it in the meantime.
     
  48. sstublic

    sstublic

    Joined:
    Nov 12, 2012
    Posts:
    13
    Well, so far we've figured out that current IL2CPP doesn't support instantiation of LinkedList<> generic type, so avoid it for now.
     
  49. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,875
    Hi, I would like to know if this can be achieved with this library:

    I have a bunch of classes which derive from a base abstract class.
    Now, I have a List<Abstract Class>elements which is part of the main storage class which I want to serialize.

    I have tried to serialize this storage class but it says "elements" cannot be serialised because it's an abstract class. Any workaround?
     
  50. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Make sure you're passing an instance of JsonSerializerSettings to the serializer with the TypeNameHandling property set to TypeNameHandling.All. You'll need to do the same thing when you deserialize.