mirror of
https://github.com/json-c/json-c.git
synced 2026-03-31 02:49:06 +08:00
Add a json_c_set_serialization_double_format() function to set the *library-wide* format for how doubles are written to a serialized JSON output.
This commit is contained in:
@@ -27,5 +27,31 @@ int main()
|
||||
json_object_set_serializer(obj, NULL, NULL, NULL);
|
||||
printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
|
||||
|
||||
json_object_put(obj);
|
||||
obj = json_object_new_double(0.52381);
|
||||
|
||||
printf("obj.to_string(default format)=%s\n", json_object_to_json_string(obj));
|
||||
if (json_c_set_serialization_double_format("x%0.3fy", JSON_C_OPTION_GLOBAL) < 0)
|
||||
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||||
printf("obj.to_string(with global format)=%s\n", json_object_to_json_string(obj));
|
||||
#ifdef HAVE___THREAD
|
||||
if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
|
||||
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||||
printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
|
||||
if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX", JSON_C_OPTION_THREAD) < 0)
|
||||
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||||
printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
|
||||
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
|
||||
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||||
printf("obj.to_string(back to global format)=%s\n", json_object_to_json_string(obj));
|
||||
#else
|
||||
// Just fake it up, so the output matches.
|
||||
printf("obj.to_string(with thread format)=%s\n", "T0.52X");
|
||||
printf("obj.to_string(back to global format)=%s\n", "x0.524y");
|
||||
#endif
|
||||
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
|
||||
printf("ERROR: json_c_set_serialization_double_format() failed");
|
||||
printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
|
||||
|
||||
json_object_put(obj);
|
||||
}
|
||||
|
||||
@@ -6,3 +6,6 @@ Test explicit serializer with custom userdata:
|
||||
obj.to_string(custom)=test
|
||||
Test reset serializer:
|
||||
obj.to_string(reset)=0.5
|
||||
obj.to_string(default format)=0.52381
|
||||
obj.to_string(with global format)=x0.524y
|
||||
obj.to_string(with thread format)=T0.52X
|
||||
|
||||
Reference in New Issue
Block a user