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 Reader / Writer / Modifier API [Tier 1]

Author
Message
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 15th Feb 2017 16:34 Edited at: 15th Feb 2017 16:50
*** IGNORE , sorted it out thanks ****

Hi,

Must admit I'm very rusty with AppGameKit but I'm just re-coding the RUBE importer for AGK2 and thought it would be good if I can use this great bit of code, which will allow me to import more information for generating physics worlds.

https://forum.thegamecreators.com/thread/218808#msg2596737

Just don't seem to be able to get the values out, I've attached a sample export json file , looking for some pointers to get me started.

just trying to get the root level objects atm, stepsPerSecond , allowSleep for example. Any pointers much appreciated.

Thanks in advance

Stuart

Attachments

Login to view attachments
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 16th Feb 2017 07:58
I am pleased about everyone who works with my api.

Have fun with it.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Feb 2017 23:10 Edited at: 16th Feb 2017 23:12
I have another challenge I can't quite work out...arrays inside arrays. Here is a section of the JSON:



I have got the Race successfully, but I don't know how to specify the path to the sub-array (jString() is a simple function to simplify the calls to the JSON functions):



Any help appreciated!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 17th Feb 2017 03:07
The RUBE importer will be definitely very helpful for all of us. If anyone could help TrezSoft to get the values out, will be much appreciated.

Richard.
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 17th Feb 2017 07:44 Edited at: 17th Feb 2017 07:49
I have attached your code snippet two comments and changed a line. Have not tested. But it should work.

To say it briefly, change the line -
dSectionRacer = jsonGetArray(gGame.data, "Races(" + str(i) + ")", "Racers")
to
dSectionRacer = jsonGetObjectEntry(gGame.data, race, "Racers")


I know it is confusing. There are functions that do almost the same. Such as - jsonGetArray and jsonGetObjectValue. jsonGetArray tests only if the result is an array.

Is probably because I'm not so familiar with AGK-Basic. My main programming language is C / C ++. There I also wrote a JSON-lib.
Access to the elements of the file is much easier. It will look like that.

gGame.data[ "Races" ][ i ][ "Racers" ][ j ][ "sponsor" ].AsString ();

Unfortunately is not possible in AGK.

P.S.
I have still one question. Why do you store integer and floats as strings? Or do not you create the JSON file itself?
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Feb 2017 08:29 Edited at: 17th Feb 2017 08:30
Many thanks, that makes sense I just tested and it works perfectly.
My main language is C# so I understand your challenge making a JSON library with the limitations of Basic.

Quote: " Why do you store integer and floats as strings? Or do not you create the JSON file itself?"

The data is extracted from an Excel Workbook which is 8 worksheets and dozens of parameters per sheet (not a linear list). I have opted to use a simplified method to export data, and manage the data types in the receiving systems.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Attachments

Login to view attachments
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Feb 2017 00:11 Edited at: 19th Feb 2017 05:18
It would be cool if you could create a json document from a json-formatted string instead of just a file. Then I could pull data from the web.

A few other things I'm curious about. In your example for loading a document:



But looking at the code for jsonLoad, you create a document inside that function. So wouldn't one of those be redundant?

And then, also in the jsonLoad function, you call __json_skip_whitespace(docID) right before calling __json_parse_object. But in that parse function the very first thing it does is call __json_skip_whitespace, thus that function is being called twice in a row.



Oh, and I can't get any examples to work. I can't even create a document without error.

Basic example:


Error:


Line 113:

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Feb 2017 14:22
@Phaelax doc isn't an integer, it's a jsonDocument type.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 19th Feb 2017 15:21
I think it is an integer
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Feb 2017 16:16
BatVink wrote: "@Phaelax doc isn't an integer, it's a jsonDocument type."


According to the changes in the zip file I downloaded.

* Change. The JSON-Document must now be an integer and no longer from the TYPE jsonDocument.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 19th Feb 2017 17:29
Hi,

I'm currently using it like this:


BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Feb 2017 18:23
OK, my bad.
Ironically, this change was made on my suggestion!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 20th Feb 2017 01:14 Edited at: 20th Feb 2017 01:22
Phaelax wrote: "A few other things I'm curious about. In your example for loading a document:
...
But looking at the code for jsonLoad, you create a document inside that function. So wouldn't one of those be redundant?
"

In the example, jsonCreateDocument would be redundant. Of the lib rather not.
So I had not changed it in the example. Initially, the jsonCreateDocument call was not present in the jsonLoad. So, loading a document would be right now -

And to create a s document -


Phaelax wrote: "And then, also in the jsonLoad function, you call __json_skip_whitespace(docID) right before calling __json_parse_object. But in that parse function the very first thing it does is call __json_skip_whitespace, thus that function is being called twice in a row."

Again, the code has gradually evolved. If you follow the course of the code one recognizes the function __json_parse_value also __json_skip_whitespace is called. You see I wanted to be sure that no whitespace is available at this place . I probably did not remove it, because it works as it is now.
Maybe I'll change that.

Phaelax wrote: "Oh, and I can't get any examples to work. I can't even create a document without error."

I had not planned that you can use your own id. So always use the id you get from create or load the document.

Phaelax wrote: "It would be cool if you could create a json document from a json-formatted string instead of just a file. Then I could pull data from the web."

Good point. I will implement it soon.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
Scotty1973
AGK Backer
13
Years of Service
User Offline
Joined: 2nd Jun 2011
Location: Burton-on-Trent, uk
Posted: 20th Feb 2017 16:06 Edited at: 20th Feb 2017 22:42
Hi

Great and helpful code, but I'm a bit slow on JSON stuff and have been struggling to read in the data. So far I have:-


as my JSON file

and in AppGameKit I have:-


I am having a problem reading the data"1, 2, 3, 4, 5, 33, 34, 35 ... ] line. I believe it to be in an array of the layers (currently only got one for testing)
I have not found out how to access this and any help would be appreciated.

Many thanks

Scotty.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Feb 2017 16:47
Is that all of the JSON data? If so, it is invalid.

Use a tool like this to check your JSON.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Scotty1973
AGK Backer
13
Years of Service
User Offline
Joined: 2nd Jun 2011
Location: Burton-on-Trent, uk
Posted: 20th Feb 2017 22:46
Hi BatVink

Used the tool to check and it comes out as valid JSON.

It was exported from tiled map editor.

Scotty
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Feb 2017 08:07
I'm no expert either, but I think the issue is that layers is an array (with one element) and data is an array inside an array. So you first need to:

data = jsonGetObjectEntry(doc, layers, "data")

Then you can get your data:

entry=jsonGetArrayEntry(doc,data,0)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 21st Feb 2017 09:56
Hi,

I found that I had to get the object rather that the array for the internal arrays, may be wrong but it works not had chance to look any deeper.

Code below should do what you need:

MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Feb 2017 09:58 Edited at: 21st Feb 2017 10:03
Ok, BatVink is right so far. But is not the only one. The sequence is not the best.

Here step by step.

First get the array 'layers'
layer_array=jsonGetArray(doc,"","layers")

now get a layer from the layer_array
layer=jsonGetArrayEntry(doc, layer_array, n) // n stands for the index in layer_array. In your example, there is only one layer. Thus n should be 0.

So we know layer is an object. And we can receive data from this object.
data = jsonGetObjectEntry(doc, layer, "data") // the return value is always a valueId. That can be a string, float, integer, array or an object.

name_value = jsonGetObjectEntry(doc, layer, "name")
name$ = jsonGetValueAsString(name)


You can now iterate through the 'data'-array.
count = jsonGetValueCount(doc, data)
for i=0 to count
jValue As Integer
jValue = jsonGetArrayValue(doc, data, i)
v_int = jsonGetValueAsInteger(doc, jValue)

// or in one line
// v_int = jsonGetValueAsInteger(doc, jsonGetArrayValue(doc, data, i))

next

i hope this helps

EDIT:
oops, too slow. Trez has the right solution. ( i think. )
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
Scotty1973
AGK Backer
13
Years of Service
User Offline
Joined: 2nd Jun 2011
Location: Burton-on-Trent, uk
Posted: 21st Feb 2017 17:34
Hi

Many thanks to BatVink, Trezsoft and madbit, works well now.

Like I say i'm a complete novice to JSON and was struggling like mad at it.


Thank you again

Scotty
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 23rd Feb 2017 10:15
New release !!!
In the first post are more information and the new download. Codebase has been updated as well.

The most important is a new function (jsonCreateFromString) proposed by Phaelax. Creating a JSON document with a string.

That should look like this.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
Wilf
Valued Member
18
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 23rd Feb 2017 15:48 Edited at: 23rd Feb 2017 16:12
Hi MadBit, thanks for continuing to develop this API. I'm getting an error when loading a spine animation file which passes the json validation test.

Quote: "
Error: Failed to get byte from memblock 100001, offset 250287 is greater than memblock size 250287 in json.agc at line 894
"


250287 is returned as the memblock size by GetMemblockSize( __json_g_documents[docID].memid )

Heres the json:



EDIT: Pretty simple fix. Store the size of the memblock and only get the next byte if the memptr is less than that value.

- Add memsize as integer to type jsonDocument
- Add this line to jsonLoad() after creating the memblock: __json_g_documents[docID].memsize = GetMemblockSize( __json_g_documents[docID].memid)
- Amend __json_read_next_byte(docID As Integer) with an if then comparing the sizes:

george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 23rd Feb 2017 15:52
Quote: "New release !!!"

I've already used your library in my project.
Do you believe that if I use the new release, I will face some issues?
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 24th Feb 2017 10:50 Edited at: 24th Feb 2017 10:50
@Wilf
Yes thanks for the hint. This happens when the json file behind the last brace does not have a whitespace.

I will gladly take your changes. Thanks for that.

george++ wrote: "Do you believe that if I use the new release, I will face some issues?"

The changes are no so big.
It is up to you if you use the new or the old release. The error described above was shown only in ErrorMode: 2 ( SetErrorMode ( 2 ) ). But now it is fiexed.

Bug fix release in the first post.

I hope everything is going well now.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
Center
7
Years of Service
User Offline
Joined: 16th Apr 2017
Location:
Posted: 20th Apr 2017 00:56
Hey MadBit,
thx for this nice little json handler. I'm a newbee in AGK2 programming and you save me lots of hour's to write it on my own. I'll use it for my first game to save and load levels.
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 20th Apr 2017 12:17
Thanks a lot for using my lib.

Welcome to the community.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 20th Apr 2017 16:25 Edited at: 20th Apr 2017 16:26
Hey,
I just wanted to mention that you can not just truncate the float after the first occurrence of "0" as for example 10 is a valid float in AppGameKit and jsonCreateFloatValue() creates a jsonValue string with just a "1" from it
I know, you probably should write 10.0 but that's not user friendly ...not like the rest of your code

Also I don't get the jsonCreateFromString() working

jsonLoad works fine

anyway... I appreciate your nice library

Using AGKv2 Tier1
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 20th Apr 2017 21:23
Hmm, I do not know what you mean.
You can create a float value so
jsonSetObjectValue(doc, "", "Float1", jsonCreateFloatValue(10), 1)
or so
jsonSetObjectValue(doc, "", "Float2", jsonCreateFloatValue(10.0), 1)

This little program


generates this json file


Or did I misunderstand you?
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 20th Apr 2017 22:16 Edited at: 20th Apr 2017 22:18
You are right !
I tested that without the function surrounding:

The value as float makes it work

You have an Idea what could cause doc=jsonCreateFromString(-1,String$) from not working while doc=jsonLoad(-1,File$) does ?
Both are the same strings but I need to write a file before using jsonLoad ofc

Also I am a bit jealous:
MadBit wrote: "The most important is a new function (jsonCreateFromString) proposed by Phaelax"

I tried a broad hint on that
Janbo wrote: "Is it right that currently there is no way to create a doc reference from a string directly
If I had no access to the code I must save my string, I got from the API, to a json file and then load it again with JsonLoad(doc,File) right ?"

Using AGKv2 Tier1
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 20th Apr 2017 23:10 Edited at: 20th Apr 2017 23:11
janbo wrote: "Also I am a bit jealous:
MadBit wrote: "The most important is a new function (jsonCreateFromString) proposed by Phaelax"

I tried a broad hint on that
Janbo wrote: "Is it right that currently there is no way to create a doc reference from a string directly
If I had no access to the code I must save my string, I got from the API, to a json file and then load it again with JsonLoad(doc,File) right ?""


Oh I'm sorry. I must have overlooked this.

janbo wrote: "You have an Idea what could cause doc=jsonCreateFromString(-1,String$) from not working while doc=jsonLoad(-1,File$) does ?
Both are the same strings but I need to write a file before using jsonLoad ofc
"

So, I've tested it again.
This code works as it should.



With this json file.


What happens?
Do you have an example?
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 21st Apr 2017 14:11 Edited at: 21st Apr 2017 14:12
Thanks your example pointed out that I had not the newest version.
It Works now for some json Strings but for some its the same as before

I tested my files in your small example and it seems that something in the files is wrong

I get this Error:

Using AGKv2 Tier1

Attachments

Login to view attachments
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Apr 2017 16:05 Edited at: 21st Apr 2017 16:15
Wow, I do not know what's going on. But what can I say, it is not to your json-file (I think) and also not to my lib.
I have your file validated and it seems all ok. I have tested your json file with this little code (without my lib) and it throws an error message every time.
(Text format were ANSI, UTF-8 and UTF-8 without BOM). Maybe a BUG in AppGameKit?

code:



Validated JSON-file
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Apr 2017 16:05 Edited at: 21st Apr 2017 16:12
Sorry tripple post
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 21st Apr 2017 16:11 Edited at: 21st Apr 2017 16:12
Sorry tripple post
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine

Attachments

Login to view attachments
MadBit
VIP Member
Gold Codemaster
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 23rd Apr 2017 15:25
Haha, I've figured it out now. Your JSON file uses the German umlauts (ä, ö and ü).
AGK obviously can not process the umlauts.
I exchanged them for ae, oe and ue.
Now there is no error message.
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Pixie-Particle-Engine
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 23rd Apr 2017 22:07
Thank you MadBit,
That solved one problem for one json file of my smart mirror project.
But I also retrieve a list of my Friends from the Steam API and this list wont get parsed.
I changed all umlauts to its equivalent and there is no error but also no result.

I send you the list per PM

Using AGKv2 Tier1

Login to post a reply

Server time is: 2024-11-23 15:18:00
Your offset time is: 2024-11-23 15:18:00