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
19 #define __STRICT_ANSI__
20 #include <json/json.h>
23 redef interface Jsonable
24 # Get a JsonObject representing this instance, specific to the C library
25 private fun to_json_object
: JsonObject is abstract
28 # Will ignore non-jsonable
29 redef class Map[ K
, V
]
30 # Get a json-formatted string of this map
31 fun to_pretty_json
: String do return native_to_json
(true)
32 fun to_json
: String do return native_to_json
(false)
34 fun native_to_json
( pretty
: Bool ): String import to_json_object
, NativeString.to_s
`{
36 const char *json_native_string;
39 jobj = Map_of_Object_nullable_Object_to_json_object( recv );
40 #ifdef JSON_C_TO_STRING_PRETTY
42 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PRETTY );
44 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PLAIN );
46 json_native_string = json_object_to_json_string( jobj );
48 json_string = NativeString_to_s( (char*)json_native_string );
52 redef fun to_json_object
54 var jobj
= new JsonObject
59 if key
isa String then
61 if val
isa Jsonable then
62 var jsubobj
= val
.to_json_object
63 jobj
.add
( key
, jsubobj
)
64 else if val
== null then
67 print
"WARNING: value \"{val}\
" not jsonable, cannot be converted to json."
70 print
"WARNING: key \"{key}\
" not a string, cannot be converted to json."
79 redef class SequenceRead[ E
]
80 redef fun to_json_object
82 var jarray
= new JsonArray
84 if e
isa nullable Jsonable then
88 var obj
= e
.to_json_object
92 print
"WARNING: element \"{e}\
" not a Jsonable, cannot be converted to json."
101 redef fun to_json_object
import NativeString.to_s
, String.to_cstring
`{
102 char *native_recv = String_to_cstring( recv );
103 return json_object_new_string( native_recv );
108 redef fun to_json_object
`{
109 return json_object_new_int( recv );
114 redef fun to_json_object
`{
115 return json_object_new_boolean( recv );
120 redef fun to_json_object
`{
121 return json_object_new_double( recv );
125 redef class JsonObject
126 new `{ return json_object_new_object(); `}
128 # Add a key and value to the object
129 fun add( key : String, val : nullable JsonObject ) import String.to_cstring, JsonObject as not nullable `{
132 native_key
= String_to_cstring( key
);
134 if ( nullable_JsonObject_is_null
(val
) ) {
135 json_object_object_add
( recv
, native_key
, NULL );
138 jobj
= nullable_JsonObject_as_JsonObject
( val
);
139 json_object_object_add
( recv
, native_key
, jobj
);
144 private extern JsonArray
147 new `{ return json_object_new_array(); `}
149 fun push
( val
: nullable JsonObject ) `{
150 if ( nullable_JsonObject_is_null(val) )
151 json_object_array_add( recv, NULL );
153 json_object_array_add( recv, nullable_JsonObject_as_JsonObject(val) );