From: Alexis Laferrière Date: Thu, 16 May 2013 01:01:07 +0000 (-0400) Subject: lib/json: separate the pretty print from libjson0 v0.10 X-Git-Tag: v0.6~48^2~4 X-Git-Url: http://nitlanguage.org lib/json: separate the pretty print from libjson0 v0.10 Signed-off-by: Alexis Laferrière --- diff --git a/lib/json/json_writer.nit b/lib/json/json_writer.nit index 81946f3..d568140 100644 --- a/lib/json/json_writer.nit +++ b/lib/json/json_writer.nit @@ -23,16 +23,23 @@ end # 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; `} diff --git a/tests/sav/test_json.res b/tests/sav/test_json.res index a20ecbf..7dde8c1 100644 --- a/tests/sav/test_json.res +++ b/tests/sav/test_json.res @@ -1,40 +1,11 @@ ../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 - } -} diff --git a/tests/test_json.nit b/tests/test_json.nit index 1a06290..8697c35 100644 --- a/tests/test_json.nit +++ b/tests/test_json.nit @@ -39,8 +39,10 @@ redef class String 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