Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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,562
    I think you missed one step. The last thing you need to do is select that imported dll in the inspector. Then check All Platforms to map it to everything and click apply. Then it'll be found by the editor.
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    889
    I did, but I switched back to 3.5, reinstalled old version and new version of Unity, this made the project reinitiate anyway, seems to work now. Dunno what was the problem: Unity 2018.1.6 suddenly stopped working to execute your script parts and so I never got data from my json string into gameobjects. So, i guess, reinitializing the project helped it out, thank you anyway for your help! :)
     
    Dustin-Horne likes this.
  3. gmfy

    gmfy

    Joined:
    Nov 22, 2016
    Posts:
    9
    When will the new version be released?
     
    cgrow67 likes this.
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    couple weeks.
     
    cgrow67 likes this.
  5. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    442
    Hi there !

    I'm using JSON.Net for a while, and now I need to serialize class of type UnityEngine.Object like GameObject and MonoBehavior.

    I'm not sure about the approach. I guess we have to serialize each Unity Object individually, store them in a data structure (dictionnary, DB) and serialize only references to that Objects each time this type is met during serialization.

    But the deserialization seems tricky as you can't instantiate things yourself.

    Is anybody can share a working implementation of that?
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    889
    Can you show a code of your approach in a support forum thread? Maybe we can help you there @methusalah999
     
  7. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    442
    Fact is, I haven't started yet. I wanted to hear from the community before going into it.
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    889
    So, as far as I am using it, I serialize classes with objects in it. That is not the approach you are going for? Maybe you can describe some example of your game environment that needs to be serialized.
     
  9. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    442
    I have a simple hierarchy of a board, with tiles, and many things in each tile. The board is made at game launch, depending on the size choose by the player, and the player will place things in tiles. So everything is dynamic, not existing in the original scene.

    I have to save/load all that stuff to the hard drive.

    I began to make everything serializable by Unity. It was a success : I can hot reload the game at any moment. But it seems that the data stream Unity is making is not accessible and there is no way to use it as a save/load mechanism, which by the way is very frustrating.

    So now I need to make what Unity Serialization is already making under the hood : saving Unity.Object (mainly GameObjects and MonoBehaviors) as references, saving the hierarchy of GameObjects in my own serialized data stream, and finding a way to instantiate back all those objects.

    I already know about the solution of making proxies and doing a lot of data mapping, but I beleive there is a free generic solution out there to this very common issue and I hoped to hear about it among Unity JSON.Net users.
     
  10. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    This is something I'd have to look at more in depth. The biggest issue, as you stated, is that you can't just initialize these objects. That's for a variety of reasons like hierarchy relationships and transforms. The only way I could think that would make this work would be by deserializing by doing an Instantiate, which would be possible but managing references between gameobjects would be extremely difficult and all of this would need to be done via custom JsonConverters. I'm not sure how feasible it is.
     
  11. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    889
    Maybe we can look into the addressables system for that now, seems like the right part to involve relationships.
     
  12. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Quite possible. I haven't looked at that at all. I'm concentrating on .net 4.6 upgrade, fixing a couple edge bugs and getting ready for the 2018.3 update to support mapping by platform.
     
    Laicasaane likes this.
  13. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    889
    Looking forward for your next update, as my JSON.Net Populate Function isn't working with 2018.2 right now. Instead of overriding existing parts, it is creating new ones :D
     
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    That sounds like it could be a separate bug I've not seen. Could you drop me an email and share the code you're using so I can take a look at it?
     
  15. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    442
    I've made some progress on the subject.

    Serialization:
    I've created two proxies to wrap Unity objects : PersistedGameObject and PersistedComponent.

    I create a proxy that wrap the root GameObject I want to persist. Upon creation, the proxy goes through the hierarchy :
    - it builds a proxy for each Component and for each child GameObject,
    - it stores the ID of its parent,
    - it also stores GameObject's name, local position, rotation and scale.

    The PersistedComponent proxy is different.
    - it stores the ID of its owner GameObject,
    - it explores the wrapped Component members by reflexion, ignoring obsoletes, non-serializable, non-read/write, private, etc.,
    - it serialize values in a dictionary, thanks to Unity Json.NET,
    - it uses a single JsonConverter to manage nested Unity objects, saving only their instance ID.

    Finally, I serialize a dictionary of these proxies, with instance IDs as keys.

    Instantiation:
    Now we need to instantiate things. My proxies are made so that they are autonomous to instantiate the wrapped Unity object, because they the parent/owner instance ID:

    - if the parent of a game object proxy is not yet instantiated, the child proxy asks him to instantiate first, then connects the transforms.
    - if the owner of a component proxy is not yet instantiated, the component proxy asks him to instantiate first, then calls AddComponent on it.

    This is lazy instantiation: things are instantiated the first time they are needed. We don't have to worry about the hierarchy or the links. Eventually, everything is instantiated, the hierarchy is rebuild, and any link to a Unity object is retrieved. Phew !

    It works well in the scope of my need. Do you guys think it seems OK?

    Remaining issues:
    Now I need to solve two issues :
    If a GameObject has been instantiated from a prefab, I need to use that prefab again. This way, a save of my scene won't become obsolete if the prefab has changed in the meanwhile. Plus it will produce a much lighter json to save.

    For this one, I need a GameObject to store what's needed to find the source prefab. I suppose I will need a Component here, plus a standardized prefab management using enumeration, or path, or...?

    Also, for the first implementation, I only saved my own components to have better control. But one may change anything at runtime, like a collider, a mesh, the state of an animator... it will be necessary to save any kind component. But some component have complex data structure and I doubt it will be always possible. I guess I will have to study them one by one.
     
    Last edited: Jul 19, 2018
    Dustin-Horne likes this.
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    @methusalah999 that is awesome work! If you want to DM me, I'd love to take a look at the source and see if I can help you polish it up and maybe come up with a solution to the prefab issue. We'd also want to make sure we're preserving references in case two gameObjects reference the same gameObject... don't want to deserialize it twice. If you get this sucker working you might even be able to throw it up on the asset store for $5 or so.
     
  17. FallenTreeGames

    FallenTreeGames

    Joined:
    Jun 22, 2016
    Posts:
    25
    HI, I've been using your asset fine with Mono backend and it's been brilliant but I'm having issues using IL2CPP. I'm trying with the new v4 scripting backend, so have installed your new package, make sure that I deselect the import settings for the old dlls and selected 'Any Platform' for the new dll, but I'm getting the same issues as previously reported, that the Netwonsoft namespace can't be found.
     
    jvalencia likes this.
  18. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    @Dustin-Horne I'm using this.NET Standard 2.0 library (configuration: 2018.2.3f1 with .NET Standard 2.0 and not 4.x):

    https://github.com/aspnet/Configuration/blob/master/src/Config.Json/Config.Json.csproj

    (It has nothing to do with ASP NET so don't be afraid).

    It has more dependencies on other libraries but my main issues are two:
    1. If I use the regular "Newtonsoft.Json" from nuget for .NET Standard 2.0 I have issues on AOT platforms like any published with IL2CPP. I know that you made that fork for this specific matter.
    2. If I use your library I get "Unloading broken assembly Assets/Libs/Configuration/Microsoft.Extensions.Configuration.Json.dll, this assembly can cause crashes in the runtime". I suspect that one of the issues can be that it's not compiled against .NET Standard 2.0 but .NET 4.x. I think that we need that version too or have the source code (I see no commits since 2016 in the repo on github) and put it in Unity itself so it compiles to the correct target.

    Thanks.

    And finally one thought. Why the original library doesn't include your fixes for AOT and the specific Unity API things (like converters) be distributed by one on the Asset Store? Unity it's not the only suffering this, Xamarin developers would have the same problem at least on iOS.

    Edit: I also have tried the new preview package on this post and I have the same problem: https://forum.unity.com/threads/json-net-for-unity.200336/page-33#post-3533015
     
    Last edited: Aug 13, 2018
  19. rcFMS

    rcFMS

    Joined:
    Apr 17, 2018
    Posts:
    8
    I am also experiencing trouble when using the IL2CPP Scripting Backend (while the Mono Scripting Backend seems to work):

    I'd like to profile the memory usage of a windows standalone build using https://bitbucket.org/Unity-Technologies/memoryprofiler for which IL2CPP is needed in order to be able to see all objects in memory. (Using Mono it only shows some of the objects but not all.)

    Deserializing a ~60 MB file from which my scene gets procedurally generated usually takes ~8 seconds while running in the main thread. (Since this is blocking I've also added an option to call the deserialization on a new thread which is somewhat slower though with ~50 seconds.)

    While this works fine in the editor this is not the case in a windows standalone build. Instead I get this Exception:

    I've installed the contents of the .unitypackage linked above and even tried adding a new link.xml file to prevent code stripping of all classes (according to https://www.robinryf.com/blog/2017/08/22/unity-link-xml-file-definitions.html):
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <linker>
    3.     <assembly fullname="*">
    4.         <type fullname="*" preserve="all"/>
    5.     </assembly>
    6. </linker>
    (Note: I've tried several other versions with long lists of explicit names but the result was always the same. And in the end not even the wildcard version worked...)

    If anyone knows a solution to this it'd be much appreciated!
     
  20. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    I'll need to do some digging on this. I haven't tested with the new Netstandard 2.0 changes and you're correct in that the original was compiled against .NET 4.6. I have a feeling this is the same issue the others are running into in not being able to find the namespace.

    I don't control the official release and James Newton King has said he has no interest in supporting specific platforms (like Unity). I think the Xamarin folks do have some similar issues but a lot of the workarounds are pretty Unity specific.
     
  21. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    I'm very interested on getting a release for .NET Standard 2.0 as soon as possible.

    Can we get access to the repo? Or this new version is not open source? Can I get any preview release? Can I help you with some testing? Thanks.

    I don't understand that. That library is the most popular on nuget and it's used by almost every developer. It should be reasonable to have all developer resources in one repo and not across different ones fixing things making forks of the original one. Also Microsoft has adopted it as you can see in the library I mentioned in my previous post that it's causing me problems with Unity and the original Json .NET library.
     
  22. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Absolutely. The repo is here:
    https://github.com/ianmacgillivray/Json-NET-for-Unity

    It does not have any of the .NET 4.6 stuff in it as I'm still trying to solidify that.

    It's under Ian's account because it's a Google owned Repo. But I am still the lone contributor.
     
  23. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    I mean that when can we have access to the current version because in that repo I can't see your current work day by day.

    Thanks.
     
  24. v-ercart

    v-ercart

    Joined:
    Apr 25, 2018
    Posts:
    8
    I'm on Unity 2018.1.6f1, and I imported JSON.net for Unity. My platform is set to UWP. When I try to use JsonConvert.SerializeObjecT() unity complains about Newtsonsoft.Json.JsonConvert being defined twice.

    To fix this, I went to the Standalone folder and selected the Newtonsoft DLL and unchecked Editor. Now it is not defied twice. Are both DLLs getting loaded in the editor when your platform is set to UWP?
     
  25. ItzChris92

    ItzChris92

    Joined:
    Apr 26, 2017
    Posts:
    365
    Hi, I am using JSON.net to serialize my game data. I'm having an issue when serializing my inventory. It is a list of ScriptableObjects and I have a warning occur each time I deserialize each element of my inventory list (example below).

    Weapon must be instantiated using the ScriptableObject.CreateInstance method instead of new Weapon.
    UnityEngine.ScriptableObject:.ctor()
    Item:.ctor() (at Assets/_Scripts/Items/Item.cs:9)
    Equippable:.ctor()
    Weapon:.ctor()
    Weapon:CreateWeapon()
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateNewObject(JsonReader, JsonObjectContract, JsonProperty, JsonProperty, String, Boolean&)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateObject(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateValueInternal(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:populateList(IList, JsonReader, JsonArrayContract, JsonProperty, String)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateList(JsonReader, Type, JsonContract, JsonProperty, Object, String)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateValueInternal(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:SetPropertyValue(JsonProperty, JsonConverter, JsonContainerContract, JsonProperty, JsonReader, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:populateObject(Object, JsonReader, JsonObjectContract, JsonProperty, String)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateObject(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateValueInternal(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:SetPropertyValue(JsonProperty, JsonConverter, JsonContainerContract, JsonProperty, JsonReader, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:populateObject(Object, JsonReader, JsonObjectContract, JsonProperty, String)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateObject(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:CreateValueInternal(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader:Deserialize(JsonReader, Type, Boolean)
    Newtonsoft.Json.JsonSerializer:DeserializeInternal(JsonReader, Type)
    Newtonsoft.Json.JsonSerializer:Deserialize(JsonReader, Type)
    Newtonsoft.Json.JsonConvert:DeserializeObject(String, Type, JsonSerializerSettings)
    Newtonsoft.Json.JsonConvert:DeserializeObject(String, JsonSerializerSettings)
    DataManager:LoadPlayerData(String) (at Assets/_Scripts/Data/DataManager.cs:28)
    Player:LoadPlayerData() (at Assets/_Scripts/Player.cs:46)
    Player:Start() (at Assets/_Scripts/Player.cs:21)

    Is there any built in way to handle this? If not, could you please advise a fix if possible? Any help would be much appreciated.

    Ta

    EDIT: Excuse the smilies on the above warning. Not too sure how to get rid of them now they're there!
     
  26. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    They shouldn't be... Are you using IL2CPP as your target? It's also possible that another asset you're using has a Newtonsoft.Json.dll in it that's being used for editor.
     
  27. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    That's Unity creating those warnings. ScriptableObject isn't currently supported directly (just like GameObject) because of the need to instantiate and now "new". You could write your own CustomCreationHandler for it or a JsonConverter that would handle it for you by using ScriptableObject.CreateInstance(), then using PopulateObject to populate it's properties.
     
  28. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Right, my day to day work is not in the repo (yet). Also, there seem to be some issues with NetStandard 2.0 and .NET Framework 4.6.1... it's something that's exploded into discussion over the last couple of days and evidently Microsoft is moving the full framework version compatibility to 4.7.2 and saying they shouldn't have tried to push NetStandard 2.0 into 4.6.1.. :/
     
  29. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    As you may know there are many popular libraries on nuget that depend on JSON .NET and right now we have serious problems as we can't publish to any platform with the combo IL2CPP + .NET 4.x + .NET Standard 2.0.

    We can't target .NET 4.6 instead of .NET Standard 2.0 because the libraries we are using have references to Windows APIs and Unity throws exceptions when importing them in the project. They could work on standalone but not on mobile or WebGL platform.

    Unity have said many times that we should target .NET Standard 2.0 if we go cross platform because it's the safer way.

    Do you have an estimated ETA on a .NET Standard 2.0 compatible version? At least a preview version.

    Thanks.
     
  30. Eveliyse

    Eveliyse

    Joined:
    Apr 21, 2017
    Posts:
    2
    Have you seen this article?
    https://docs.microsoft.com/en-us/visualstudio/cross-platform/unity-scripting-upgrade?view=vs-2017
    They use json.NET and .NET Standard 2.0 as an example. I haven't tried it yet so not sure if it works but wouldn't hurt to give it a try?
     
  31. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    I've not yet tested that but the official netstandard compatible release of json .net should work. You would likely have to lift my link.xml and converters as well to properly support types like vectors and matrices. The reason it's a lot of work with my package is because I also maintain backward compatibility.
     
  32. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    But .NET Standard compatibility doesn't mean AOT compatibility.

    I'm not sure that nuget official version works on all platforms that Unity supports.

    In my case I get this error on Android using IL2CPP and .NET Standard 2.0. On Mono works without any error. I don't know if this error is an stripping problem.

    Do you know anything @Dustin-Horne ??
     

    Attached Files:

  33. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    That's correct. Netstandard 2.0 API compatibility does not mean it will work with il2cpp. I don't think that particular error is stripping related. I haven't seen it before. I'm not familiar with the LightLambda namespace. It looks like it's trying to create a dynamic delegate for an object constructor.

    Also to answer your previous question, I don't have an online WIP because I no longer own the repo.
     
  34. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    I don't know what to do. James from Newtonsoft is not very friendly on Unity or Xamarin related issues. I read old issues with rude answers.

    Do you know when will you have a preview version? You are my only solution right now.
     
  35. ashikns

    ashikns

    Joined:
    Nov 23, 2016
    Posts:
    8
    I had the same issue as in UWP. Fixed it by adding this to link.xml


    <assembly fullname="System.Core">
    <type fullname="System.Linq.Expressions*" preserve="all"/>
    </assembly>


    Also in general I needed Json .Net 11.0.2, so I took the .NetStandard 2.0 dll from latest nuget. It works well for me so far with link.xml from SaladLab's Json.Net.
     
    Dustin-Horne likes this.
  36. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    Thanks you @ashikns I will try that!
     
  37. Uli_Okm

    Uli_Okm

    Joined:
    Jul 10, 2012
    Posts:
    93
    Hello @Dustin-Horne
    I'm trying to remove all the references to System.Xml.dll from the source code (to make my webgl build smaller). I have successfully opened the source, removed every single System.xml.dll and System.Xml.Linq references from the code, removing all the necessary functions and compiling the fixed dll files (also removing the references from the .sln project).
    But even doing this, Unity is still getting references to System.Xml.dll during the build process.
    I'm testing using Unity 2018.2.3, WebGL build target, all stripping options on, and I already tested an empty project with and without Json.NET and it is really it who is adding the reference to the System.Xml.dll.
    Any idea of what can be causing this behaviour? Is there a reference to something outside the "using System.Xml" and "using System.Xml.Linq" that you know that can have a "hidden" reference to system.xml.dll?

    Thanks
     
  38. Tongtiep

    Tongtiep

    Joined:
    Apr 17, 2016
    Posts:
    1
    I have problem when build il2cpp with windons, I'm using unity 2018.0.1 and Json.net latest version.
    Ex [Hub - diskShaking]: IHub.OnMethod - callback - Message: 1: C:\unity2018.0\Editor\Data\il2cpp\libil2cpp\icalls\mscorlib\System.Reflection.Emit\DynamicMethod.cpp(19) : Unsupported internal call for IL2CPP:DynamicMethod::create_dynamic_method - System.Reflection.Emit is not supported. at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructor[T] (System.Type type) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract (System.Type objectType) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
     
  39. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    38
    I have a scenario where I have a 3rd party library which is dependent upon 8.0.3 of JSON.NET from Nuget, and want to consume it within unity. Is there any way to redirect it to use the provided JSON.NET asset store version to satisfy its dependency? as at the moment it complains that the version it wants cannot be found (I have rebuilt the original one to use 8.0.3 as per the Asset Store target version).

    Is there any way to get this scenario working or is impossible to "trick" the 3rd party .net dll into using this JSON version rather than the nuget version? (I can rebuild the 3rd party DLL using any JSON.NET nuget version as the source is on github if there is a way to do it).
     
  40. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Hi, I'm having issues deserializing.

    I was able to serialize to memory stream but I'm getting an error when deserializing:

    JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[dbMatchTanks]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
    To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
    Path '0'.
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    Newtonsoft.Json.JsonSerializer.Deserialize[T] (Newtonsoft.Json.JsonReader reader) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
    SaveLoad_TankData.Load_OnlineData (System.Byte[] boxedData) (at Assets/10_Scripts/SaveLoad/SaveLoad_TankData.cs:52)
    MultiplayerPlayerSetup.Sync_Ai_Dat (System.Byte[] _dataDic) (at Assets/10_Scripts/Multiplayer/MultiplayerPlayerSetup.cs:160)
    MultiplayerConnector.SyncAIdata (System.Byte[] _dataToSend) (at Assets/10_Scripts/Multiplayer/MultiplayerConnector.cs:312)
    System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    NetworkingPeer.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, System.Int32 senderID) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3007)
    NetworkingPeer.RPC (PhotonView view, System.String methodName, PhotonTargets target, PhotonPlayer player, System.Boolean encrypt, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3839)
    PhotonNetwork.RPC (PhotonView view, System.String methodName, PhotonTargets target, System.Boolean encrypt, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2942)
    PhotonView.RPC (System.String methodName, PhotonTargets target, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:597)
    MultiplayerConnector.RequestMasterAiSync () (at Assets/10_Scripts/Multiplayer/MultiplayerConnector.cs:301)
    System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    NetworkingPeer.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, System.Int32 senderID) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3007)
    NetworkingPeer.RPC (PhotonView view, System.String methodName, PhotonTargets target, PhotonPlayer player, System.Boolean encrypt, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3863)
    PhotonNetwork.RPC (PhotonView view, System.String methodName, PhotonTargets target, System.Boolean encrypt, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2942)
    PhotonView.RPC (System.String methodName, PhotonTargets target, System.Object[] parameters) (at Assets/zz_Extensions/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:597)
    MultiplayerPlayerSetup.Start () (at Assets/10_Scripts/Multiplayer/MultiplayerPlayerSetup.cs:85)


    The kind of data I'm serializing is List<dbMatchTanks>

    where
    Code (CSharp):
    1. public class dbMatchTanks
    2. {
    3.     [PrimaryKey]
    4.     public int id { get; set; }
    5.     public string model { get; set; }
    6.     public int skin { get; set; }
    7.     public int faction { get; set; }
    8.     public int team { get; set; }
    9.     public int tier { get; set; }
    10.     public int era { get; set; }
    11.     public bool ai { get; set; }
    12. }
    The serializing script looks like this
    Code (CSharp):
    1.  
    2.  
    3.     public static byte[] Save_OnlineData(List<dbMatchTanks> TankDatabaseOutput){
    4.  
    5.         Debug.Log("online save");
    6.         using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    7.         {
    8.             var writer = new BsonWriter(ms);
    9.             var serializer = new JsonSerializer();
    10.             serializer.Serialize(writer, TankDatabaseOutput);
    11.             Debug.Log("vvvvvvvvvvvv");
    12.             return ms.ToArray();
    13.  
    14.         }
    15.  
    16.     }
    17.  
    18.     //En online se recive byte[] via RPC y se envia aqui para deserializar
    19.     public static List<dbMatchTanks> Load_OnlineData( byte[] boxedData) {
    20.  
    21.         List<dbMatchTanks> data;
    22.      
    23.         using (System.IO.MemoryStream ms = new System.IO.MemoryStream(boxedData))
    24.         {
    25.             var reader = new BsonReader(ms);
    26.             var serializer = new JsonSerializer();
    27.             data = (List<dbMatchTanks>)serializer.Deserialize<List<dbMatchTanks>>(reader);  
    28.         }
    29.         return data;  
    30.     }

    I've seen two pages ago a dude having issues with lists too but that conversation vanishes without solution, yet he has not same issue. I'm targeting Xbox too. Not sure if this could be related. I'm not sure either WHat a Jason array is, likely I'm doing something wrong, prior to this I was serializing dicitonaries without issues and I switched to least in belief it would be ase simple but....

    EDIT: OK , I see this kind of stuff is not meant to work out of the box.

    So I'll pretty much revert to the dictonary setup and dump the database output in teh dictionary because after all it is pretty much a key, Value (custom class) setup like the one I had before.
     
    Last edited: Sep 19, 2018
  41. RoCo

    RoCo

    Joined:
    Apr 16, 2013
    Posts:
    1
    Hi, the asset store doesn't specify if the library is supported by Unity 2017/2018, I see people getting some issues in 2018 but is not clear if these are particular issues for custom builds, or using specific features from this library.

    Can anyone clarify how stable is for these versions or what common/known problems we can face during the integration?

    thanks in advance
     
  42. GuardHei

    GuardHei

    Joined:
    Feb 10, 2018
    Posts:
    65
    I know that it was an old thread, but I just can't find ColorConverter in the namespace Newtonsoft.Json.Converters and those Unity specific converters. My version is 11.2
     
  43. GuardHei

    GuardHei

    Joined:
    Feb 10, 2018
    Posts:
    65
    Oh, just realized that I shouldn't use the Github version but the from the asset store
     
    Dustin-Horne likes this.
  44. TriNityGER

    TriNityGER

    Joined:
    Sep 1, 2017
    Posts:
    29
    Hey guys, I'm getting an error when starting our game. Any ideas on this one? I suspect code stripping...

    We're building for Android with Unity 2018.1.9f1 IL2CPP.

    EDIT: found the culprit. The plugin collides with the stripping of the array constructor.
    I had to change EnumType[] arr; to List<EnumType> arr;

    I tried everything with link.xml, no result. Any ideas @Dustin-Horne?


    Code (CSharp):
    1. 10-09 18:11:35.796: E/Unity(8321): Exception has been thrown by the target of an invocation.
    2. 10-09 18:11:35.796: E/Unity(8321):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
    3. 10-09 18:11:35.796: E/Unity(8321):   at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0
    4. 10-09 18:11:35.796: E/Unity(8321):   at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
    5. 10-09 18:11:35.796: E/Unity(8321):   at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory+<>c__DisplayClass5_0`1[T].<CreateDefaultConstructor>b__1 () [0x00000] in <00000000000000000000000000000000>:0
    6. 10-09 18:11:35.796: E/Unity(8321):   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateTemporaryCollection () [0x00000] in <00000000000000000000000000000000>:0
    7. 10-09 18:11:35.796: E/Unity(8321):   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serializat
    8. 10-09 18:11:35.834: E/Unity(8321): [Error]Error while trying to handle result (KingArt.Upgame.User.DataTransfer.GetUserResponse):Exception has been thrown by the target of an invocation.
    9. 10-09 18:11:35.834: E/Unity(8321):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
    10. 10-09 18:11:35.834: E/Unity(8321):   at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0
    11. 10-09 18:11:35.834: E/Unity(8321):   at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
    12. 10-09 18:11:35.834: E/Unity(8321):   at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory+<>c__DisplayClass5_0`1[T].<CreateDefaultConstructor>b__1 () [0x00000] in <00000000000000000000000000000000>:0
    13. 10-09 18:11:35.834: E/Unity(8321):   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateTemporaryCollection () [0x00000] in <00000000000000000000000000000000>:0
    14. 10-09 18:11:35.834: E/Unity(8321):   at Newtonsoft.Json.Serialization.JsonSerial
    15. 10-09 18:11:35.841: E/Unity(8321): Unable to find libc
    16. 10-09 18:11:35.877: E/Unity(8321): [Exception:r]Exception in MessageBus.PublishInternal: System.ArgumentNullException: Value cannot be null.
    17. 10-09 18:11:35.877: E/Unity(8321): Parameter name: source
    18. 10-09 18:11:35.877: E/Unity(8321):   at System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <00000000000000000000000000000000>:0
    19. [...]
    :
     
    Last edited: Oct 12, 2018
  45. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    120
    How do I access list of lists?? Any help???thanks in advance
     
  46. TriNityGER

    TriNityGER

    Joined:
    Sep 1, 2017
    Posts:
    29
    Okay I found a new bug. The StringEnumConverter does NOT respect the CamelCase property!
    No matter the setting, it always saves as CamelCase.
    For example I have the enum Quality which has 3 values: Bronze, Silver, Gold. and I want a property serialized as "Quality": "bronze", it will always result in: "Quality": "Bronze".

    Previously it always worked like I want it now. No idea what broke it. I had to brutally rip out all referenced classes and add a small .ToLowerInvariant() to the EnumUtils.ToEnumName() method.

    I need advice from you @Dustin-Horne
     
  47. jrhee

    jrhee

    Joined:
    Dec 5, 2013
    Posts:
    72
    Is there any way to avoid including the "Assembly-CSharp" suffix in my $type fields? The namespace and assembly info will be the same in all the data I'm serializing, so it'd be great to be able to avoid populating it for every object field.

    I've tried overriding SerializationBinder in my serializer settings as suggested here, but when I do I can't get the specified type to resolve.

    Code (CSharp):
    1.  
    2. public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
    3. {
    4.     assemblyName = "Assembly-CSharp";
    5.     typeName = serializedType.Name;
    6. }
    7.  
    Any help would be appreciated!
     
  48. helloworldgames

    helloworldgames

    Joined:
    Mar 16, 2017
    Posts:
    60
    Is it being maintained or dead?
     
  49. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    My apologies, I've not been on the forums for awhile and for some reason I haven't received any post notifications. Looks like I have a few to get through here.
     
    TriNityGER likes this.
  50. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    This sounds like an incorrect DLL mapping. Open the /jsondotnet/assemblies folder and look at the DLLs in those folders. Make sure that only the AOT dll is mapped for IL2CPP on Windows. It does not make use of Reflection.Emit.
     
unityunity