| 942 CX_TEST(test_json_write_string_escape) { |
942 CX_TEST(test_json_write_string_escape) { |
| 943 /** |
943 /** |
| 944 * According to RFC-8259 we have to test the following characters: |
944 * According to RFC-8259 we have to test the following characters: |
| 945 * " quotation mark |
945 * " quotation mark |
| 946 * \ reverse solidus |
946 * \ reverse solidus |
| 947 * / solidus |
947 * / solidus ---> we make this optional, see test_json_write_solidus |
| 948 * b backspace |
948 * b backspace |
| 949 * f form feed |
949 * f form feed |
| 950 * n line feed |
950 * n line feed |
| 951 * r carriage return |
951 * r carriage return |
| 952 * t tab |
952 * t tab |
| 953 * And all other control characters must be encoded uXXXX - in our example the bell character. |
953 * And all other control characters must be encoded uXXXX - in our example the bell character. |
| 954 * Also, all unicode characters are encoded that way - in our example the 'ö'. |
954 * Also, all unicode characters are encoded that way - in our example the 'ö'. |
| 955 */ |
955 */ |
| 956 CxJsonValue* str = cxJsonCreateString(NULL, |
956 CxJsonValue* str = cxJsonCreateString(NULL, |
| 957 "hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f"); |
957 "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"); |
| 958 CxJsonWriter writer = cxJsonWriterCompact(); |
958 CxJsonWriter writer = cxJsonWriterCompact(); |
| 959 CxBuffer buf; |
959 CxBuffer buf; |
| 960 cxBufferInit(&buf, NULL, 128, NULL, 0); |
960 cxBufferInit(&buf, NULL, 128, NULL, 0); |
| 961 CX_TEST_DO { |
961 CX_TEST_DO { |
| 962 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
962 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
| 963 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
963 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
| 964 CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""))); |
964 CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""))); |
| 965 } |
965 } |
| 966 cxBufferDestroy(&buf); |
966 cxBufferDestroy(&buf); |
| 967 cxJsonValueFree(str); |
967 cxJsonValueFree(str); |
| 968 } |
968 } |
| 969 |
969 |
| 970 CX_TEST(test_json_write_name_escape) { |
970 CX_TEST(test_json_write_name_escape) { |
| 971 CxJsonValue* obj = cxJsonCreateObj(NULL); |
971 CxJsonValue* obj = cxJsonCreateObj(NULL); |
| 972 cxJsonObjPutLiteral(obj, |
972 cxJsonObjPutLiteral(obj, |
| 973 CX_STR("hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE); |
973 CX_STR("hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE); |
| 974 CxJsonWriter writer = cxJsonWriterCompact(); |
974 CxJsonWriter writer = cxJsonWriterCompact(); |
| 975 CxBuffer buf; |
975 CxBuffer buf; |
| 976 cxBufferInit(&buf, NULL, 128, NULL, 0); |
976 cxBufferInit(&buf, NULL, 128, NULL, 0); |
| 977 CX_TEST_DO { |
977 CX_TEST_DO { |
| 978 cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer); |
978 cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer); |
| 979 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
979 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
| 980 CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"))); |
980 CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"))); |
| 981 } |
981 } |
| 982 cxBufferDestroy(&buf); |
982 cxBufferDestroy(&buf); |
| 983 cxJsonValueFree(obj); |
983 cxJsonValueFree(obj); |
| |
984 } |
| |
985 |
| |
986 CX_TEST(test_json_write_solidus) { |
| |
987 CxJsonValue* str = cxJsonCreateString(NULL,"test/solidus"); |
| |
988 CxJsonWriter writer = cxJsonWriterCompact(); |
| |
989 CxBuffer buf; |
| |
990 cxBufferInit(&buf, NULL, 16, NULL, 0); |
| |
991 CX_TEST_DO { |
| |
992 // default: do not escape |
| |
993 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
| |
994 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test/solidus\""))); |
| |
995 |
| |
996 // enable escaping |
| |
997 writer.escape_slash = true; |
| |
998 cxBufferReset(&buf); |
| |
999 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
| |
1000 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test\\/solidus\""))); |
| |
1001 } |
| |
1002 cxBufferDestroy(&buf); |
| |
1003 cxJsonValueFree(str); |
| 984 } |
1004 } |
| 985 |
1005 |
| 986 CxTestSuite *cx_test_suite_json(void) { |
1006 CxTestSuite *cx_test_suite_json(void) { |
| 987 CxTestSuite *suite = cx_test_suite_new("json"); |
1007 CxTestSuite *suite = cx_test_suite_new("json"); |
| 988 |
1008 |