JSON
Read and write JSON objects and arrays.
To use these operators, add the import to the program:
Import "JSON.rgr"Source: lib/JSON.rgr.
This file holds two operator mechanisms. The template operators write target code from a string per language. The type methods are Ranger code. The compiler compiles them for every target that loads the library.
Template operators
Section titled “Template operators”| Operator | Arguments | Gives |
|---|---|---|
array_length |
e: JSONArrayObject | int |
asArray |
e: JSONValueUnion | <optional>JSONArrayObject |
case |
arg: JSONValueUnion, item: JSONArrayObject, code: block | void |
case |
arg: JSONValueUnion, item: JSONDataObject, code: block | void |
from_string |
txt: string | JSONDataObject |
getArray |
e: JSONDataObject, key: string | <optional>JSONArrayObject |
getBoolean |
e: JSONDataObject, key: string | <optional>boolean |
getDouble |
e: JSONDataObject, key: string | <optional>double |
getInt |
e: JSONDataObject, key: string | <optional>int |
getObject |
e: JSONDataObject, key: string | <optional>JSONDataObject |
getStr |
e: JSONDataObject, key: string | <optional>string |
getValue |
e: JSONArrayObject, index: int | JSONValueUnion |
isArray |
e: JSONValueUnion | boolean |
json_array |
— | JSONArrayObject |
json_array |
e: JSONArrayUnion | JSONArrayObject |
json_obj |
e: JSONObjectUnion | JSONDataObject |
json_obj.attr |
name: string, value: JSONArrayObject | JSONKeyValue |
json_obj.attr |
name: string, value: JSONDataObject | JSONKeyValue |
json_obj.attr |
name: string, value: string | JSONKeyValue |
json_object |
— | JSONDataObject |
json.attr |
name: string, value: double | JSONKeyValue |
json.attr |
name: string, value: int | JSONKeyValue |
json.attr |
name: string, value: JSONArrayObject | JSONKeyValue |
json.attr |
name: string, value: JSONDataObject | JSONKeyValue |
json.attr |
name: string, value: string | JSONKeyValue |
keys |
e: JSONDataObject | [string] |
print |
e: JSONDataObject | void |
push |
e: JSONArrayObject, el: JSONArrayUnion | void |
set |
e: JSONDataObject, key: string, value: boolean | void |
set |
e: JSONDataObject, key: string, value: double | void |
set |
e: JSONDataObject, key: string, value: enum | void |
set |
e: JSONDataObject, key: string, value: int | void |
set |
e: JSONDataObject, key: string, value: JSONValueUnion | void |
set |
e: JSONDataObject, key: string, value: string | void |
to_string |
e: JSONArrayObject | string |
to_string |
e: JSONDataObject | string |
Target support marks
- Own template
- Default template
- No template
The operator works for a target with an own template and for a target with the default template. The default template is the * entry of the definition: it writes the same code for every target that has no template of its own. A target with no template writes no code, and a program that uses the operator does not compile for that target.
array_length
(array_length e) → int
array_length(e: JSONArrayObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3629
asArray
(asArray e) → <optional>JSONArrayObject
asArray(e: JSONValueUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: own template
- Python: own template
- Java: no template
- Kotlin: no template
- Dart: own template
- Swift: no template
- C#: own template
- C++: own template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 3080
case
(case arg item code) → void
case(arg: JSONValueUnion, item: JSONArrayObject, code: block)Target support
- JavaScript: no template
- TypeScript: no template
- Go: no template
- Rust: own template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 4239
case
(case arg item code) → void
case(arg: JSONValueUnion, item: JSONDataObject, code: block)Rust narrows a union by matching the enum variant, and the variant is not the type name that `(typeof 2)` writes, so the generic `case` in stdlib.rgr can not serve it. These two entries name the JSON shapes directly. A target that has no template here does not match them and reaches the generic one.
Target support
- JavaScript: no template
- TypeScript: no template
- Go: no template
- Rust: own template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 4224
from_string
(from_string txt) → JSONDataObject
from_string(txt: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 4254
getArray
(getArray e key) → <optional>JSONArrayObject
getArray(e: JSONDataObject, key: string)todo: proper array detection for optionals
Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 2545
getBoolean
(getBoolean e key) → <optional>boolean
getBoolean(e: JSONDataObject, key: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 1797
getDouble
(getDouble e key) → <optional>double
getDouble(e: JSONDataObject, key: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 1375
getInt
(getInt e key) → <optional>int
getInt(e: JSONDataObject, key: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 949
getObject
(getObject e key) → <optional>JSONDataObject
getObject(e: JSONDataObject, key: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 2162
getStr
(getStr e key) → <optional>string
getStr(e: JSONDataObject, key: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 397
getValue
(getValue e index) → JSONValueUnion
getValue(e: JSONArrayObject, index: int)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3360
isArray
(isArray e) → boolean
isArray(e: JSONValueUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: no template
- Kotlin: no template
- Dart: own template
- Swift: no template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3014
json_array
(json_array) → JSONArrayObjectTarget support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 5571
json_array
(json_array e) → JSONArrayObject
json_array(e: JSONArrayUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: no template
- Python: own template
- Java: own template
- Kotlin: no template
- Dart: no template
- Swift: own template
- C#: no template
- C++: no template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 5691
json_obj
(json_obj e) → JSONDataObject
json_obj(e: JSONObjectUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5703
json_obj.attr
(json_obj.attr name value) → JSONKeyValue
json_obj.attr(name: string, value: JSONArrayObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5761
json_obj.attr
(json_obj.attr name value) → JSONKeyValue
json_obj.attr(name: string, value: JSONDataObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5744
json_obj.attr
(json_obj.attr name value) → JSONKeyValue
json_obj.attr(name: string, value: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5710
json_object
(json_object) → JSONDataObjectTarget support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 5631
json.attr
(json.attr name value) → JSONKeyValue
json.attr(name: string, value: double)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5735
json.attr
(json.attr name value) → JSONKeyValue
json.attr(name: string, value: int)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5727
json.attr
(json.attr name value) → JSONKeyValue
json.attr(name: string, value: JSONArrayObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5769
json.attr
(json.attr name value) → JSONKeyValue
json.attr(name: string, value: JSONDataObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5753
json.attr
(json.attr name value) → JSONKeyValue
json.attr(name: string, value: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: no template
- Python: no template
- Java: no template
- Kotlin: no template
- Dart: no template
- Swift: no template
- C#: no template
- C++: no template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 5719
keys
(keys e) → [string]
keys(e: JSONDataObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 2910
print
(print e) → void
print(e: JSONDataObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: no template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: no template
- Dart: own template
- Swift: no template
- C#: own template
- C++: own template
- PHP: no template
- Scala: no template
Definition: lib/JSON.rgr, line 101
push
(push e el) → void
push(e: JSONArrayObject, el: JSONArrayUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 5474
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: boolean)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 4034
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: double)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3948
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: enum)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3690
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: int)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3862
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: JSONValueUnion)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 4120
set
(set e key value) → void
set(e: JSONDataObject, key: string, value: string)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 3776
to_string
(to_string e) → string
to_string(e: JSONArrayObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 5107
to_string
(to_string e) → string
to_string(e: JSONDataObject)Target support
- JavaScript: own template
- TypeScript: own template
- Go: own template
- Rust: own template
- Python: own template
- Java: own template
- Kotlin: own template
- Dart: own template
- Swift: own template
- C#: own template
- C++: own template
- PHP: own template
- Scala: no template
Definition: lib/JSON.rgr, line 4728
Type methods
Section titled “Type methods”A type method is an operator of the receiver type. The call is
receiver.name(…). The body is Ranger code, so the compiler writes it for
every target that compiles the library.
JSONArrayObject
Section titled “JSONArrayObject”| Method | Arguments | Gives | Targets |
|---|---|---|---|
forEach |
: (fn:i ()) | `` | every target |
map |
cb: (fn:S (item:JSONValueUnion index:int)), to: [S] | [S] |
every target |
forEach
receiver.forEach() →
forEach(: (fn:i ()))The compiler writes this method for every target.
The Ranger source of the method
fn forEach( cb:(_:void (item:JSONValueUnion index:int))) { def cnt (array_length self) def i 0 while( cnt > 0 ) { def value (getValue self i) cb(value i) cnt = cnt - 1 i = i + 1 }}Definition: lib/JSON.rgr, line 76
map
receiver.map(cb to) → [S]
map(cb: (fn:S (item:JSONValueUnion index:int)), to: [S])The compiler writes this method for every target.
The Ranger source of the method
fn map:[S] (cb:(_:S (item:JSONValueUnion index:int)) to@(noeval):[S] ) { def len (array_length self) def res:[S] def i 0 while ( i < len) { def it (getValue self i) push res (cb(it i)) i = i + 1 } return res}Definition: lib/JSON.rgr, line 86
Ranger 3.5.1 · commit f323fd4 · development build