EnglishGermanFrenchRussianItalianSpanish
Log inRegister
 
[SCRIPT LIB] JSON parser v1.2 [2011-09-25]
Post new topic Reply to topic
View previous topic :: View next topic
Author Message
ThisIsHarsh





Joined: 19 Oct 2008
Posts: 1132 on topic

Thank you for registering your game
modified
PostPosted: Wed, 21. Oct 09, 00:44    Post subject: [SCRIPT LIB] JSON parser v1.2 [2011-09-25] Reply with quote Print

JSON parser

This is a library for use by scripters. It provides functions to parse JSON object definitions and to get/set values within JSON objects.

Download: JSON parser library spk (or zip)

Additional download link from xdownloads (plus google and onedrive mirror):







Introduction

JSON: JavaScript Object Notation is a lightweight general purpose data exchange format. It's basically an alternative to XML. If you aren't familiar with it, the examples below should make it pretty obvious, also there is a huge wealth of information to be found via google.

Features: The plugin includes a 'parse' script, which will take a string containing a JSON object definition and return what I call a jsonobj - basically a hierarchy of arrays, with the lowest level being 2-element key-value pair arrays. The next most important script is the 'get' script, which allows you to navigate the jsonobj to get a specific value. Then there is the 'set' script, allowing you to modify the value within the jsonobj's hierarchy. Finally there is the 'union' script, allowing you to join together two jsonobjs.

Terminology:
  • jsonobj : a complete hierarchy of objects, arrays and values.
  • object : a set of key-value pairs, where value can be either integer, string, object or array.
  • member : a specific key-value pair within an object.
  • element : a specific value at a given index in an array.


Examples

JSON object definition stored in t-file:
Code:

<t id="10000">
{
  "astring"  : "mystring",
  "anumber"  : 42,
  "anobject" : { "param1" : "myobject", "param2" : 99 },
  "anarray"  :
    [
      "str",
      11,
      { "id" : "myarrobj"},
    ],
}
</t>


Parsing object definition into a jsonobj:
Code:

$jsondef = read text: page=$page.id id=10000
$jsonobj = $nul -> call script 'plugin.JSON.parse' : json=$jsondef



Fetching values:
Code:

$string = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='astring' idx0=null idx1=null idx2=null

$objparam = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anobject.param2' idx0=null idx1=null idx2=null

$arrnum = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anarray[%s]' idx0=1 idx1=null idx2=null

$arrobj = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anarray[%s].id' idx0=2 idx1=null idx2=null



jsonobjs form a nested hierarchy:
Code:

$anobj.jsonobj = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anobject' idx0=null idx1=null idx2=null

$objparam = $nul -> call script 'plugin.JSON.get' : json object=$anobj.jsonobj member='param2' idx0=null idx1=null idx2=null


Special keyword 'length' for arrays:
Code:

$idx = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anarray.length' idx0=null idx1=null idx2=null

while $idx
  dec $idx =

  $val = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anarray[%s]' idx0=$idx idx1=null idx2=null
end



Arrays are stored as 1D arrays, so can be used normally as well as within jsonobj hierarchies:
Code:

$arr = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anarray' idx0=null idx1=null idx2=null

$idx = size of array $arr

while $idx
  dec $idx =

$val = $arr[$idx]
end

  • $idx = 0, $val = "str"
  • $idx = 1, $val = "11"
  • $idx = 2, $val is a jsonobj!


Setting:
Code:

= $nul -> call script 'plugin.JSON.set' : json object=$jsonobj member='anumber' value=223 idx0=null idx1=null

$plyship = [PLAYERSHIP]

= $nul -> call script 'plugin.JSON.set' : json object=$jsonobj member='anarray[%s]' value=$plyship idx0=1 idx1=null



Union - source is merged with target. If any members match, then target's values take precedence:
Code:

<t id="10001">
{
  "anobject" :
    {
      "param1"   : "unionme",
      "param2"   : 0,
      "newparam" : "newparameter"
    },
}
</t>

Code:

$new.jsondef = read text: page=$page.id id=10001
$new.jsonobj = $nul -> call script 'plugin.JSON.parse' : json=$jsondef

= $nul -> call script 'plugin.JSON.union' : target jsonobj=$jsonobj source jsonobj=$new.jsonobj

$obj.param1 = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anobject.param1' idx0=null idx1=null idx2=null

$obj.newparam = $nul -> call script 'plugin.JSON.get' : json object=$jsonobj member='anobject.newparam' idx0=null idx1=null idx2=null

  • $obj.param1 = "myobject"
  • $obj.newparam = "newparameter"


Limitations and general notes

  • JSON stadard types true, false and null are not supported. For true/false use integer 0/1. The null value is used as an error return in case, for example, you pass an invalid member to the 'get' script.

  • Badly formatted JSON can, very easily, cause an infinite loop lockup. You can enable debugging by modifying the line in plugin.JSON.parse. This will enable infinite loop detection and log a verbose status in log10000.txt. It will take much longer to run the json parser/get functions in debug mode.

  • All jsonobjs are passed by reference, no array cloning takes place.



Changelog
v1.2:
- Modified: union script will only append additional elements to json arrays, not all.
- Fixed: bug in reading numbers within json arrays.
v1.1:
- Modifed: incorporated Lit's suggested optimization, cheers for that.
- Fixed: accessing non-jsonobj array elements didn't work.
v1.0:
- inital release


_________________
There are 10 types of people in the S&M forums - those who understand binary, and those who don't.

Black holes are where God divided by zero.


Last edited by ThisIsHarsh on Sun, 2. Oct 11, 12:08; edited 3 times in total
Back to top
View user's profile Send private message
Lit





Joined: 17 Feb 2006
Posts: 45 on topic
Location: Russia
Thank you for registering your game
PostPosted: Sun, 10. Jan 10, 15:51    Post subject: Reply with quote Print

I fix multilanguage problem

This strange loop Wink :
Code:
017   $str = ''
018   
019   * First char is "
020   inc $idx =
021   
022   while [TRUE]
023   |skip if not $idx = $json.length
024   ||break
025   |
026   |$char = get substring of $json offset=$idx length=1
027   |
028   |if $char = '"'
029   ||break
030   |else
031   ||$str = sprintf: fmt='%s%s', $str, $char, null, null, null
032   |end
033   |
034   |inc $idx =
035   end
036   inc $idx =


Replace to:
Code:
017   * First char is "
018   inc $idx =
019   
020   $str = get substring of $json offset=$idx length=null
021   $str.length = find position of pattern '"' in $str
022   $str = get substring of $str offset=0 length=$str.length
023   $idx = $idx + $str.length + 1


Also, I think it will work faster than before.


_________________
***modified***
Back to top
View user's profile Send private message
ThisIsHarsh





Joined: 19 Oct 2008
Posts: 1132 on topic

Thank you for registering your game
PostPosted: Sun, 5. Sep 10, 09:28    Post subject: Reply with quote Print

Update v1.1

Arrow Incorporated Lit's optimization above, cheers for that. Fixed issue setting non-jsonobj array elements.


_________________
There are 10 types of people in the S&M forums - those who understand binary, and those who don't.

Black holes are where God divided by zero.
Back to top
View user's profile Send private message
ThisIsHarsh





Joined: 19 Oct 2008
Posts: 1132 on topic

Thank you for registering your game
PostPosted: Sun, 25. Sep 11, 14:18    Post subject: Reply with quote Print

Update v1.2

Arrow Fixed a couple of parsing bugs.


_________________
There are 10 types of people in the S&M forums - those who understand binary, and those who don't.

Black holes are where God divided by zero.
Back to top
View user's profile Send private message
Kinseth





Joined: 18 Sep 2014
Posts: 2 on topic

Thank you for registering your game
PostPosted: Wed, 24. Sep 14, 01:47    Post subject: Reply with quote Print

Took 40+ minutes to find the thing again, but here is a new link for it.

http://xdownloads.co.uk/index.php/component/jdownloads/summary/47-utility-and-library-scripts/1329-jsonparserlibrary-v1-2-25-09-2011?Itemid=0

/waits for the love.

Back to top
View user's profile Send private message
cheetah222222





Joined: 31 Aug 2008
Posts: 18 on topic

Thank you for registering your game
PostPosted: Sat, 29. Aug 15, 17:27    Post subject: json lib Reply with quote Print

Any idea where I can find the library?

Back to top
View user's profile Send private message
X2-Illuminatus
Moderator (Deutsch)
Moderator (Deutsch)

MEDALMEDALMEDAL

Joined: 02 Apr 2006
Posts: 20457 on topic

Thank you for registering your game
PostPosted: Sat, 29. Aug 15, 21:42    Post subject: Reply with quote Print

On xdownloads.co.uk:




_________________
Besteht Interesse an einem Nopileos-Hörbuch?

Farnhams Legende und Nopileos als E-Books!

"People who think they know everything are a great annoyance to those of us who do." - Isaac Asimov

"If it's not impossible, there must be a way to do it." - Sir Nicholas Winton
Back to top
View user's profile Send private message Visit poster's website
Menzoberranzan





Joined: 29 Nov 2008
Posts: 16 on topic

Thank you for registering your game
PostPosted: Wed, 11. Nov 15, 11:58    Post subject: Reply with quote Print

X2-Illuminatus wrote:
On xdownloads.co.uk:



I'm not sure about anyone else, but I can't download this (and a few other files) from the site. Apparently I'm not authorized???

I have the file available for anyone using Google Drive (I of course will remove the file at author's request):

https://drive.google.com/file/d/0BwKqSG-c8xhnOElxZWljNkd6WFU/view?usp=sharing

Back to top
View user's profile Send private message
X2-Illuminatus
Moderator (Deutsch)
Moderator (Deutsch)

MEDALMEDALMEDAL

Joined: 02 Apr 2006
Posts: 20457 on topic

Thank you for registering your game
PostPosted: Wed, 11. Nov 15, 12:10    Post subject: Reply with quote Print

xdownloads.co.uk was hacked a couple of days ago, and SS_T is currently rebuilding the site, hence why previous download links don't work at the moment. The files are backed up on onedrive though. (I don't have the direct download link at hand though.)

Edit [9th Jan 2016]: Added working download links to the opening post.


_________________
Besteht Interesse an einem Nopileos-Hörbuch?

Farnhams Legende und Nopileos als E-Books!

"People who think they know everything are a great annoyance to those of us who do." - Isaac Asimov

"If it's not impossible, there must be a way to do it." - Sir Nicholas Winton
Back to top
View user's profile Send private message Visit poster's website
efernal





Joined: 11 Dec 2005
Posts: 484 on topic
Location: P.A.
Thank you for registering your game
PostPosted: Mon, 18. Apr 16, 04:25    Post subject: Reply with quote Print

Only the Microsoft onedrive link is still working.


_________________
Those Damn alien bastards are gonna pay for blowing up my ride!
--Duke Nukem 3D
Back to top
View user's profile Send private message
pintcapable424





Joined: 15 Jul 2016



PostPosted: Fri, 15. Jul 16, 15:26    Post subject: Reply with quote Print

Finally OneDrive link is working.

Thanks

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic Reply to topic
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
Control Panel
Login Data
The time now is Thu, 14. Dec 17, 00:31

All times are GMT + 2 Hours


Board Security

Copyright © EGOSOFT 1989-2017
Powered by phpBB © 2001, 2005 phpBB Group
Template created by Avatar & BurnIt!
Debug: page generation = 0.12425 seconds, sql queries = 25