Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Deserializing JSON Array (C# / JsonFx)

Discussion in 'Scripting' started by Jelling, Jan 27, 2013.

  1. Jelling

    Jelling

    Joined:
    Jan 27, 2013
    Posts:
    6
    I'm working with JsonFx to serialize some data but I get: "JsonTypeCoercionException: Only objects with default constructors can be deserialized. (Level[])" when I try to deserialize the data.

    I've tried adding a default constructor to the serialized class but I still get the error. Fwiw, I tried the different suggestions in a similar thread:
    http://forum.unity3d.com/threads/117256-C-deserialize-JSON-array

    Here's my code:

    Code (csharp):
    1.  
    2.  
    3. //C#
    4. using System;
    5. using UnityEngine;
    6. using System.Collections;
    7. using JsonFx.Json;
    8. using System.IO;
    9.  
    10. public class LoadLevel : MonoBehaviour {
    11.    
    12.    
    13.     string _levelFile = "levels.json";
    14.     Level[] _levels;
    15.    
    16.     void Start () {
    17.                
    18.         if (!File.Exists (_levelFile)){
    19.                                
    20.             // write an example entry so we have somethng to read
    21.             StreamWriter sw = File.CreateText(_levelFile);
    22.            
    23.             Level firstLevel = new Level();
    24.             firstLevel.LevelName = "First Level";              
    25.             firstLevel.Id = Guid.NewGuid().ToString();
    26.            
    27.    
    28.             sw.Write(JsonFx.Json.JsonWriter.Serialize(firstLevel));
    29.             sw.Close();
    30.            
    31.         }
    32.        
    33.            
    34.         // Load our levels
    35.         if(File.Exists(_levelFile)){
    36.        
    37.             StreamReader sr = File.OpenText(_levelFile);
    38.                    
    39.             _levels = JsonReader.Deserialize<Level[]>(sr.ReadToEnd());
    40.  
    41.         }
    42.                    
    43.     }
    44. }
    45.  
    46.  
    And here's the object it's serializing:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class Level {
    7.    
    8.     public string Id;
    9.     public string LevelName;
    10.    
    11.     public Level() {}
    12.    
    13. //  public string Id { get; set;}
    14. //  public string LevelName { get; set; }
    15. // 
    16. // 
    17. }
    18.  
    19.  
    20.  
    Any ideas?
     
    Last edited: Jan 27, 2013
  2. mckamey

    mckamey

    Joined:
    Dec 13, 2010
    Posts:
    10
    Not sure if this is the only issue but it does look like you are serializing a single `Level` object and then trying to read back an `Array` of `Level` objects. These two would serialize differently in JSON and it is probably having trouble trying to coerce an object into an Array class.
     
  3. Jelling

    Jelling

    Joined:
    Jan 27, 2013
    Posts:
    6
    Thanks but that wasn't it. I added a second item and had the same issue. So weird.
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    You got to leave all the serialization to JsonFx, and save the end result. Then, when you load the saved result and deserialize it with using JsonFx, it will work.

    (noticed you are writing a single object, but should write the array instead - if reading it as array later)

    Similar thread in the case of further questions.
     
  5. AustinK

    AustinK

    Joined:
    Dec 10, 2012
    Posts:
    86
    Where did you add the second level?

    The first level you created isn't being added to a level array either.

    Code (csharp):
    1.  
    2. Level firstLevel = new Level();
    3. firstLevel.levelName = "myLevelName";
    4. firstLevel.id = "myLevelID";
    5.  
    6. Level[] myLevels = new Level[1];
    7. myLevels[0] = firstLevel;
    8.  
    9. JsonFx.Json.JsonWriter.Serialize(myLevels);
    10.  
    11. Level[] DS_Levels = JsonReader.Deserialize<Level[]>(myLevels);
    12.  
    I can't know much without you posting your new code, but I think you're serializing each level individually, which will have a different structure than an array of Levels.
     
  6. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85
    I've been using jsonfx 1.4 in my game for a couple months and it has been working great, but sometime in the last couple weeks it has totally broken on iOS. I wish I knew exactly when but I expect it is whenever the last Unity version came out, which was a couple weeks ago IIRC.

    It still works fine in the editor but on iOS it fails when trying to deserialize (serialization still works) ints, uints, or floats. Strings still work.

    I know it's not my game code because I made a really simple test project with only the jsonfx dll file and a single script to test it. You can test serialization / deserialization of data structures with ints, uints, floats, and strings. Every test fails except for the string test.

    Is anyone still using jsonfx successfully on iOS with the latest Unity?

    I uploaded a very simple test project so you guys can easily test jsonfx with your version of Unity + iOS.
    https://github.com/tayl0r/jsonfxV1-UnityTest

    If anyone still has the version of Unity before Unity 4.01f2 I would really appreciate if you could give that project a test to see if it runs on your iOS device.
     
  7. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85