1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # Serialisation from Nit objects to the Json format
16 intrude import jsonable
18 redef interface Jsonable
19 # Get a JsonObject representing this instance, specific to the C library
20 private fun to_json_object
: JsonObject is abstract
23 # Will ignore non-jsonable
24 redef class Map[ K
, V
]
25 # Get a json-formatted string of this map
26 fun to_pretty_json
: String do return native_to_json
(true)
27 fun to_json
: String do return native_to_json
(false)
29 fun native_to_json
( pretty
: Bool ): String import to_json_object
`{
31 const char *json_native_string;
34 jobj = Map_to_json_object( recv );
35 #ifdef JSON_C_TO_STRING_PRETTY
37 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PRETTY );
39 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PLAIN );
41 json_native_string = json_object_to_json_string( jobj );
43 json_string = new_String_from_cstring( (char*)json_native_string );
47 redef fun to_json_object
49 var jobj
= new JsonObject
54 if key
isa String then
56 if val
isa Jsonable then
57 var jsubobj
= val
.to_json_object
58 jobj
.add
( key
, jsubobj
)
59 else if val
== null then
62 print
"WARNING: value \"{val}\
" not jsonable, cannot be converted to json."
65 print
"WARNING: key \"{key}\
" not a string, cannot be converted to json."
74 redef class Sequence[ E
]
75 redef fun to_json_object
77 var jarray
= new JsonArray
79 if e
isa nullable Jsonable then
83 var obj
= e
.to_json_object
87 print
"WARNING: element \"{e}\
" not a Jsonable, cannot be converted to json."
96 redef fun to_json_object
import String::from_cstring
`{
97 char *native_recv = String_to_cstring( recv );
98 return json_object_new_string( native_recv );
103 redef fun to_json_object
`{
104 return json_object_new_int( recv );
109 redef fun to_json_object
`{
110 return json_object_new_boolean( recv );
115 redef fun to_json_object
`{
116 return json_object_new_double( recv );
120 redef class JsonObject
121 new `{ return json_object_new_object(); `}
123 # Add a key and value to the object
124 fun add( key : String, val : nullable JsonObject ) import String::to_cstring, nullable JsonObject as not nullable `{
127 native_key
= String_to_cstring( key
);
129 if ( JsonObject_is_null(val
) ) {
130 json_object_object_add
( recv
, native_key
, NULL );
133 jobj
= JsonObject_as_not_null( val
);
134 json_object_object_add
( recv
, native_key
, jobj
);
139 private extern JsonArray
142 new `{ return json_object_new_array(); `}
144 fun push
( val
: nullable JsonObject ) `{
145 if ( JsonObject_is_null(val) )
146 json_object_array_add( recv, NULL );
148 json_object_array_add( recv, JsonObject_as_not_null(val) );