SaladLab / Json.Net.Unity3D

Forked Newtonsoft.Json to support Unity3D
MIT License
918 stars 170 forks source link

JsonReaderException when json contains empty array #13

Open Josk opened 8 years ago

Josk commented 8 years ago

Hi, We have a problem with your package. Our json looks like that:

{ "status": 1, "errorCode": [], "message": "aircraft.listComplete", "fullErrorMessage": [], "scriptDateTime": { "date": "2016-08-19 14:39:29.000000", "timezone_type": 3, "timezone": "UTC" }, "inGameDateTime": { "date": "2001-04-22 15:56:54.000000", "timezone_type": 3, "timezone": "UTC" }, "irlDateTime": { "date": "2016-08-19 14:39:29.000000", "timezone_type": 3, "timezone": "UTC" }, }

When parsing, the code throw an exception:

JsonReaderException: Unexpected character encountered while parsing value: [. Path 'errorCode', line 1, position 25. Newtonsoft.Json.JsonTextReader.ReadNumberValue (ReadType readType) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonTextReader.cs:931) Newtonsoft.Json.JsonTextReader.ReadAsInt32 () (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/JsonTextReader.cs:424) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonContract contract, Boolean hasConverter) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:2238) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) (at C:/Project/Github/Json.Net.Unity3D/src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:2389)

How can I solve it?

IceflowRE commented 8 years ago

an empty array is not the problem :)

NOTE: no exception catcher ;)

start.cs

using UnityEngine;
using System.IO;
using Newtonsoft.Json;

public class start : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Data data = JsonConvert.DeserializeObject<Data>(File.ReadAllText("./data.json").ToString());
        UnityEngine.Debug.Log(data.status);
        UnityEngine.Debug.Log(data.errorCode);
        UnityEngine.Debug.Log(data.message);
        UnityEngine.Debug.Log(data.fullErrorCode);
        UnityEngine.Debug.Log(data.date);
    }
}

Data.cs

using Newtonsoft.Json;
using System.Collections.Generic;

public class Data {
    [JsonProperty(PropertyName = "status", Required = Required.Always)]
    public int status;
    [JsonProperty(PropertyName = "errorCode", Required = Required.Always)]
    public List<int> errorCode;
    [JsonProperty(PropertyName = "message", Required = Required.Always)]
    public string message;
    [JsonProperty(PropertyName = "fullErrorCode", Required = Required.Always)]
    public int[] fullErrorCode;
    [JsonProperty(PropertyName = "date", Required = Required.Always)]
    public string date;
}

data.json

{
    "status": 1,
    "errorCode": [],
    "message": "aircraft.listComplete",
    "fullErrorCode": [],
    "date": "2016-08-19 14:39:29.000000"
}

Due to lack of information i cant tell you where the problem is.

veblush commented 8 years ago

I'm not sure there is a problem or not because your start.cs works well under UnityEditor writing following output.

1
System.Collections.Generic.List`1[System.Int32]
aircraft.listComplete
System.Int32[]
2016-08-19 14:39:29.000000

If there is an error around this code, please let me know your environment :smile:

IceflowRE commented 8 years ago

I should write more clear ;D With my code there shouldnt be an error. It was just a simple code, to show how he can parse his json. I think he has problems in his code, but didnt post it.

veblush commented 8 years ago

@IceflowRE Oh I missed it! Good explanation :+1:

@Josk From callstack, I suspect that a type of the field errorCode of your class may be int or something scalar not array and it causes a parse error because scalar field cannot consume array at all.