Sun, 14 Dec 2025 15:45:01 +0100
change signatures of cxJsonToString() and cxJsonToPrettyString()
relates to #780
| docs/Writerside/topics/json.h.md | file | annotate | diff | comparison | revisions | |
| src/cx/json.h | file | annotate | diff | comparison | revisions | |
| src/json.c | file | annotate | diff | comparison | revisions | |
| tests/test_json.c | file | annotate | diff | comparison | revisions |
--- a/docs/Writerside/topics/json.h.md Sun Dec 14 15:41:02 2025 +0100 +++ b/docs/Writerside/topics/json.h.md Sun Dec 14 15:45:01 2025 +0100 @@ -377,11 +377,11 @@ int cxJsonWrite(void* target, const CxJsonValue* value, cx_write_func wfunc, const CxJsonWriter* settings); -cxmutstr cxJsonToString(CxJsonValue *value, - const CxAllocator *allocator); +cxmutstr cxJsonToString( + const CxAllocator *allocator, CxJsonValue *value); -cxmutstr cxJsonToPrettyString(CxJsonValue *value, - const CxAllocator *allocator); +cxmutstr cxJsonToPrettyString( + const CxAllocator *allocator, CxJsonValue *value); ``` A JSON value can be formatted with the `cxJsonWrite()` function.
--- a/src/cx/json.h Sun Dec 14 15:41:02 2025 +0100 +++ b/src/cx/json.h Sun Dec 14 15:45:01 2025 +0100 @@ -476,28 +476,28 @@ /** * Produces a compact string representation of the specified JSON value. * + * @param allocator the allocator for the string * @param value the JSON value - * @param allocator the allocator for the string * @return the produced string * @see cxJsonWrite() * @see cxJsonWriterCompact() * @see cxJsonToPrettyString() */ -cx_attr_nonnull_arg(1) -CX_EXPORT cxmutstr cxJsonToString(CxJsonValue *value, const CxAllocator *allocator); +cx_attr_nonnull_arg(2) +CX_EXPORT cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value); /** * Produces a pretty string representation of the specified JSON value. * + * @param allocator the allocator for the string * @param value the JSON value - * @param allocator the allocator for the string * @return the produced string * @see cxJsonWrite() * @see cxJsonWriterPretty() * @see cxJsonToString() */ -cx_attr_nonnull_arg(1) -CX_EXPORT cxmutstr cxJsonToPrettyString(CxJsonValue *value, const CxAllocator *allocator); +cx_attr_nonnull_arg(2) +CX_EXPORT cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value); /** * Initializes the JSON interface.
--- a/src/json.c Sun Dec 14 15:41:02 2025 +0100 +++ b/src/json.c Sun Dec 14 15:45:01 2025 +0100 @@ -1443,12 +1443,12 @@ } -cxmutstr cxJsonToString(CxJsonValue *value, const CxAllocator *allocator) { +cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) { CxJsonWriter writer = cxJsonWriterCompact(); return cx_json_to_string(value, allocator, &writer); } -cxmutstr cxJsonToPrettyString(CxJsonValue *value, const CxAllocator *allocator) { +cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) { CxJsonWriter writer = cxJsonWriterPretty(true); return cx_json_to_string(value, allocator, &writer); }
--- a/tests/test_json.c Sun Dec 14 15:41:02 2025 +0100 +++ b/tests/test_json.c Sun Dec 14 15:45:01 2025 +0100 @@ -1601,8 +1601,8 @@ CX_TEST_ASSERT(cxJsonCompare(a[i], b) == 0); // alternative comparison using cxJsonToString - cxmutstr aStr = cxJsonToString(a[i], NULL); - cxmutstr bStr = cxJsonToString(b, NULL); + cxmutstr aStr = cxJsonToString(NULL, a[i]); + cxmutstr bStr = cxJsonToString(NULL, b); CX_TEST_ASSERT(cx_strcmp(aStr, bStr) == 0); cxFree(cxDefaultAllocator, aStr.ptr); cxFree(cxDefaultAllocator, bStr.ptr); @@ -1685,8 +1685,8 @@ CX_TEST_ASSERT(b->type == a[i]->type); CX_TEST_ASSERT(cxJsonCompare(a[i], b) == 0); - cxmutstr a_str = cxJsonToString(a[i], NULL); - cxmutstr b_str = cxJsonToString(b, NULL); + cxmutstr a_str = cxJsonToString(NULL, a[i]); + cxmutstr b_str = cxJsonToString(NULL, b); CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0); cx_strfree(&a_str); cx_strfree(&b_str); @@ -1718,8 +1718,8 @@ CX_TEST_ASSERT(cxJsonIsArray(b)); CX_TEST_ASSERT(cxJsonCompare(a[i], b) == 0); - cxmutstr a_str = cxJsonToString(a[i], NULL); - cxmutstr b_str = cxJsonToString(b, NULL); + cxmutstr a_str = cxJsonToString(NULL, a[i]); + cxmutstr b_str = cxJsonToString(NULL, b); CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0); cx_strfree(&a_str); cx_strfree(&b_str); @@ -2084,7 +2084,7 @@ ); CxJsonValue *obj = test_json_write_create_test_object(allocator); - cxmutstr result = cxJsonToString(obj, allocator); + cxmutstr result = cxJsonToString(allocator, obj); CX_TEST_ASSERT(0 == cx_strcmp(result, expected)); CX_TEST_ASSERT(result.ptr[result.length] == '\0'); @@ -2122,7 +2122,7 @@ ); CxJsonValue *obj = test_json_write_create_test_object(allocator); - cxmutstr result = cxJsonToPrettyString(obj, allocator); + cxmutstr result = cxJsonToPrettyString(allocator, obj); CX_TEST_ASSERT(0 == cx_strcmp(result, expected)); CX_TEST_ASSERT(result.ptr[result.length] == '\0');