- Home /
EasyJSON Casting Error
Working with Easy JSON and I have run into a problem, I have a complex json structure (not my fault, I don't control it.). Working entirely in JS for compatibility issues with another asset I am using later on.
I receive this error:
InvalidCastException: Unable to cast <{"flight":[{"actual_blocks_time":"04/08/2015 07:07:00",
"actual_time":"04/08/2015 07:10:00",
"aircraft_type":"320",
"alternate_remark_one":"Landed",
"alternate_remark_two":"Landed",
"airline":"JQ",
"codeshare_airlines":["QF"],
"codeshare_flights":["QF5701"],
"estimated_time":"04/08/2015 07:10:00",
"flight_number":"JQ701",
"leg":"A",
"locations":["Melbourne"],
"operational_date":"04/08/2015",
"primary_remark":"Landed",
"scheduled_time":"04/08/2015 07:15:00",
"terminal":"1","trip_number":0}]}> (with type = System.Collections.Generic.Dictionary`2[System.String,EasyJSON.fsData]) to type System.Collections.Generic.List`1[EasyJSON.fsData]
EasyJSON.fsData.Cast[List`1] ()
EasyJSON.fsData.get_AsList ()
EasyJSON.Internal.fsIEnumerableConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType)
EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.Internal.fsReflectedConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType)
EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.Internal.fsReflectedConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType)
EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result)
EasyJSON.fsSerializer.TryDeserialize[FlightRoot] (EasyJSON.fsData data, .FlightRoot& instance)
EasyJSON.Serializer.Deserialize[FlightRoot] (System.String json)
FIDSLoader.GetLatestFile () (at Assets/FIDSLoader.js:76)
FIDSLoader.Start () (at Assets/FIDSLoader.js:20)
I have tried a variety of solutions however none seem to work.
My codebase is very small so I will add it here.
#pragma strict
import System.DateTime;
import System.IO;
import System.Collections.Generic;
import EasyJSON;
public var path:String = "C:\\fids\\";
private var output:String = "";
public var dates:List.<double> = new List.<double>();
public var filenames:List.<String> = new List.<String>();
public var timer:float = 0;
public var check_interval:float = 3200;
public var ui:UIControl;
public var flightdata:FlightRoot;
function Start ()
{
//path.Replace('\\', Path.DirectorySeparatorChar); //for multi-OS compatibility only. Not required right now
GetLatestFile();
ui.PromtUpdate();
}
function Update ()
{
if(timer >= check_interval)
{
GetLatestFile();
ui.PromtUpdate();
timer = 0;
}
else
{
timer+=Time.deltaTime;
}
}
function GetLatestFile()
{
try
{
var info = new DirectoryInfo(path);
var fileInfo = info.GetFiles();
var nojson:boolean = true;
if(fileInfo.Length > 0)
{
for(var file in fileInfo)
{
if(file.Extension)
{
if(file.Extension.Equals(".json"))
{
filenames.Add(file.Name);
dates.Add(DateTimeToUnixTimestamp(file.CreationTime));
nojson = false;
}
}
else
{
nojson = false;
}
}
if(nojson)
{
Debug.Log("No Valid data file");
}
else
{
//find the laest and load it!!
var filename:String = filenames[GetNewestPosition()];
output = System.IO.File.ReadAllText(path+filename);
//convert to a json file
flightdata = new Serializer.Deserialize.<FlightRoot>(output);
//delete all except latest.
for(var oldfile in fileInfo)
{
if(oldfile.Extension)
{
if(oldfile.Extension.Equals(".json"))
{
if(!oldfile.Name.Equals(filename))
{
//baweted!
System.IO.File.Delete(path+oldfile.Name);
}
}
}
}
//clear arrays
dates.Clear();
filenames.Clear();
}
}
else
{
Debug.Log("No Valid data file");
}
}
catch(e:DirectoryNotFoundException)
{
Debug.Log("Invalid Directory, check directory settings");
}
}
public function GetNewestPosition():int
{
var highest:double = 0;
var highestpos:int = 0;
var currentpos:int = 0;
for(var date in dates)
{
if(date >= highest)
{
highestpos = currentpos;
}
currentpos++;
}
return highestpos;
}
public function GetData():FlightRoot
{
return flightdata;
}
public static function DateTimeToUnixTimestamp(dateTime:System.DateTime):double
{
return (dateTime - new System.DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
}
public class FlightRoot
{
var flight_set:FlightSet;
}
import System.Collections.Generic;
public class FlightSet
{
var airport:String;
var date:String;
var flights:List.<Flight>;
var time_window:String;
}
import System.Collections.Generic;
public class Flight
{
var actual_blocks_time:String;
var actual_time:String;
var aircraft_type:int;
var alternate_remark_one:String;
var alternate_remark_two:String;
var airline:String;
var codeshare_airlines:List.<String>;
var codeshare_flights:List.<String>;
var estimated_time:String;
var flight_number:String;
var leg:String;
var locations:List.<String>;
var operational_date:String;
var primary_remark:String;
var scheduled_time:String;
var terminal:int;
var trip_number:int;
}
The actual error it is giving me though is, it is unable to cast from a Dictionary to a List. What tells you it is a stack overflow issue?
Whats up with location?, if it's suppose to be a list of string it only has a closing/ending square bracket and is missing an opening one to indicate a collection for that property. Was the json hand created?
Damn and that looks like an airport control system with a faulty json. $$anonymous$$akes you think twice before flying....
Answer by fafase · Aug 21, 2015 at 12:36 PM
If the json snippet is the whole file, it is not a valid json
"locations":"Melbourne"],
should be
"locations":["Melbourne"],
Copy paste your snippet into some json validator like jsonlint and it will let you know that it is wrong there.
Answer by Gaiyamato · Aug 23, 2015 at 06:32 PM
The issue with locations is just something the formatting here has done, it happened when I used the code tag around it, it also tried to turn a lot of " into ' ' . the json is fine in reality.
Answer by Gaiyamato · Aug 23, 2015 at 06:32 PM
Reposting the error here without the formatting issues it had in my OP
InvalidCastException: Unable to cast <{"flight":[{"actual_blocks_time":"04/08/2015 07:07:00","actual_time":"04/08/2015 07:10:00","aircraft_type":"320","alternate_remark_one":"Landed","alternate_remark_two":"Landed","airline":"JQ","codeshare_airlines":["QF"],"codeshare_flights":["QF5701"],"estimated_time":"04/08/2015 07:10:00","flight_number":"JQ701","leg":"A","locations":["Melbourne"],"operational_date":"04/08/2015","primary_remark":"Landed","scheduled_time":"04/08/2015 07:15:00","terminal":"1","trip_number":0}]}> (with type = System.Collections.Generic.Dictionary`2[System.String,EasyJSON.fsData]) to type System.Collections.Generic.List`1[EasyJSON.fsData] EasyJSON.fsData.Cast[List`1] () EasyJSON.fsData.get_AsList () EasyJSON.Internal.fsIEnumerableConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType) EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.Internal.fsReflectedConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType) EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.Internal.fsReflectedConverter.TryDeserialize (EasyJSON.fsData data, System.Object& instance, System.Type storageType) EasyJSON.fsSerializer.InternalDeserialize_4_Converter (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_4_Cycles (EasyJSON.fsData data, System.Type resultType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_3_Inheritance (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_2_Version (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.InternalDeserialize_1_CycleReference (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.TryDeserialize (EasyJSON.fsData data, System.Type storageType, System.Object& result) EasyJSON.fsSerializer.TryDeserialize[FlightRoot] (EasyJSON.fsData data, .FlightRoot& instance) EasyJSON.Serializer.Deserialize[FlightRoot] (System.String json) FIDSLoader.GetLatestFile () (at Assets/FIDSLoader.js:76) FIDSLoader.Start () (at Assets/FIDSLoader.js:20)
On a second look at the stack tracer, it seems it would be trying to convert some data to list and fails at that. $$anonymous$$aybe you could sub parse the file. Ins$$anonymous$$d of parsing the whole thing at once, you do part by part until you manage to reproduce the issue.
If you would have access to the EasyJSON code, you could place some debugging lines here and there to figure out when and where it fails.
No access to the EasyJSON Code unfortunately. The Developer was initially forthco$$anonymous$$g, but has not responded to email for a few days. I linked this thread to the developer.
I am thinking I might write a non-generic deserializer specific to this JSON content myself. I was trying to avoid all that work hoping to get something out of the box, but JSON in Unity leaves a lot to be desired even today. EasyJSON is so far the best package I have come across for simple JSON functionality, however I think it might be built more in $$anonymous$$d for dealing with lots of very small JSON files associated with game objects - such as you would get in a game (funny that!).
Your answer
Follow this Question
Related Questions
Parseing JTokens 0 Answers
Getting data out of Dictionary 0 Answers
Parsing JsonData throws error when value is null, what to do? 0 Answers
JSON-Serializer (Renewed open) 0 Answers
Whew, I'm stuffed! (Hunger Script) 1 Answer