Is it possible to use DataContractJsonSerializer?

I have an already established architecture using DataContracts in conjunction with DataContractJsonSerializer but cannot seem to get it working with unity. It suggests a missing assembly reference but I’ve added the reference and also tried to place the dll in the plugins folder. Has anyone done this or know what the problem might be?

Also I noticed the reference to JsonFX.Json.dll did unity use this for a localization solution? Do you recommend using JsonFX instead?

Yes you can. You need to find the .net vs 3.5 System.ComponenetModel.DataAnnotations dll and move it into the assets/plugins/ folder.

I personally use both. I use DataAnnotations so that my model objects can easily be validated using model state in my MVC api controller. In unity DataAnnotations doesn’t do anything (for me) - I just have it there to save the headache of excluding the attributes in the shared dll. I use JsonFX to do the actual serialization in Unity. I am using JsonFX vs 1.4.1003.3007 using the 2.0.50727 run-time.

I still haven’t been able resolve my issue with DataContractJsonSerializer but I did find out useful information that I can share.

Firstly unity doesn’t actually use the .NET framework which in hindsight is obvious as it would ruin localization–only working on windows. Unity scripts are executed on the .NET 2.0 runtime and as of unity version 3/4 you can use C# 4.0 syntax (default params is an example). Mono which unity runs version 2.6 (not the latest) is comparable to the .NET framework but does not support all the features of 3.5 and 4 versions of dot NET.

Now onto the DataContractJsonSerializer well the DLL I was original looking for System.Runtime.Serialization.dll is .NET 4.0 which I assume isn’t supported. Any older version of System.Runtime.Serialization.dll doesn’t have a DataContractJsonSerializer because it was actually moved from System.ServiceModel.Web.dll so I tried that in 3.5 with a System.Runtime.Serialization.dll from 3.5 and while everything seemed correct in MonoDevelop unity fired off an error of System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

I was either very close to or on the complete wrong track to resolving this issue. I’m going a different route because in the time it took me to hunt down all this information I could have coded a different solution. Hopefully it will be helpful to someone else and they are more determined to resolve it.