Git Product home page Git Product logo

Comments (14)

tmpsantos avatar tmpsantos commented on September 17, 2024 4

Had the same issue with Unity 5.4. Removing { get; set; } and making attributes public fields partially solved the problem.

I think @veblush is right about IL2CPP wrongly stripping code. Adding the assemblies containing the serializable classes to link.xml telling IL2CPP to preserve it fixed for me.

diff --git a/example/playground/Assets/Plugins/link.xml b/example/playground/Assets/Plugins/link.xml
index 0e3e596..ae3ada0 100644
--- a/example/playground/Assets/Plugins/link.xml
+++ b/example/playground/Assets/Plugins/link.xml
@@ -1,4 +1,6 @@
 <linker>
+  <assembly fullname="MyAssembly" preserve="all"/>
   <assembly fullname="System">
     <type fullname="System.ComponentModel.TypeConverter" preserve="all"/>
     <type fullname="System.ComponentModel.ArrayConverter" preserve="all"/>

from json.net.unity3d.

dlapcenko avatar dlapcenko commented on September 17, 2024 3

SOLVED! the problem was with my object fields having {get; set;} accessors. as soon as i removed them - everything works.

issue could be closed, but perhaps not deleted, since someone else might experience same issue? Up to you, veblush. In any case - thanks for your fork!

from json.net.unity3d.

dlapcenko avatar dlapcenko commented on September 17, 2024 1
    [System.Serializable]
    public class CharacterListItem
    {
        public int Id;
        public string Name;
        public int Level;
        public string Class;
        public string Sex;
    }

there you go, simple, but them getter-setters made all the difference. By your accounts, it should've been working with setters?

from json.net.unity3d.

veblush avatar veblush commented on September 17, 2024 1

Thanks for sharing it. BTW following code was tested on Unity 5.3.5f1 targeting to iOS and worked successfully. There should be some differences between mine and yours.

[System.Serializable]
public class CharacterListItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Level { get; set; }
    public string Class { get; set; }
    public string Sex { get; set; }
}

void TestJson()
{
    var json = "{\"Id\":51,\"Name\":\"padre\",\"Level\":0,\"Class\":\"Vampire\",\"Sex\":\"F\"}";
    var c = JsonConvert.DeserializeObject<CharacterListItem>(json);
    WriteLine(c.Id + " " + c.Name);
}

Unity strips out some codes that are thought to be unnecessary by heuristic rules which have been
changed a few times. Could you tell me your unity version and any idea around this code?

from json.net.unity3d.

KellanHiggins avatar KellanHiggins commented on September 17, 2024

If you can't get it working and are using Unity 5.3, you could use Unity's Json.net serializer. JSONUtility.FromJson(...). It's not as extensive as this fork but it will work for simple objects.

from json.net.unity3d.

dlapcenko avatar dlapcenko commented on September 17, 2024

Thanks. I was quite happy this fork gave the generic dictionary serialization functionality so I'd love to stick with it. Hopefully something could be done to fix the issue. I'll be trying to gather more debug info as soon as I get my hands on it.

from json.net.unity3d.

KellanHiggins avatar KellanHiggins commented on September 17, 2024

Oh yeah, if you need that then unity's solution won't work.

from json.net.unity3d.

veblush avatar veblush commented on September 17, 2024

Could you show me error message on iOS error case? There might be jit issues on this case and it's quite easy to fix this if this is related with jit problem. Check this article and part The game crashes with the error message "ExecutionEngineException: Attempting to JIT compile method ‘SometType`1:.ctor ()’ while running with –aot-only.

from json.net.unity3d.

dlapcenko avatar dlapcenko commented on September 17, 2024

Funnily enough there is no error message whatsoever. If I print the unserialized received object it is in correct JSON format, but when i try to deserialize it just does not work whatsoever, the lines with JSONConvert.Deserialize do not do anything, as well as the *.Serialize ones. and there is no exception whatsoever. I am sure your .dll is in the correct place, since it works on desktop client build. But this weird behaviour on iOS is concerning.. :/

from json.net.unity3d.

dlapcenko avatar dlapcenko commented on September 17, 2024

screen shot 2016-07-11 at 7 10 41 pm

Top debug line is the JSON object that I am receiving. second line (which states NULL) is trying to access the NAME field post-deserialization. No exception whatsoever.. simply a null object.

Any idea? :/ same behaviour in iOS simulator as well. possibly the binary is not being included properly? Or because I have not set up the JSON flags on any serialized class or property? (didn't do it before since for desktop everything works flawlessly without it)

Thanks for your time!

from json.net.unity3d.

veblush avatar veblush commented on September 17, 2024

Good to hear that it's solved. 😄 It's suspicious that IL2CPP striped out ctor of T or setter or name property. For further investigation, could you show me the definition of T which holds name property.

from json.net.unity3d.

blake-r avatar blake-r commented on September 17, 2024

@tmpsantos I had enough preserve Newtonsoft.Json assembly only. I tried preserve only ContainerWrapper and DictionaryWrapper, but it was unsufficient.

<assembly fullname="Newtonsoft.Json" preserve="all"/>

from json.net.unity3d.

AngelKyriako avatar AngelKyriako commented on September 17, 2024

@veblush Any news on this ?

I am seeing this behaviour as well in webGL build with either unity 2017.4 or 2018.2.

While the AOT newtonsoft assembly works as expected within the editor, in a webGL build a simple class with public get; set; properties always serializes to empty object.

I will do some more tests on an empty project to check it out, but I was wondering if anything has been done to resolve this.

from json.net.unity3d.

Harriet92 avatar Harriet92 commented on September 17, 2024

Hi guys!
I have this strange issue only on iOS - when I'm trying to serialize an object with an interface array it just stops without throwing any errors - I have debug logs before and after serialization and the first log always appears on console, and the other one only when I do not have the IInterface[] field in the dto object.

[code=CSharp]private JsonSerializerSettings allNameHandlingSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, NullValueHandling = NullValueHandling.Ignore };

public string Serialize<T>(T objectToSerialize)
{
    Debug.Log("Serializing...");
    var str = JsonConvert.SerializeObject(objectToSerialize, Formatting.Indented, allNameHandlingSettings);
    Debug.Log("Finished serializing");
    return str;
}[/code]

works for

[code=CSharp][Serializable]
private class DtoTest
{
public string TestString;
}[/code]

silently fails before writing the second log for:

[code=CSharp][Serializable]
private class DtoTest
{
public string TestString;
public IComponent[] components;
}[/code]

Do you have any ideas? Apart from that it works perfectly...

from json.net.unity3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.