- Home /
JSONFX, Reading JSON Object
Hi,
I suppose this question is for people who have actually worked with JSonFX. I've downloaded a version of JSonFX specific to unity: here
From what I understand... you create a class the inherits from an interface (JsonConverter), which will convert the json dictionary into an object using the overridden ReadJson() function. I've created two classes that convert the passed dictionary into a C# class object. Both converter class look basically the same, but will return different types of objects, but for some reason the ReadJson function is only being called on one Converter class, and not the other.
Does anyone know of a reason why this would be happening? I have been mucking with this all day, and I can't find a reason as to why the ReadJson() func of one class is called and not the other. Both CanConvert() funcs return true and then false because it calls CanConvert on the type, then on the dictionary for some reason, but that's besides the point, which is that BOTH CanConvert() functions return true, then false, BUT afterwords ClassA's ReadJson() is called, but ClassB's ReadJson() wont be called.
I hope I haven't confused you guys too much.
Also, side question, is there a way to only grab the Dictionary of the json object?
Thank you in advance for any help.
Answer by mckamey · Feb 13, 2013 at 07:03 PM
I know a lot about JsonFx but not a lot about the changes made for the Unity fork. The JsonConverter
stuff is new as JsonFx is supposed to be able to figure out the types and automatically do the type coercion itself (this is much improved in v2 rather than v1 which is what the Unity fork is from).
Just specify the type in JsonReader.Deserialize(Type)
to tell it do do type coercion. To get a dictionary instead of coercing to a type, don't specify a type (this is the default).
That being said, did you tell the reader about the converter via JsonReaderSettings.AddTypeConverter(JsonConverter)
? It probably knows about one via C#'s type inference, but not the other.
Hey mckamey. I figured out the issue, which was that the object that was co$$anonymous$$g through was co$$anonymous$$g in as an array, even if it was just an array of 1 object. So when it would try to deserialize, the array of json objects was passed, and it would crash, but not report and error or crash the game.
You said that JsonFX will automatically do the conversion... would that be for any type, or just primitives? (Could I create my own class and pass that as a type) If so, do you think it would be better to just use the regular jsonfx from here and just drop the fork i downloaded?
Also, since I found the issue, do I post my own answer? Or just update my question.
Your answer
Follow this Question
Related Questions
JSON and Javascript 0 Answers
jsonfx anomalies 0 Answers
JsonFX and Polymorphism? 1 Answer
Can anyone tell me what's wrong in this, I try to post values to API 0 Answers