Libraries
JSON Source Code
 previous   up   next 

Types
jsonCategory
Enumeration type describing the category of a jsonValue.
jsonValue
Interface type to represent JSON values.

jsonCategory

const type: jsonCategory

Enumeration type describing the category of a jsonValue. Categories are JSON_NULL, JSON_BOOLEAN, JSON_NUMBER, JSON_STRING, JSON_ARRAY and JSON_OBJECT.


jsonValue

const type: jsonValue

Interface type to represent JSON values. JSON values can be null (jsonNull), booleans (jsonBoolean), numbers (jsonNumber), strings (jsonString), arrays (jsonArray) or objects (jsonObject).

var jsonValue: json is jsonValue.value;
...
json := readJson("{\"size\": 2, \"keys\": [{\"id\": 1}, {\"id\": 2}]");
if "keys" in json and category(json["keys"]) = JSON_ARRAY then
  for aKey range json["keys"] do
    write(integer(aKey["id"]) <& " ");
  end for;
  writeln;
end if;

Operator Summary
jsonValue
(in jsonValue: aValue) [ (in integer: index) ]
Access one element from the JSON array aValue.
jsonValue
(in jsonValue: aValue) [ (in string: name) ]
Access one element from the JSON object aValue.
boolean
(in string: aKey) in (in jsonValue: aValue)
Determine if aKey is an element name of the JSON object aValue.
boolean
(in string: aKey) not in (in jsonValue: aValue)
Determine if aKey is not an element name of the JSON object aValue.

Function Summary
jsonCategory
category (in jsonValue: aValue)
Get the category of the JSON value aValue.
type
type (in jsonValue: aValue)
Get the type of the JSON value aValue.
void
void (in jsonValue: aValue)
Get the void value from the JSON null aValue.
boolean
boolean (in jsonValue: aValue)
Get the boolean value from the JSON boolean aValue.
integer
integer (in jsonValue: aValue)
Get the integer value from the JSON number aValue.
bigInteger
bigInteger (in jsonValue: aValue)
Get the bigInteger value from the JSON number aValue.
float
float (in jsonValue: aValue)
Get the float value from the JSON number aValue.
string
string (in jsonValue: aValue)
Get the string value from the JSON string aValue.
integer
length (in jsonValue: aValue)
Length of JSON array aValue.
integer
minIdx (in jsonValue: aValue)
Minimum index of JSON array aValue.
integer
maxIdx (in jsonValue: aValue)
Maximum index of JSON array aValue.
array string
keys (in jsonValue: aValue)
Obtain the element names of the JSON object aValue.
array jsonValue
values (in jsonValue: aValue)
Obtain the elements of the JSON array aValue.
string
str (in jsonValue: aValue)
Convert the given JSON value aValue to a string.
void
for (inout jsonValue: forVar) range (in jsonValue: aValue) do (in proc: statements) end for
For-loop where forVar loops over the elements of a JSON array.
jsonValue
jsonValue (NULL)
Create a null jsonValue.
jsonValue
jsonValue (in boolean: okay)
Create a jsonValue with the given boolean okay.
jsonValue
jsonNumber (in string: number)
Create a jsonNumber with the given number.
jsonValue
jsonValue (in integer: number)
Create a jsonValue with the given integer number.
jsonValue
jsonValue (in bigInteger: number)
Create a jsonValue with the given bigInteger number.
jsonValue
jsonValue (in float: number)
Create a jsonValue with the given float number.
jsonValue
jsonValue (in string: stri)
Create a jsonValue with the given string stri.
jsonValue
jsonValue (in jsonValueArray: elements)
Create a jsonValue with the given elements.
jsonValue
readJson (inout file: inFile)
Read a jsonValue from the given inFile.
jsonValue
readJson (in string: jsonStri)
Read a jsonValue from the given jsonStri.

Operator Detail

[

const func jsonValue: (in jsonValue: aValue) [ (in integer: index) ]

Access one element from the JSON array aValue.

type(   readJson("[false, 0.125]")[1]) returns boolean
boolean(readJson("[false, 0.125]")[1]) returns FALSE
type(   readJson("[false, 0.125]")[2]) returns float
float(  readJson("[false, 0.125]")[2]) returns 0.125
Returns:
the element with the specified index from aValue.
Raises:
INDEX_ERROR - If index is less than 1 or greater than maxIdx(aValue).
ILLEGAL_ACTION - The value aValue is not a JSON array.

[

const func jsonValue: (in jsonValue: aValue) [ (in string: name) ]

Access one element from the JSON object aValue.

type(   readJson("{\"fee\": 0.5, \"foo\": true}")["fee"])  returns  float
float(  readJson("{\"fee\": 0.5, \"foo\": true}")["fee"])  returns  0.5
type(   readJson("{\"fee\": 0.5, \"foo\": true}")["foo"])  returns  boolean
boolean(readJson("{\"fee\": 0.5, \"foo\": true}")["foo"])  returns  TRUE
Returns:
the element with the specified name from aValue.
Raises:
INDEX_ERROR - If aValue does not have an element with the key name.
ILLEGAL_ACTION - The value aValue is not a JSON object.

in

const func boolean: (in string: aKey) in (in jsonValue: aValue)

Determine if aKey is an element name of the JSON object aValue.

"fee" in readJson("{\"fee\": 0.5, \"foo\": true}")  returns  TRUE
"fi"  in readJson("{\"fee\": 0.5, \"foo\": true}")  returns  FALSE
Returns:
TRUE if aKey is an element name of the JSON object aValue, FALSE otherwise.

not in

const func boolean: (in string: aKey) not in (in jsonValue: aValue)

Determine if aKey is not an element name of the JSON object aValue.

"fee" not in readJson("{\"fee\": 0.5, \"foo\": true}")  returns  FALSE
"fi"  not in readJson("{\"fee\": 0.5, \"foo\": true}")  returns  TRUE
Returns:
FALSE if aKey is an element name of the JSON object aValue, TRUE otherwise.

Function Detail

category

const func jsonCategory: category (in jsonValue: aValue)

Get the category of the JSON value aValue. Returns one of JSON_NULL, JSON_BOOLEAN, JSON_NUMBER, JSON_STRING, JSON_ARRAY or JSON_OBJECT.

category(jsonValue(NULL))                  returns  JSON_NULL
category(jsonValue(TRUE))                  returns  JSON_BOOLEAN
category(jsonValue(              12345 ))  returns  JSON_NUMBER
category(jsonValue(9223372036854775808_))  returns  JSON_NUMBER
category(jsonValue("foo"))                 returns  JSON_STRING
category(readJson("false"))                returns  JSON_BOOLEAN
category(readJson("9223372036854775808"))  returns  JSON_NUMBER
category(readJson("0.00000762939453125"))  returns  JSON_NUMBER
Returns:
the category of the JSON value aValue.

type

const func type: type (in jsonValue: aValue)

Get the type of the JSON value aValue. Returns one of void, boolean, integer, bigInteger, float, string, jsonValueArray or jsonValueMap.

type(jsonValue(NULL))                  returns  void
type(jsonValue(TRUE))                  returns  boolean
type(jsonValue(9223372036854775808_))  returns  bigInteger
type(jsonValue(0.00000762939453125 ))  returns  float
type(jsonValue("foo"))                 returns  string
type(readJson("false"))                returns  boolean
type(readJson("9223372036854775808"))  returns  bigInteger
type(readJson("0.00000762939453125"))  returns  float
Returns:
the type of the JSON value aValue.

void

const func void: void (in jsonValue: aValue)

Get the void value from the JSON null aValue. This function is defined for completeness. If applied on a JSON null it will always return empty.

void(jsonValue(NULL))   returns  empty
void(readJson("null"))  returns  empty

The function type can be used to check for JSON null:

if type(aJasonValue) = void then

The function category can be used for the same purpose:

if category(aJasonValue) = JSON_NULL then
Returns:
the void value of a JSON null.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON null.

boolean

const func boolean: boolean (in jsonValue: aValue)

Get the boolean value from the JSON boolean aValue.

boolean(jsonValue(FALSE))  returns  FALSE
boolean(readJson("true"))  returns  TRUE

The function type can be used to check for a boolean:

if type(aJasonValue) = boolean then
  aBoolean := boolean(aJasonValue);

The function category can be used for the same purpose:

if category(aJasonValue) = JSON_BOOLEAN then
  aBoolean := boolean(aJasonValue);
Returns:
the boolean value of a JSON boolean.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON boolean.

integer

const func integer: integer (in jsonValue: aValue)

Get the integer value from the JSON number aValue.

integer(jsonValue(12345))                  returns  12345
integer(readJson("12345"))                 returns  12345
integer(readJson("12345678909876543210"))  raises   RANGE_ERROR

The function type can be used to check for an integer:

if type(aJasonValue) = integer then
  anInteger := integer(aJasonValue);
Returns:
the integer value of a JSON number.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON number.
RANGE_ERROR - The JSON number cannot be represented as integer.

bigInteger

const func bigInteger: bigInteger (in jsonValue: aValue)

Get the bigInteger value from the JSON number aValue.

bigInteger(jsonValue(12345_))   returns  12345_
bigInteger(readJson("12345"))   returns  12345_
bigInteger(readJson("123.45"))  raises   RANGE_ERROR

The function type can be used to check for a bigInteger:

if type(aJasonValue) = bigInteger then
  aBigInteger := bigInteger(aJasonValue);
Returns:
the bigInteger value of a JSON number.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON number.
RANGE_ERROR - The JSON number cannot be represented as bigInteger.

float

const func float: float (in jsonValue: aValue)

Get the float value from the JSON number aValue.

float(jsonValue(0.0625))   returns  0.0625
float(readJson("0.0625"))  returns  0.0625

The function type can be used to check for a float:

if type(aJasonValue) = float then
  aFloat := float(aJasonValue);
Returns:
the float value of a JSON number.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON number.

string

const func string: string (in jsonValue: aValue)

Get the string value from the JSON string aValue.

string(jsonValue(""))               returns  ""
string(jsonValue("abcdefg"))        returns  "abcdefg"
string(jsonValue(" \t\r\n"))        returns  " \t\r\n"
string(readJson("\"\""))            returns  ""
string(readJson("\"abcdefg\""))     returns  "abcdefg"
string(readJson("\" \\t\\r\\n\""))  returns  " \t\r\n"

The function type can be used to check for a string:

if type(aJasonValue) = string then
  aString := string(aJasonValue);

The function category can be used for the same purpose:

if category(aJasonValue) = JSON_STRING then
  aString := string(aJasonValue);
Returns:
the string value of a JSON string.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON string.

length

const func integer: length (in jsonValue: aValue)

Length of JSON array aValue.

length(readJson("[]"))           returns 0
length(readJson("[1]"))          returns 1
length(readJson("[1, \"foo\"]")) returns 2
Returns:
the length of the JSON array.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON array.

minIdx

const func integer: minIdx (in jsonValue: aValue)

Minimum index of JSON array aValue.

minIdx(readJson("[]"))           returns 1
minIdx(readJson("[1]"))          returns 1
minIdx(readJson("[1, \"foo\"]")) returns 1
Returns:
the minimum index of the JSON array.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON array.

maxIdx

const func integer: maxIdx (in jsonValue: aValue)

Maximum index of JSON array aValue.

maxIdx(readJson("[]"))           returns 0
maxIdx(readJson("[1]"))          returns 1
maxIdx(readJson("[1, \"foo\"]")) returns 2
Returns:
the maximum index of the JSON array.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON array.

keys

const func array string: keys (in jsonValue: aValue)

Obtain the element names of the JSON object aValue.

keys(readJson("{}"))                             returns  0 times ""
keys(readJson("{\"fee\": 0.5}"))                 returns  [] ("fee")
keys(readJson("{\"fee\": 0.5, \"foo\": true}"))  returns  [] ("fee", "foo")
Returns:
an array with the element names of the JSON object.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON object.

values

const func array jsonValue: values (in jsonValue: aValue)

Obtain the elements of the JSON array aValue.

values(readJson("[]"))           returns  0 times jsonValue.value
values(readJson("[\"fee\"]"))    returns  [] (jsonValue("fee"))
values(readJson("[0.5, true]"))  returns  [] (jsonValue(0.5), jsonValue(TRUE))
Returns:
an array with the elements of the JSON array.
Raises:
ILLEGAL_ACTION - The value aValue is not a JSON array.

str

const func string: str (in jsonValue: aValue)

Convert the given JSON value aValue to a string.

str(readJson("[1, \"a\", {\"ok\" : true}]"))  returns  "[1,\"a\",{\"ok\":true}]"
Returns:
the JSON value as string.

for

const proc: for (inout jsonValue: forVar) range (in jsonValue: aValue) do (in proc: statements) end for

For-loop where forVar loops over the elements of a JSON array.

var jsonValue: json is jsonValue.value;
var jsonValue: anElement is jsonValue.value;
...
if "keys" in json and category(json["keys"]) = JSON_ARRAY then
  for anElement range json["keys"] do
    ...

jsonValue

const func jsonValue: jsonValue (NULL)

Create a null jsonValue.

jsonValue(NULL)
Returns:
a jsonNull as jsonValue.

jsonValue

const func jsonValue: jsonValue (in boolean: okay)

Create a jsonValue with the given boolean okay.

jsonValue(TRUE)
Returns:
a jsonBoolean with the given okay as jsonValue.

jsonNumber

const func jsonValue: jsonNumber (in string: number)

Create a jsonNumber with the given number.


jsonValue

const func jsonValue: jsonValue (in integer: number)

Create a jsonValue with the given integer number.

jsonValue(123)
Returns:
a jsonNumber with the given integer number as jsonValue.

jsonValue

const func jsonValue: jsonValue (in bigInteger: number)

Create a jsonValue with the given bigInteger number.

jsonValue(123_)
Returns:
a jsonNumber with the given bigInteger number as jsonValue.

jsonValue

const func jsonValue: jsonValue (in float: number)

Create a jsonValue with the given float number.

jsonValue(0.125)
Returns:
a jsonNumber with the given float number as jsonValue.

jsonValue

const func jsonValue: jsonValue (in string: stri)

Create a jsonValue with the given string stri.

jsonValue("test")
Returns:
a jsonString with the given string stri as jsonValue.

jsonValue

const func jsonValue: jsonValue (in jsonValueArray: elements)

Create a jsonValue with the given elements.

jsonValue(0 times jsonValue.value)
Returns:
a jsonArray with the given elements as jsonValue.

readJson

const func jsonValue: readJson (inout file: inFile)

Read a jsonValue from the given inFile.

var file: aFile is STD_NULL;
var jsonValue: json is jsonValue.value;
...
aFile := openUtf8("test.json", "r");
if aFile <> STD_NULL then
  json := readJson(aFile);
Returns:
the jsonValue read from inFile.
Raises:
RANGE_ERROR - The inFile does not contain valid JSON.

readJson

const func jsonValue: readJson (in string: jsonStri)

Read a jsonValue from the given jsonStri.

var jsonValue: json is jsonValue.value;
...
json := readJson("[1, \"a\", {\"ok\" : true}]");
Returns:
the jsonValue read from jsonStri.
Raises:
RANGE_ERROR - The string jsonStri does not contain valid JSON.


 previous   up   next