Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 16, 2020 at 03:59 PM by a88236 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by a88236 · Jul 15, 2020 at 08:48 AM · jsonil2cpp

,Cannot deserialize object of class from imported library by json.net

,In Unity project I have some class WsMessage for WebSocket interaction. This class located in my own library WebSocketModels.

 namespace WebSocketModels
 {
     [Serializable]
     public enum WsMessageType 
     { 
         System, Player, Challenge, DeclineChallenge, RemoveChallenge, Game, Move, Moves, Chat,
         Players, Challenges, Games, Clock                        
     }    
     
     [Serializable]
     public class WsMessage
     {
         public WsMessageType type { get; set; }
         public string data { get; set; }
 
         public WsMessage() { }
 
         public WsMessage(WsMessageType type, string data)
         {
             this.type = type;
             this.data = data;
         }
     }
 }

By some reason it cannot be deserialized using JSON.NET. I didn't see any errors. If i move this class from library directly to Unity project object of WsMessage creating normally. I use this simple command for get an object of WsMessage:

 WsMessage message = JsonConvert.DeserializeObject<WsMessage>(inputWsMessage);

I've met this problem after change my Unity player Scripting Backend to IL2CPP. On Mono everything was OK.

Example of JSON content

 {"type":10,"data":"[{\"id\":\"0d8648e4-ce15-4084-87f9-f3de2b5a9b32\",\"fromPlayer\":{\"id\":\"af76e7c3-27b2-4d05-bcd3-f4b41c3bb7ba\",\"name\":\"Aydar\",\"rating\":1600.0,\"isOnline\":false},\"color\":0,\"timeControl\":{\"time_range\":10,\"time_increment\":5,\"control_type\":0},\"toPlayer\":null}]"}
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image a88236 · Jul 16, 2020 at 03:58 PM 0
Share

Resolved by deleting getters, setters on public variable: public Ws$$anonymous$$essageType type { get; set; } public string data { get; set; }

change to: public Ws$$anonymous$$essageType type; public string data;

So, there are 3 points need to take attention when make serialization of object from 3rd party library when using IL2CPP: 1. No getters/setters 2. All public fields also must be [Serializable], like Ws$$anonymous$$essageType in my case 3. Class must have default constructor without parameters.

2 Replies

  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 15, 2020 at 11:16 AM

IL2CPP is an AOT platform (ahead of time compilation) so no dynamic code generations is possible on an AOT platform. There are modified forks of the original newtonsoft json.net that should work on AOT platforms. I have never used it but it's worth a try.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by a88236 · Jul 15, 2020 at 12:56 PM

Bunny83, thank you for your answer, but actually i am already use this version of json.net (re-installed again for sure, but no result). Also, as i mentioned, object deserializing normally when i moving class directly to Unity project, but not when this class locating in library.

Possibly, my library WebSocketModels not compiling for AOT. Is there some special requirements how to import library for AOT platform?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# JSON library 6 Answers

Install JsonFX 1 Answer

minijson pars 0 Answers

3D dynamic map generation 0 Answers

WebGL and Access-Control-Allow-Origin 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges