JSON and Rebol

Chris got the ball rolling with a JSON module, originally from 2005:

http://www.ross-gill.com/page/JSON_and_Rebol

Starting discussion from a cache of that page.


Comprehensive functions for exchanging data from Rebol/Red to JSON serial formats. To get started, enter the following into the Rebol Console or paste into a Rebol script:

do http://reb4.me/r/altjson

At the Console, both functions include basic usage information:

? load-json
? to-json

to-json

This function will serialise Rebol values to a JSON string. to-json is fairly aggressive at converting unsupported datatypes to strings:

>> to-json ["A" "block" of 6.0 Rebol #values]
== {["A","block","of",6.0,"Rebol","values"]}

Note: date! is converted to a RFC 3339 Date String

to-json will attempt to resolve GET-WORD! values:

>> foo: ["Foo" "Bar"]

>> to-json [baz: :foo]
== {{"baz":["Foo","Bar"]}}

load-json

This function will attempt to decode JSON-formatted data:

>> probe load-json {{"foo":"bar","num":10,"null":null}}
make object! [
    foo: "bar"
    num: 10
    null: none
]

Alternate usage with the /flat refinement will allow you to parse the resultant Rebol values. Object keys are imported as tag! types:

>> probe load-json/flat {{"foo":"bar","num":1.0}}
[
    <foo> "bar"
    <num> 1.0
]
1 Like