lib/file: fix border-use-cases of String::basename
[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 in "C Header" `{
19 #define __STRICT_ANSI__
20 #include <json/json.h>
21 `}
22
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
26 end
27
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)
33
34 fun native_to_json( pretty: Bool ): String import to_json_object, NativeString.to_s `{
35 json_object *jobj;
36 const char *json_native_string;
37 String json_string;
38
39 jobj = Map_of_Object_nullable_Object_to_json_object( recv );
40 #ifdef JSON_C_TO_STRING_PRETTY
41 if ( pretty )
42 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PRETTY );
43 else
44 json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PLAIN );
45 #else
46 json_native_string = json_object_to_json_string( jobj );
47 #endif
48 json_string = NativeString_to_s( (char*)json_native_string );
49 return json_string;
50 `}
51
52 redef fun to_json_object
53 do
54 var jobj = new JsonObject
55
56 var iter = iterator
57 while iter.is_ok do
58 var key = iter.key
59 if key isa String then
60 var val = iter.item
61 if val isa Jsonable then
62 var jsubobj = val.to_json_object
63 jobj.add( key, jsubobj )
64 else if val == null then
65 jobj.add( key, null )
66 else
67 print "WARNING: value \"{val}\" not jsonable, cannot be converted to json."
68 end
69 else
70 print "WARNING: key \"{key}\" not a string, cannot be converted to json."
71 end
72
73 iter.next
74 end
75 return jobj
76 end
77 end
78
79 redef class SequenceRead[ E ]
80 redef fun to_json_object
81 do
82 var jarray = new JsonArray
83 for e in self do
84 if e isa nullable Jsonable then
85 if e == null then
86 jarray.push( null )
87 else
88 var obj = e.to_json_object
89 jarray.push( obj )
90 end
91 else
92 print "WARNING: element \"{e}\" not a Jsonable, cannot be converted to json."
93 end
94 end
95
96 return jarray
97 end
98 end
99
100 redef class String
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 );
104 `}
105 end
106
107 redef class Int
108 redef fun to_json_object `{
109 return json_object_new_int( recv );
110 `}
111 end
112
113 redef class Bool
114 redef fun to_json_object `{
115 return json_object_new_boolean( recv );
116 `}
117 end
118
119 redef class Float
120 redef fun to_json_object `{
121 return json_object_new_double( recv );
122 `}
123 end
124
125 redef class JsonObject
126 new `{ return json_object_new_object(); `}
127
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 `{
130 char* native_key;
131
132 native_key = String_to_cstring( key );
133
134 if ( nullable_JsonObject_is_null(val) ) {
135 json_object_object_add( recv, native_key, NULL );
136 } else {
137 json_object *jobj;
138 jobj = nullable_JsonObject_as_JsonObject( val );
139 json_object_object_add( recv, native_key, jobj );
140 }
141 `}
142 end
143
144 private extern JsonArray
145 super JsonObject
146
147 new `{ return json_object_new_array(); `}
148
149 fun push( val : nullable JsonObject ) `{
150 if ( nullable_JsonObject_is_null(val) )
151 json_object_array_add( recv, NULL );
152 else
153 json_object_array_add( recv, nullable_JsonObject_as_JsonObject(val) );
154 `}
155 end