--- a/tests/test_json.c Fri Jan 03 17:16:49 2025 +0100 +++ b/tests/test_json.c Fri Jan 03 19:18:00 2025 +0100 @@ -730,8 +730,8 @@ CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); + cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); - cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); cxJsonArrAddValues(objects, obj_in_arr, 2); cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), (double[]){3.1415, 47.11, 8.15}, 3); @@ -787,7 +787,6 @@ CxJsonWriter writer = cxJsonWriterCompact(); CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); - CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } @@ -820,7 +819,6 @@ CxJsonWriter writer = cxJsonWriterPretty(true); CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); - CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } @@ -856,6 +854,39 @@ cx_testing_allocator_destroy(&talloc); } +CX_TEST(test_json_write_pretty_preserve_order) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + cxstring expected = CX_STR( +"{\n" +" \"bool\": false,\n" +" \"int\": 47,\n" +" \"strings\": [\"hello\", \"world\"],\n" +" \"nested\": {\n" +" \"objects\": [{\n" +" \"name1\": 1,\n" +" \"name2\": 3\n" +" }, {\n" +" \"name2\": 7,\n" +" \"name1\": 3\n" +" }],\n" +" \"floats\": [3.1415, 47.11, 8.15],\n" +" \"literals\": [true, null, false],\n" +" \"ints\": [4, 8, 15, [16, 23], 42]\n" +" }\n" +"}" + ); + + CxJsonWriter writer = cxJsonWriterPretty(true); + writer.sort_members = false; + CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + CxTestSuite *cx_test_suite_json(void) { CxTestSuite *suite = cx_test_suite_new("json"); @@ -877,6 +908,7 @@ cx_test_register(suite, test_json_write_default_format); cx_test_register(suite, test_json_write_pretty_default_spaces); cx_test_register(suite, test_json_write_pretty_default_tabs); + cx_test_register(suite, test_json_write_pretty_preserve_order); return suite; }