# Will ignore non-jsonable
redef class Map[ K, V ]
# Get a json-formatted string of this map
- fun to_json( pretty: Bool ) : String import to_json_object `{
+ fun to_pretty_json: String do return native_to_json(true)
+ fun to_json: String do return native_to_json(false)
+
+ fun native_to_json( pretty: Bool ): String import to_json_object `{
json_object *jobj;
char *json_native_string;
String json_string;
jobj = Map_to_json_object( recv );
+#ifdef JSON_C_TO_STRING_PRETTY
if ( pretty )
json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PRETTY );
else
json_native_string = json_object_to_json_string_ext( jobj, JSON_C_TO_STRING_PLAIN );
+#else
+ json_native_string = json_object_to_json_string( jobj );
+#endif
json_string = new_String_from_cstring( json_native_string );
return json_string;
`}
../lib/json/json_reader.nit: In function 'JsonObject_json_to_string___impl':
../lib/json/json_reader.nit:96:11: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
-../lib/json/json_writer.nit: In function 'Map_to_json___impl':
-../lib/json/json_writer.nit:34:23: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
-../lib/json/json_writer.nit:36:23: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
+../lib/json/json_writer.nit: In function 'Map_native_to_json___impl':
+../lib/json/json_writer.nit:38:23: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
+../lib/json/json_writer.nit:40:23: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
{"int":"1234","float":"0.1234","str":"str","null": null}
{"int":1234,"float":0.123400,"str":"str","null":null}
-{
- "int":1234,
- "float":0.123400,
- "str":"str",
- "null":null
-}
{"arr":"123","obj":"{"int":"123","float":"-234.449997"}"}
{"arr":[1,2,3],"obj":{"int":123,"float":-234.449997}}
-{
- "arr":[
- 1,
- 2,
- 3
- ],
- "obj":{
- "int":123,
- "float":-234.449997
- }
-}
{"arr":"12.3str","obj":"{"int":"123","float":"-234.449997"}"}
{"arr":[1,2.300000,null,"str"],"obj":{"int":123,"float":-234.449997}}
-{
- "arr":[
- 1,
- 2.300000,
- null,
- "str"
- ],
- "obj":{
- "int":123,
- "float":-234.449997
- }
-}
var json_map = json_to_object
if json_map != null then
print json_map
- print json_map.to_json(false)
- print json_map.to_json(true)
+ print json_map.to_json
+
+ # only available for libjson0 v0.10
+ # print json_map.to_pretty_json
else
print "Conversion to json failed."
end