Version française
Home     About     Download     Resources     Contact us    
Browse thread
Long-term storage of values
[ 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] Long-term storage of values
On Thu, 20 Mar 2008, Dario Teixeira wrote:

>>
>> There's json-static for automatic marshalling:
>>
>>    http://martin.jambon.free.fr/json-static.html
>
> Hi,
>
> Thanks for your reply, Martin.  Looking at the examples, it seems that
> json-static is JSON-centric, in the sense that the type definitions for
> the automatic (de)serialisers are to be written in JSON.  Does it support
> the reverse, namely "here's an Ocaml type: please write Ocaml functions
> that (de)serialise into JSON"?

Dario,

No JSON needs to be written by hand.
Here's a simple example:

type json point = { x : int; y : int }  (* an OCaml record *)

It creates the functions with the following signature:

val json_of_point : point -> Json_type.t
val point_of_json : Json_type.t -> point

Json_type.t is the JSON syntax tree that you can serialize using 
Json_io.string_of_json.


# let j = json_of_point { x = 12; y = 34 };;
val j : Json_type.t =
   Json_type.Object [("x", Json_type.Int 12); ("y", Json_type.Int 34)]

# Json_io.string_of_json j;;
- : string = "{ \"x\": 12, \"y\": 34 }"



Martin

--
http://wink.com/profile/mjambon
http://martin.jambon.free.fr