728 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
728 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
729 CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
729 CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
730 CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
730 CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
731 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
731 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
732 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
732 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
|
733 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
733 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
734 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
734 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
|
735 cxJsonArrAddValues(objects, obj_in_arr, 2); |
735 cxJsonArrAddValues(objects, obj_in_arr, 2); |
736 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
736 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
737 (double[]){3.1415, 47.11, 8.15}, 3); |
737 (double[]){3.1415, 47.11, 8.15}, 3); |
738 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
738 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
739 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
739 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
785 ); |
785 ); |
786 |
786 |
787 CxJsonWriter writer = cxJsonWriterCompact(); |
787 CxJsonWriter writer = cxJsonWriterCompact(); |
788 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
788 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
789 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
789 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
790 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
791 } |
790 } |
792 cx_testing_allocator_destroy(&talloc); |
791 cx_testing_allocator_destroy(&talloc); |
793 } |
792 } |
794 |
793 |
795 CX_TEST(test_json_write_pretty_default_spaces) { |
794 CX_TEST(test_json_write_pretty_default_spaces) { |
817 "}" |
816 "}" |
818 ); |
817 ); |
819 |
818 |
820 CxJsonWriter writer = cxJsonWriterPretty(true); |
819 CxJsonWriter writer = cxJsonWriterPretty(true); |
821 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
820 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
822 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
823 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
821 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
824 } |
822 } |
825 cx_testing_allocator_destroy(&talloc); |
823 cx_testing_allocator_destroy(&talloc); |
826 } |
824 } |
827 |
825 |
854 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
852 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
855 } |
853 } |
856 cx_testing_allocator_destroy(&talloc); |
854 cx_testing_allocator_destroy(&talloc); |
857 } |
855 } |
858 |
856 |
|
857 CX_TEST(test_json_write_pretty_preserve_order) { |
|
858 CxTestingAllocator talloc; |
|
859 cx_testing_allocator_init(&talloc); |
|
860 CxAllocator *allocator = &talloc.base; |
|
861 CX_TEST_DO { |
|
862 cxstring expected = CX_STR( |
|
863 "{\n" |
|
864 " \"bool\": false,\n" |
|
865 " \"int\": 47,\n" |
|
866 " \"strings\": [\"hello\", \"world\"],\n" |
|
867 " \"nested\": {\n" |
|
868 " \"objects\": [{\n" |
|
869 " \"name1\": 1,\n" |
|
870 " \"name2\": 3\n" |
|
871 " }, {\n" |
|
872 " \"name2\": 7,\n" |
|
873 " \"name1\": 3\n" |
|
874 " }],\n" |
|
875 " \"floats\": [3.1415, 47.11, 8.15],\n" |
|
876 " \"literals\": [true, null, false],\n" |
|
877 " \"ints\": [4, 8, 15, [16, 23], 42]\n" |
|
878 " }\n" |
|
879 "}" |
|
880 ); |
|
881 |
|
882 CxJsonWriter writer = cxJsonWriterPretty(true); |
|
883 writer.sort_members = false; |
|
884 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); |
|
885 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
886 } |
|
887 cx_testing_allocator_destroy(&talloc); |
|
888 } |
|
889 |
859 CxTestSuite *cx_test_suite_json(void) { |
890 CxTestSuite *cx_test_suite_json(void) { |
860 CxTestSuite *suite = cx_test_suite_new("json"); |
891 CxTestSuite *suite = cx_test_suite_new("json"); |
861 |
892 |
862 cx_test_register(suite, test_json_init_default); |
893 cx_test_register(suite, test_json_init_default); |
863 cx_test_register(suite, test_json_simple_object); |
894 cx_test_register(suite, test_json_simple_object); |
875 cx_test_register(suite, test_json_allocator_parse_error); |
906 cx_test_register(suite, test_json_allocator_parse_error); |
876 cx_test_register(suite, test_json_create_value); |
907 cx_test_register(suite, test_json_create_value); |
877 cx_test_register(suite, test_json_write_default_format); |
908 cx_test_register(suite, test_json_write_default_format); |
878 cx_test_register(suite, test_json_write_pretty_default_spaces); |
909 cx_test_register(suite, test_json_write_pretty_default_spaces); |
879 cx_test_register(suite, test_json_write_pretty_default_tabs); |
910 cx_test_register(suite, test_json_write_pretty_default_tabs); |
|
911 cx_test_register(suite, test_json_write_pretty_preserve_order); |
880 |
912 |
881 return suite; |
913 return suite; |
882 } |
914 } |
883 |
915 |