BlitzMax JSON by Scaremonger.
VERSION: 3.2
LINUX:
# mkdir -p ~/BlitzMax/mod/bmx.mod
# cd ~/BlitzMax/mod/bmx.mod
# git clone https://github.com/blitzmax-itspeedway-net/json.mod.git
# cd json.mod
# chmod +x compile.sh
# ./compile.sh
WINDOWS:
C:\> mkdir C:\BlitzMax\mod\bmx.mod
C:\> cd /d C:\BlitzMax\mod\bmx.mod
C:\> git clone https://github.com/blitzmax-itspeedway-net/json.mod.git
C:\> cd json.mod
C:\> compile.bat
- Create a folder in your BlitzMax/mod folder called "bmx.mod"
- Download ZIP file from GitHub and unzip it: You will have a folder called json.mod.
- Copy folder json.mod-main/json.mod to BlitzMax/mod/bmx.mod/
- Run the compile.sh or compile.bat file located in the json.mod folder to compile
LINUX:
# cd ~/BlitzMax/mod/bmx.mod/json.mod
# git pull
# chmod +x compile.sh
# ./compile.sh
WINDOWS:
C:\> cd /d C:\BlitzMax\mod\bmx.mod\json.mod
C:\> git pull
C:\> compile.bat
Internally all JSON variables are stored as JSON types that represent the following types to store Blitzmax variables:
JSON | INTERNAL | VALUES | NOTES |
---|---|---|---|
JINVALID | TString | ||
JARRAY | TObjectList | ||
JNUMBER | String | ||
JOBJECT | TMap | ||
JSTRING | String | ||
JKEYWORD | String | "false", "true", "null" | |
JBOOLEAN | String | "false", "true | Stored as JKEYWORD |
JNULL | String | "null" | Stored as JKEYWORD |
String to JSON - JSON to String
Import bmx.json
Local JText:String = "{ ~qpeople~q:[~qjack~q,~qjill~q]}"
Local J:JSON = JSON.Parse( JText )
Local Text:string = J.Stringify()
Print( Text )
JSON to String - JSON to Pretty String
Import bmx.json
Local JText:String = "{ ~qpeople~q:[~qjack~q,~qjill~q]}"
Local J:JSON = JSON.Parse( JText )
Local Text:String = J.Prettify()
Print( Text )
JSON Resource Paths
Import bmx.json
Local JText:String = "{~qstory~q:{~qJack and Jill~q:{~qcharacters~q:[~qjack~q,~qjill~q]}}}"
Local J:JSON = JSON.Parse( JText )
Local characters:JSON = J.find("story|Jack and Jill|characters")
Print( characters.Prettify() )
Manual JSON Creation
Import bmx.json
Local J:JSON = New JSON()
J["id"] = "31"
J["name"] = "Scaremonger"
J["age"] = 99
J["addresses|home"] = "[email protected]"
J["addresses|work"] = "[email protected]"
Print( J.prettify() )
BMX Type Serialisation
Import bmx.json
Type TPerson
Field name: String
Field age: Int
Method New( name:string, age:int )
self.name = name
self.age = age
End Method
End Type
Local person:TPerson = new TPerson( "Scaremonger", 99 )
Local J:JSON = JSON.serialize( person )
Print( J.Prettify() )
Type fields can have the following metadata attached to direct the serialisation process: {serializedname="NAME"} Uses the given NAME field instead of the actula filed. This is useful if your JSON data contains keywords like "field" or "method".
{noserialize}
This prevents the fields from being serialised, effectivly ignoring it.
BMX Type De-Serialisation
Import bmx.json
Type TPerson
Field name: String
Field age: Int
End Type
Local JText:String = "{~qname~q:~qScaremonger~q,~qage~q:99}"
Local J:JSON = JSON.Parse( JText )
Local person:TPerson = TPerson( J.Transpose( "TPerson" ) )
If Person
Print( "NAME: "+Person.name )
Print( "AGE: "+Person.age )
End If
https://www.json.org/json-en.html
FUNCTION | DESCRIPTION |
---|---|
.parse:JSON( JText:string ) | Converts JSON Text string into JSON Object |
.serialise:JSON( obj:Obj ) | Convert Blitzmax Object to JSON |
.transpose:Object( typestr:string ) | Convert JSON into given BlitzMax object. |
.version:string() | Get the current version |
.versioncheck:int( minver:float, minbuild:int ) | Check for JSON library minimum version |
OVERLOAD | DESCRIPTION |
---|---|
[]:String( key:string ) | Equivalent of .find:String( key:String ) |
[]:JSON( key:int ) | Equivalent of .find:JSON( key:int ) |
[]=( route:String, value:String ) | Equivalent of .set( route:string, value:string ) |
[]=( route:String, value:Int ) | Equivalent of .set( route:string, value:Int ) |
[]=( route:String, value:Float ) | Equivalent of .set( route:string, value:Float ) |
[]=( route:String, value:JSON ) | Equivalent of .set( route:string, value:JSON ) |
VERSION | DATE | DETAIL |
---|---|---|
V1.0 | 20 JUL 21 | Basic JSON Parser |
V1.1 | 20 JUL 21 | Added Transpose into TypeStr using reflection, added find() and set() |
V1.2 | 20 JUL 21 | Moved parser into JSON_Parser. Merged JSON and JNode |
V1.3 | 20 JUL 21 | Added error() |
V1.4 | 24 AUG 21 | Re-Written to use bmx.lexer and converted to a Module |
V1.5 | 26 AUG 21 | Fixed issue with escaped characters including Hexcodes. |
V1.6 | 31 AUG 21 | Added contains() method plus example |
V1.7 | 01 SEP 21 | Added PrettyPrint() plus example |
V1.8 | 03 SEP 21 | Changed Array datatype from JSON[] to TObjectList toInt() now processes "true","false" and "null", properly class and value are now private Added constants for the class identifiers Added getClassID() and getClassName() instead of using class directly PrettyPrint() renamed to Prettify() Added isTrue(), isFalse(), isNull() for dealing with boolean/null Added size(), addFirst(), addLast(), RemoveFirst(), RemoveLast() for dealing with Arrays Added examples json-boolean.bmx and json-array.bmx |
V1.8.1 | 04 SEP 21 | Fixed bug in JSON_ constants |
V1.9 | 06 SEP 21 | Fixed bug when parsing multiline (Pretty) JSON |
V1.10 | 22 OCT 21 | Find returns empty JSON instead of NULL when not found. |
V1.11 | 24 OCT 21 | Fixed bug in .toArray() |
V2.0 | 29 OCT 21 | Major refactoring to support TLexer V2.0 Fixed issue parsing whitespace strings Fixed line numbers in errors Fixed bug where EOL was reported as unexpected before a comma |
V2.1 | 09 NOV 21 | Fixed Escaping issue with strings containing quotes |
V2.2 | 06 DEC 21 | Updated size() to support objects Added search() and is() |
V2.3 | 12 DEC 22 | Added Function serialize() Fixed 'Floating point number as string' issue |
V2.4 | 13 DEC 22 | Added toByte(), toDouble(), toFloat(), toLong(), toShort(), toSize_T(), toUInt() and toULong() Added copy() Updated Transpose() Fixed serialise bug when serialising Type JSON. |
V2.5 | 14 DEC 22 | Added Operator overloading to allow easy creation of JSON |
V3.0 | 02 FEB 23 | Internal classes are now INT instead of STRING |
Fixed toUInt(), toULong(), .count(). Added getLastError() | ||
V3.1 | 11 FEB 23 | Fixed issue when parse string is a JSON array |
V3.2 | 18 MAY 24 | Added unset() to remove object from JOBJECT |