There is now rudimentary support for converting parrot objects to JSON strings.

11:56 <Coke> JSON?
11:56 <purl> well, JSON is Javascript Object Notation, at
             http://www.crockford.com/JSON/



From the SYNPOSIS:


+ # generate a JSON representation of a PMC.
+ $S0 = _json( $P0 )
+
+ # generate a PMC from a JSON representation:
+ $P1 = _json_to_pmc( "[1,2,3]" )
+ #$P1 is now a array-like container PMC with three Integer elements.
+
+ .end
+ .include 'library/JSON.imc'



Some notes:

Specific type information is lost. Expect a does-like behavior: if you're one of the 30 different PMC array types, you're an array in JSON.

It's (mostly) reversible: you can marshall the PMC to a generic JSON, then unmarshall it back to a similar PMC. (You can also trade these JSON strings with anything else that speaks JSON.) This is a contrast to free/thaw, which are parrot only, but capture enough information to reproduce the specific PMC faithfully. {{ Except for the part where _json_to_pmc isn't done yet, of course. }}

From the TODO:


+=item Hashed subentries are not entirely pretty yet.
+
+=item Pending a more comprehensive test suite from Roger Browne
+
+=item String Escaping
+
+=item implement _jsan_to_pmc


Sample:

.sub test :main
    $P1 = new .Boolean
    $P1 = 0

    $P2 = new .Float
    $P2 = 1.234

    $S3 = "hello"

    $P5 = new .Hash
    $P5["a"]= 1
    $P5["b"]= "two"
    $P5["c"]= 3.14

    $P4 = new .Array
    $P4 = 4

    $P4[0] = $P1
    $P4[1] = $P2
    $P4[2] = $S3
    $P4[3] = $P5

    $S0 = _json( $P4, 1 )
    print $S0
    .end
    .include 'library/JSON.imc'

--

[
  false,
  1.234,
  "hello",
  {

    "a" :     1,
    "b" :     "two",
    "c" :     3.14
  }
]


Reply via email to