Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / JSON Object vs. Array Bug?

Author
Message
3Dski
4
Years of Service
User Offline
Joined: 8th Jan 2020
Location:
Posted: 26th Jan 2020 22:42
I have a simple JSOn file with a single object:

{
"id": 1,
"file": "bars.png"
}

It returns a compiler error on the Load() call:

main.agc:30: error: Type function "load" not recognised, built in function is .fromjson(string)

If I change the JSON to array format by enclosing the original content in square brackets the call to Load() works fine.

It's rather annoying when I know a have a single object worth of data to represent with JSON and it won't load it. This example test is small so I could call fromJSON(). The real code I want to work with is a larger file that needs to be read in.
This means I have to include the "[" and "]" and re-write the code to act like its has to work with an Array of the type, when I I have is a single item. I see this as a work-around, rather than correctly allowing the load() of a JSON object from a file. It's as though you're forcing fromJSON() to be used on any single object, even though it wouldn't make sense define it in with a String. I would think most use cases would require the read from a file, regardless if its an object or an array of objects.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 26th Jan 2020 22:54
It may not be the answer you want to hear but you could write a short program that reads the file
line by line and replaces { with [
MerryChristmas Everyone
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
MadBit
VIP Member
Gold Codemaster
14
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 27th Jan 2020 05:45
I don't know. But I think only the root element must be an array.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
3Dski
4
Years of Service
User Offline
Joined: 8th Jan 2020
Location:
Posted: 29th Jan 2020 04:10
Well, I don't mind the workaround of adding "[" and "]" to the JSON file, but it just weird : ) BTW, I did try to read the JSON in line by line and tried to cal. fromJSON() with that string and it didn't like something with that.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 29th Jan 2020 04:33 Edited at: 29th Jan 2020 04:34
You can convert a Json array to a type like so;


That way it will throw away the array and just leave the type
3Dski
4
Years of Service
User Offline
Joined: 8th Jan 2020
Location:
Posted: 2nd Feb 2020 20:13
Thanks, blink0k! That makes things a bit more logical, as it would be a pain to keep referring to properties as blahblah[0].whatever when you darn well know that there is only a single element! Plus, this is also more elegant than creating another variable to step away from the array reference; i.e., yetAnotherVariable = blahblah[0] ... yetAnotherVariable.whatever. Another function to throw into the toolkit : )

Login to post a reply

Server time is: 2024-03-28 20:32:39
Your offset time is: 2024-03-28 20:32:39