738 |
738 |
739 // write it to a buffer |
739 // write it to a buffer |
740 CxBuffer buf; |
740 CxBuffer buf; |
741 cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT); |
741 cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT); |
742 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL); |
742 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL); |
|
743 cxBufferTerminate(&buf); // makes debugging easier |
|
744 CX_TEST_ASSERT(result == 0); |
|
745 |
|
746 // compare the string |
|
747 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
|
748 |
|
749 // destroy everything |
|
750 cxBufferDestroy(&buf); |
|
751 cxJsonValueFree(obj); |
|
752 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
753 } |
|
754 cx_testing_allocator_destroy(&talloc); |
|
755 } |
|
756 |
|
757 CX_TEST(test_json_write_pretty_default_spaces) { |
|
758 CxTestingAllocator talloc; |
|
759 cx_testing_allocator_init(&talloc); |
|
760 CxAllocator *allocator = &talloc.base; |
|
761 CX_TEST_DO { |
|
762 // expected value |
|
763 cxstring expected = CX_STR( |
|
764 "{\n" |
|
765 " \"bool\": false,\n" |
|
766 " \"int\": 47,\n" |
|
767 " \"nested\": {\n" |
|
768 " \"floats\": [3.1415, 47.11, 8.15],\n" |
|
769 " \"ints\": [4, 8, 15, [16, 23], 42],\n" |
|
770 " \"literals\": [true, null, false],\n" |
|
771 " \"objects\": [{\n" |
|
772 " \"name1\": 1,\n" |
|
773 " \"name2\": 3\n" |
|
774 " }, {\n" |
|
775 " \"name1\": 3,\n" |
|
776 " \"name2\": 7\n" |
|
777 " }]\n" |
|
778 " },\n" |
|
779 " \"strings\": [\"hello\", \"world\"]\n" |
|
780 "}" |
|
781 ); |
|
782 |
|
783 // create the value |
|
784 CxJsonValue *obj = cxJsonCreateObj(allocator); |
|
785 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
|
786 cxJsonObjPutNumber(obj, CX_STR("int"), 47); // purposely use PutNumber to put an int |
|
787 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
|
788 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
|
789 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
|
790 CxJsonValue *objects = cxJsonObjPutArr(nested, CX_STR("objects")); |
|
791 CxJsonValue *obj_in_arr[2] = {cxJsonCreateObj(allocator), cxJsonCreateObj(allocator)}; |
|
792 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name1"), 1); |
|
793 cxJsonObjPutInteger(obj_in_arr[0], CX_STR("name2"), 3); |
|
794 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name1"), 3); |
|
795 cxJsonObjPutInteger(obj_in_arr[1], CX_STR("name2"), 7); |
|
796 cxJsonArrAddValues(objects, obj_in_arr, 2); |
|
797 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
|
798 (double[]){3.1415, 47.11, 8.15}, 3); |
|
799 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
|
800 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
|
801 CxJsonValue *ints = cxJsonObjPutArr(nested, CX_STR("ints")); |
|
802 cxJsonArrAddIntegers(ints, (int64_t[]){4, 8, 15}, 3); |
|
803 CxJsonValue *nested_array = cxJsonCreateArr(allocator); |
|
804 cxJsonArrAddValues(ints, &nested_array, 1); |
|
805 cxJsonArrAddIntegers(nested_array, (int64_t[]){16, 23}, 2); |
|
806 cxJsonArrAddIntegers(ints, (int64_t[]){42}, 1); |
|
807 |
|
808 // write it to a buffer |
|
809 CxBuffer buf; |
|
810 cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT); |
|
811 CxJsonWriter writer = cxJsonWriterPretty(true); |
|
812 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, &writer); |
|
813 cxBufferTerminate(&buf); // makes debugging easier |
743 CX_TEST_ASSERT(result == 0); |
814 CX_TEST_ASSERT(result == 0); |
744 |
815 |
745 // compare the string |
816 // compare the string |
746 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
817 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
747 |
818 |