Version française
Home     About     Download     Resources     Contact us    
Browse thread
picking / marshaling to strings in ocaml-revision-stable way
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] picking / marshaling to strings in ocaml-revision-stable way
On Sat, 31 May 2008, Berke Durak wrote:

> On Sat, May 31, 2008 at 7:00 PM, Robert Fischer
> <robert@fischerventure.com> wrote:
>> How far is the reach from the Jane St S-exp library from producing JSON?  I've not actually looked at it, but that'd be super nifty in the interoperation world.
>
> If you just want JSON syntax, you can use Sexplib to convert an
> arbitrary type to a
> Sexp.t
>
>  type t = Atom of string | List of t list
>
> and then output in Json format:
>
> let rec output_json oc = function
> | Atom u -> fprintf oc "%S" u
> | List xl -> fprintf oc "[%a]" (fun oc xl -> List.iter (fun x ->
> fprintf "%a," output_json x) xl) xl
>
> You can then do the same thing for parsing.

You won't obtain anything useful if you treat Atoms as JSON strings and 
Lists as JSON arrays because JSON has also null, numbers, booleans and 
objects.

This is the JSON standard: http://www.json.org/

And that is the concrete type used to represent JSON trees in the 
json-wheel library (which json-static uses):

type json_type =
     Object of (string * json_type) list
   | Array of json_type list
   | String of string
   | Int of int
   | Float of float
   | Bool of bool
   | Null


Cheers,

Martin

--
http://wink.com/profile/mjambon
http://mjambon.com