ni: fix imported classes list in module view
[nit.git] / lib / json / json_writer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
11 # another product.
12
13 # Serialisation from Nit objects to the Json format
14 module json_writer
15
16 intrude import jsonable
17
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
21 end
22
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)
28
29 fun native_to_json( pretty: Bool ): String import to_json_object `{
30 json_object *jobj;
31 const char *json_native_string;
32 String json_string;
33
34 jobj = Map_to_json_object( recv );
35 #ifdef JSON_C_TO_STRING_PRETTY
36 if ( pretty )
37 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PRETTY );
38 else
39 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PLAIN );
40 #else
41 json_native_string = json_object_to_json_string( jobj );
42 #endif
43 json_string = new_String_from_cstring( (char*)json_native_string );
44 return json_string;
45 `}
46
47 redef fun to_json_object
48 do
49 var jobj = new JsonObject
50
51 var iter = iterator
52 while iter.is_ok do
53 var key = iter.key
54 if key isa String then
55 var val = iter.item
56 if val isa Jsonable then
57 var jsubobj = val.to_json_object
58 jobj.add( key, jsubobj )
59 else if val == null then
60 jobj.add( key, null )
61 else
62 print "WARNING: value \"{val}\" not jsonable, cannot be converted to json."
63 end
64 else
65 print "WARNING: key \"{key}\" not a string, cannot be converted to json."
66 end
67
68 iter.next
69 end
70 return jobj
71 end
72 end
73
74 redef class Sequence[ E ]
75 redef fun to_json_object
76 do
77 var jarray = new JsonArray
78 for e in self do
79 if e isa nullable Jsonable then
80 if e == null then
81 jarray.push( null )
82 else
83 var obj = e.to_json_object
84 jarray.push( obj )
85 end
86 else
87 print "WARNING: element \"{e}\" not a Jsonable, cannot be converted to json."
88 end
89 end
90
91 return jarray
92 end
93 end
94
95 redef class String
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 );
99 `}
100 end
101
102 redef class Int
103 redef fun to_json_object `{
104 return json_object_new_int( recv );
105 `}
106 end
107
108 redef class Bool
109 redef fun to_json_object `{
110 return json_object_new_boolean( recv );
111 `}
112 end
113
114 redef class Float
115 redef fun to_json_object `{
116 return json_object_new_double( recv );
117 `}
118 end
119
120 redef class JsonObject
121 new `{ return json_object_new_object(); `}
122
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 `{
125 char* native_key;
126
127 native_key = String_to_cstring( key );
128
129 if ( JsonObject_is_null(val) ) {
130 json_object_object_add( recv, native_key, NULL );
131 } else {
132 json_object *jobj;
133 jobj = JsonObject_as_not_null( val );
134 json_object_object_add( recv, native_key, jobj );
135 }
136 `}
137 end
138
139 private extern JsonArray
140 super JsonObject
141
142 new `{ return json_object_new_array(); `}
143
144 fun push( val : nullable JsonObject ) `{
145 if ( JsonObject_is_null(val) )
146 json_object_array_add( recv, NULL );
147 else
148 json_object_array_add( recv, JsonObject_as_not_null(val) );
149 `}
150 end