Search Unity

Json.Net, iPhone compatibility

Discussion in 'Scripting' started by half_voxel, May 20, 2012.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi

    I'm working with serialization at the moment. Json.Net is a really great package, but out of the box, it is not compatible with Unity iPhone. So I edited the source code a bit to get rid of the System.Xml and System.Data dependencies (this also reduced the player size with about 0.7mb). But since I haven't got Unity iPhone, I cannot test this. I'm building a package for Unity, and it would be a bit bad if it didn't work on the iPhone.

    If anyone would care to test the scripts I have attached I would be so grateful :D
    Feel free to grab it and use the modified Json.Net code in your own projects if it turns out to work.

    Basically, drop the .dll and script anywhere in your project, add the script to a gameobject and see if it compiles for the iPhone. please also try to run it on the iphone since it might not throw errors until then.

    Thanks in advance!
     

    Attached Files:

  2. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    Works in editor. Compiles to iphone if you use .Net instead of .Net Subset. But crashes on launch on iphone.

    Heres the log

    Code (csharp):
    1. 2012-05-20 12:50:30.295 GymGame[497:707] -> registered mono modules 0x158e538
    2. -> applicationDidFinishLaunching()
    3. Mono path[0] = '/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed'
    4. Renderer: PowerVR SGX 535
    5. Vendor:   Imagination Technologies
    6. Version:  OpenGL ES 2.0 IMGSGX535-63.24
    7. GL_OES_depth_texture GL_OES_depth24 GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_mapbuffer GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_float GL_OES_texture_half_float GL_OES_vertex_array_object GL_EXT_blend_minmax GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_read_format_bgra GL_EXT_separate_shader_objects GL_EXT_shader_texture_lod GL_EXT_texture_filter_anisotropic GL_APPLE_framebuffer_multisample GL_APPLE_rgb_422 GL_APPLE_texture_format_BGRA8888 GL_APPLE_texture_max_level GL_IMG_read_format GL_IMG_texture_compression_pvrtc
    8. Creating OpenGLES2.0 graphics device
    9. Initialize engine version: 3.5.2f2
    10. Begin MonoManager ReloadAssembly
    11. Platform assembly: /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/UnityEngine.dll (this message is harmless)
    12. Loading /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/UnityEngine.dll into Unity Child Domain
    13. Platform assembly: /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Assembly-CSharp.dll (this message is harmless)
    14. Loading /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Assembly-CSharp.dll into Unity Child Domain
    15. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/PatStuff.dll (this message is harmless)
    16. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/System.Core.dll (this message is harmless)
    17. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/System.dll (this message is harmless)
    18. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Mono.Security.dll (this message is harmless)
    19. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/System.Configuration.dll (this message is harmless)
    20. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/System.Xml.dll (this message is harmless)
    21. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/System.Security.dll (this message is harmless)
    22. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Mono.Posix.dll (this message is harmless)
    23. Non platform assembly: /private/var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Newtonsoft.Json.dll (this message is harmless)
    24. Platform assembly: /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/PatStuff.dll (this message is harmless)
    25. Loading /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/PatStuff.dll into Unity Child Domain
    26. Platform assembly: /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Newtonsoft.Json.dll (this message is harmless)
    27. Loading /var/mobile/Applications/2E589F24-7FD0-45A5-96EC-9369A932182F/GymGame.app/Data/Managed/Newtonsoft.Json.dll into Unity Child Domain
    28. - Completed reload, in  0.401 seconds
    29. -> applicationDidBecomeActive()
    30.  
    31. Unhandled Exception: System.ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) A:CreateA ()' while running with --aot-only.
    32.  
    33.  
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Ouch... not good.
    Hm. Seems it crashes when it tries to create a new instance of the class. That could be done with the Activator class...
     
  4. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    maybe its just a setting. it says while running with --aot-only
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    The iphone always runs with Ahead Of Time compilation. It cannot run with JIT (just in time compilation). So when you try to run any dynamic methods, it cannot handle it.
    Wait a moment, I'm setting up a new test, if you have time.
     
  6. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    Im just stepping out for a bit but i could do it in about an hour
     
  7. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Ok, so I have modified the Json.Net code a bit to create types using the System.Activator class instead (which I am almost 100% positive works). So let's see what it crashes on now.
    In the script code you can see I have divided it in sections. If it crashes on some early section, do you think you can just comment out that code so I can get test results for the later sections as well?
     

    Attached Files:

  8. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Well, actually I don't think the above code will work... Too much reflection stuff.
    So I decided to try JsonFx, it even says it should work for ios. But you never know.
    I have modified that as well with a bit more features and removed System.Xml reference.
    If anyone has time to test it (should not take more than 5 minutes), that would be awesome.
    Feel free to "steal" the dll if you want. And if anyone want the source, just ask and I will send it to you.

    Thanks! :D
     

    Attached Files:

  9. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    Hey i tested the last one for you, but right on import it says the dll is missing the type or namespece "DebugConsole"
     
  10. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Oh, I must have forgot to comment out that part...
    It should work if you comment out the "using ..." statement referencing to it at the top of the script. I think that is the only reference.
    Thanks!
     
  11. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    That works now when i build to the iphone. Any chance I can get the source?
     
  12. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Yey! Awesome.
    Sure, I can send it to you when I get home.
     
  13. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
  14. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
  15. Xerosigma

    Xerosigma

    Joined:
    Mar 1, 2012
    Posts:
    27
    You're amazing. Thanks for sharing this. Makes a lot of things possible.
     
  16. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85
    Have you tested your version of jsonFX with Unity + iOS since the latest Unity 4 update (4.01f2)? It seems like it's broken now on iOS devices. I made a test project if you want to easily check it out: https://github.com/tayl0r/jsonfxV1-UnityTest

    I've tested the vanilla jsonfx 1.4 and your version of jsonfx and they both fail the same way.

    Code (csharp):
    1.  
    2. System.MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.ComponentModel.Int32Converter'.
    3.   at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
    4.   at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
    5.   at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) [0x00000] in <filename unknown>:0
    6.   at JsonFx.Json.TypeCoercionUtility.CoerceType (System.Type targetType, System.Object value) [0x00000] in <filename unknown>:0
    7.   at JsonFx.Json.JsonReader.ReadNumber (System.Type expectedType) [0x00000] in <filename unknown>:0
    8.   at JsonFx.Json.JsonReader.Read (System.Type expectedType, Boolean typeIsHint) [0x00000] in <filename unknown>:0
    9.   at JsonFx.Json.JsonReader.ReadObject (System.Type objectType) [0x00000] in <filename unknown>:0
    10.   at JsonFx.Json.JsonReader.Read (System.Type expectedType, Boolean typeIsHint) [0x00000] in <filename unknown>:0
    11.   at JsonFx.Json.JsonReader.Deserialize (Int32 start, System.Type type) [0x00000] in <filename unknown>:0
    12.   at JsonFx.Json.JsonReader.Deserialize (System.String value, Int32 start, System.Type type) [0x00000] in <filename unknown>:0
    13.   at JsonFx.Json.JsonReader.Deserialize[TestDataStructAll] (System.String value) [0x00000] in <filename unknown>:0
    14.  
     
  17. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi

    When using the stripping bytecode feature you need to include a link.xml file in the Asset folder.
    See http://docs.unity3d.com/Documentation/Manual/iphone-playerSizeOptimization.html

    Code (csharp):
    1. <linker>
    2.        <assembly fullname="System">
    3.             <type fullname="System.ComponentModel.TypeDescriptor" preserve="all"/>
    4.             <type fullname="System.ComponentModel.TypeConverter" preserve="all"/>
    5.             <type fullname="System.ComponentModel.StringConverter" preserve="all"/>
    6.             <type fullname="System.ComponentModel.BooleanConverter" preserve="all"/>
    7.             <type fullname="System.ComponentModel.EnumConverter" preserve="all"/>
    8.             <type fullname="System.ComponentModel.Int64Converter" preserve="all"/>
    9.             <type fullname="System.ComponentModel.Int32Converter" preserve="all"/>
    10.             <type fullname="System.ComponentModel.Int16Converter" preserve="all"/>
    11.             <type fullname="System.ComponentModel.ArrayConverter" preserve="all"/>
    12.             <type fullname="System.ComponentModel.CollectionConverter" preserve="all"/>
    13.             <type fullname="System.ComponentModel.DecimalConverter" preserve="all"/>
    14.             <type fullname="System.ComponentModel.SingleConverter" preserve="all"/>
    15.             <type fullname="System.ComponentModel.DoubleConverter" preserve="all"/>
    16.        </assembly>
    17. </linker>
     
  18. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85
    ^^ Wow, you're the man. Thanks so much! Somehow this was *not* broken for me until a couple of weeks ago and I've been using "stripping bytecode" since the day I started this project.
     
  19. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Most of the times it works, but some things require classes which are removed by bytecode stripping.
     
  20. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    The code seems to be broken, it has a lot of type errors. I don't see JsonConvertor in the supplied Json source

    he type or namespace name `JsonConverter' could not be found. Are you missing a using directive or an assembly reference?
     
  21. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi

    The code is fine. Just open the JsonFx.Json.sln file with monodevelop (or possibly Visual Studio, haven't tested) and build it. That should give you a dll in JsonFx.Json/bin/(Release or Debug)/JsonFx.Json.dll

    The namespacing is a bit weird in the code though. You have to add
    Code (csharp):
    1. using Pathfinding.Serialization.JsonFx;
    at the top of scripts which use the code.
     
  22. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Nope, still error. I compiled with VStudio

    error CS0308: The non-generic method `Pathfinding.Serialization.JsonFx.JsonReader.Deserialize()' cannot be used with the type arguments

    The dll in project give by talyor works fine.
     
  23. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Ok, your Deserialize interface is a little different. I got it to work. However, I have a special case where it is failing. I'm trying to ignore null values so I use the DefaultValueAttribute

    [System.ComponentModel.DefaultValueAttribute (null)]
    public Vector3? TestVector3Two;

    So I could serialize fine, but when I deserialize, I get coersion error

    JsonTypeCoercionException: Interfaces, Abstract classes, and unsupported ValueTypes cannot be deserialized. (System.Nullable`1[[UnityEngine.Vector3, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]])
    Pathfinding.Serialization.JsonFx.TypeCoercionUtility.InstantiateObject (System.Type objectType, System.Collections.Generic.Dictionary`2 memberMap)
    Pathfinding.Serialization.JsonFx.JsonReader.ReadObject (System.Type objectType)
     
  24. sterlingnewton

    sterlingnewton

    Joined:
    Jan 21, 2013
    Posts:
    1
    You say you got this working but not what the fix was. I am just downloading the project to try it out with Unity on iOS and have the same Unity compile error. It would be great if you could drop a line with the correction you made to get it to compile. I am using the most current Unity Pro version V4.2.1f4.

    I also tried downloading the project linked by tayl0r and using the dll there but I get the same error when trying to compile JSONFxDemo.cs. While I obviously do not need the demo code in my project I do need to deserialize into an object so the problem is the same.
     
  25. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85
  26. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    imtrobin didn't end up getting it working but I worked with him and I converted JSON .NET to be compatible with the WebPlayer and iOS without the AOT errors so that's what he's using now. It was approved and published to the Asset Store today. I'm not going to spam the link.. just look for: JSON .NET For Unity

    Tested and working in Unity 3.5+, 4.0, 4.2 and 4.2.1
     
  27. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Dustin JSON.Net worked great, on IOS (That real bi*** to get working on many other lib). I need something fairly complex, custom Unity3 Vector3 as nullable types.
     
  28. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Damn, is JSON so complicated that we need to spend an extra U$25 to get it on mobile?

    Or is there any free option to just simply **read** JSON?

    I have been trying https://github.com/tonioloewald/jsonparse but that doesn't work too well. On wikify they recommend using http://wiki.unity3d.com/index.php/JSONObject there but I haven't because there are too many options. Wondering if anyone had already reviewed all that... And MiniJSON looks like it's too old and outdated.
     
    Last edited: Jan 29, 2015
  29. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Probably not... although I think that my asset is the only one really working for iOS 64-bit and WebGL (the IL2CPP stuff) although I'm still seeing some IL2CPP bugs. The complexity is in mapping the json back to class objects. There are also lots of extra features like custom converters, polymorphism, reference loop handling, etc. But not everyone needs all of that.

    I would recommend trying JSONObject. I believe it works fine on mobile (even iOS). However, I don't know about IL2CPP... but it's worth a try. It's free... just a little more work to use.
     
    Cawas likes this.
  30. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    JSONObject might be perfect, its usage syntax sure looks beautiful, but seems it doesn't like playing with javascript for whatever reason.

    Since you're here, few straight and not all that related questions:

    - Are you making a living out of this? Sounds quite amazing! As a programmer myself, I only wish I had done something like that. I still don't think charging or licensing any kind of software is a good idea but, I agree there's still probably no better way to monetize on all the work done since without such enforcements people simply don't pay. Or do they? I'm always hoping there's a better way.

    - How does the licensing work? To me that's mostly the main issue behind charging, as it can get really painful for both sides.

    - Also I got no idea if it would bring an overhead from any perspective, but I really just wanted a simple class for reading JSON on mobile. If I just delete all those extra files, can I keep it smaller and simpler? Or is it ok to just not bother about it?

    Although I'll probably go with JSONObject anyway.
     
  31. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    So, try putting JSONObject in the /plugins folder. That will make it compile *before* the JavaScript and you should be able to use it. You would have to do the same thing with my asset. Mine does work for JavaScript but you have to move it to the /plugins folder.

    I certainly wish I was making a living off of it. :) I'll be honest, it does quite well on the asset store, but certainly isn't supporting my family by any means. I have a full time salaried gig as a .NET developer. The reason I don't make it free is because of support. I did spend a long time doing the port to begin with... making it play nicely with different platforms and especially AOT was not easy. There are still occasionally bugs.. and people also email me asking "How do I do 'X'?".

    Since it is a paid asset I gladly offer my assistance. I write custom converters... I help people properly format their JSON... I help people create the proper classes to match their Json... It's all part of the package. Obviously I can't do everything for them, but I try to be as helpful as I can.

    Next comes IL2CPP. I have a beta build sitting in my source control now with directives for WebGL and it's being tested. I also support IL2CPP on iOS 64 bit. We are seeing a couple of bugs related to IL2CPP but that's all being taken care of and actively maintained. So, the purchase price really goes to supporting my time for that... and with 64-bit iOS looming I have been getting roughly 10 emails or more per day with questions.

    As for licensing... it's just the standard asset store license. Part of my asset is actually MIT licensed and part of it is not. Just depends on what bits it is. As for stripping out files... it's not really that simple as there are references all over the place. Really for what you want to do I think JSONObject is the best bet. I've actually recommended it to several people. I don't think anyone should be buying my asset unless they actually need it. :)
     
    Dalton-Lima, Zaelot and Cawas like this.
  32. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    I tried, it doesn't. Then I made a CSharp to use it, works fine. I've got other CS working on plugin along with javascript without issues as well.

    Well, I need something, and I would donate something back as gratitude. But donations are also not a very good way to go... Your income would easily fall to nothing, in this case, and make supporting it impossible. Maybe we'll just need some kind of bitcoins to catch up before making a good system.

    I figured it would go like the standard asset store license, but I'm also not sure how that works. :p
     
  33. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Well it basically means you can't share or resell it but you can use it in your games without limitation.
     
    Cawas likes this.
  34. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    I have reviewed all json options in the market, and dustin one is the only one that works on ios, android, webplayer. When you don't mind type safety, you can do jsonobject parsing manually, it will work but tedious but now I can do deserialize in one line.
     
    Zaelot, Cawas and Dustin-Horne like this.
  35. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Thanks Robin. ;) And I haven't forgotten about you... need to get that converter updated to handle the nullable enums. Have been inundated with iOS 64-bit + IL2CPP emails the last couple weeks. :/
     
    imtrobin likes this.
  36. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Dustin's does sound amazing. :)

    Oh, and he won't link it, but I will as I should have done in the post before: http://forum.unity3d.com/threads/released-json-net-for-unity-3-5-with-aot-support.200336/

    Also, Dustin, what IL2CPP stands for, exactly? Google search yields only this: http://blogs.unity3d.com/2014/05/20/the-future-of-scripting-in-unity/ which insistis to use only the freaking acronym, something too many people are used to use without expanding on it. :mad:

    I can't wait for unity mobile binaries get to less than 1mb when using 2D only. :D
     
    Last edited: Jan 30, 2015
    Dustin-Horne likes this.
  37. ChristineZ

    ChristineZ

    Joined:
    May 22, 2016
    Posts:
    7
    Hi, I did as what TowerOfBricks said to add a link.xml file in asset folder in unity3d. It doesn't give me the exception of TypeCovertor now. But I got a NotSupportedException as below. How can I fix this?

    NotSupportedException: /Users/builduser/buildslave/unity/build/Tools/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(21) : Unsupported internal call for IL2CPP: DynamicMethod::create_dynamic_method - System.Reflection.Emit is not supported.
     
  38. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    IL2CPP = Itermediate Language to C-plus-plus. It's Unity's compiler that goes from IL to native.
     
    Zaelot likes this.