Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Gaiyamato · Aug 21, 2015 at 12:22 PM · jsoncasting

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;
 }

Comment
Add comment · Show 4
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 Gaiyamato · Aug 21, 2015 at 12:31 PM 0
Share

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?

avatar image Landern · Aug 21, 2015 at 12:40 PM 0
Share

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?

avatar image fafase · Aug 21, 2015 at 12:43 PM 0
Share

Damn and that looks like an airport control system with a faulty json. $$anonymous$$akes you think twice before flying....

avatar image Landern · Aug 21, 2015 at 12:45 PM 1
Share

In Australia even the software tries to kill you.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

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 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.

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 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)

Comment
Add comment · Show 3 · 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 fafase · Aug 24, 2015 at 09:25 AM 0
Share

$$anonymous$$aybe consider using another parser.

avatar image fafase · Aug 24, 2015 at 10:09 AM 0
Share

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.

avatar image Gaiyamato · Aug 25, 2015 at 11:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


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