[SCRIPT LIB] JSON parser v1.2 [2011-09-25]

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
ThisIsHarsh
Posts: 1135
Joined: Sun, 19. Oct 08, 18:46
x3tc

[SCRIPT LIB] JSON parser v1.2 [2011-09-25]

Post by ThisIsHarsh » Wed, 21. Oct 09, 00:44

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

Image

[ external image ]

[ external image ]

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: Select all

<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: Select all

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

Fetching values:

Code: Select all

$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: Select all

$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: Select all

$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: Select all

$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: Select all

= $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: Select all

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

Code: Select all

$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
Last edited by N8M4R3 on Fri, 21. Apr 23, 15:51, edited 4 times in total.
Reason: dl link to X Downloads restored
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.

Lit
Posts: 45
Joined: Fri, 17. Feb 06, 15:27
x3

Post by Lit » Sun, 10. Jan 10, 14:51

I fix multilanguage problem

This strange loop :wink: :

Code: Select all

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: Select all

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

ThisIsHarsh
Posts: 1135
Joined: Sun, 19. Oct 08, 18:46
x3tc

Post by ThisIsHarsh » Sun, 5. Sep 10, 09:28

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.

ThisIsHarsh
Posts: 1135
Joined: Sun, 19. Oct 08, 18:46
x3tc

Post by ThisIsHarsh » Sun, 25. Sep 11, 14:18

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.

Kinseth
Posts: 6
Joined: Thu, 18. Sep 14, 18:49
x4

Post by Kinseth » Wed, 24. Sep 14, 01:47

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

http://xdownloads.co.uk/index.php/compo ... 1?Itemid=0

/waits for the love.

cheetah222222
Posts: 18
Joined: Sun, 31. Aug 08, 20:07
x3tc

json lib

Post by cheetah222222 » Sat, 29. Aug 15, 17:27

Any idea where I can find the library?

User avatar
X2-Illuminatus
Moderator (Deutsch)
Moderator (Deutsch)
Posts: 24950
Joined: Sun, 2. Apr 06, 16:38
x4

Post by X2-Illuminatus » Sat, 29. Aug 15, 21:42

On xdownloads.co.uk:

[ external image ]
Nun verfügbar! X3: Farnham's Legacy - Ein neues Kapitel für einen alten Favoriten

Die komplette X-Roman-Reihe jetzt als Kindle E-Books! (Farnhams Legende, Nopileos, X3: Yoshiko, X3: Hüter der Tore, X3: Wächter der Erde)

Neuauflage der fünf X-Romane als Taschenbuch

The official X-novels Farnham's Legend, Nopileos, X3: Yoshiko as Kindle e-books!

Menzoberranzan
Posts: 27
Joined: Sat, 29. Nov 08, 06:58
x4

Post by Menzoberranzan » Wed, 11. Nov 15, 10:58

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

[ external image ]
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 ... sp=sharing

User avatar
X2-Illuminatus
Moderator (Deutsch)
Moderator (Deutsch)
Posts: 24950
Joined: Sun, 2. Apr 06, 16:38
x4

Post by X2-Illuminatus » Wed, 11. Nov 15, 11:10

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.
Nun verfügbar! X3: Farnham's Legacy - Ein neues Kapitel für einen alten Favoriten

Die komplette X-Roman-Reihe jetzt als Kindle E-Books! (Farnhams Legende, Nopileos, X3: Yoshiko, X3: Hüter der Tore, X3: Wächter der Erde)

Neuauflage der fünf X-Romane als Taschenbuch

The official X-novels Farnham's Legend, Nopileos, X3: Yoshiko as Kindle e-books!

efernal
Posts: 502
Joined: Sun, 11. Dec 05, 21:43
x3tc

Post by efernal » Mon, 18. Apr 16, 04:25

Only the Microsoft onedrive link is still working.
Those Damn alien bastards are gonna pay for blowing up my ride!
--Duke Nukem 3D

pintcapable424
Posts: 1
Joined: Fri, 15. Jul 16, 15:25

Post by pintcapable424 » Fri, 15. Jul 16, 15:26

Finally OneDrive link is working.

Thanks

camus2555
Posts: 6
Joined: Mon, 24. Nov 08, 16:37
x4

Re: [SCRIPT LIB] JSON parser v1.2 [2011-09-25]

Post by camus2555 » Fri, 21. Apr 23, 12:41

Hey anyone still have this ? the last google drive link is asking for an authorisation and all other links are dead.

User avatar
N8M4R3
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 392
Joined: Fri, 24. Nov 06, 15:48
x4

Re: [SCRIPT LIB] JSON parser v1.2 [2011-09-25]

Post by N8M4R3 » Fri, 21. Apr 23, 15:45

camus2555 wrote:
Fri, 21. Apr 23, 12:41
Hey anyone still have this ?
It's still available here: Image
Neue Erweiterung für X3 verfügbar: Farnham's Legacy | +Optional: weitere Verbesserungen im inoffiziellen Patch v1.3.14 *** Modified*** :khaak: :thumb_up:
Diese Woche im Angebot: HUD-GUI-Mix (FL) | Text-DB 0001-L049 (FL) | Textkorrekturen & Verbesserungen (FL)
Weitere Veröffentlichungen hier: N8workX
Nützliches Tool für nicht mehr vorhandene Downloads: web.archive.org
Externes Archiv für MOD/SCR Ressourcen: xdownloads.co.uk | code.google.com/archive/p/x3tcscripts/

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”