| 1333 CxJsonWriter writer = cxJsonWriterCompact(); |
1333 CxJsonWriter writer = cxJsonWriterCompact(); |
| 1334 CxBuffer buf; |
1334 CxBuffer buf; |
| 1335 cxBufferInit(&buf, NULL, 32, NULL, 0); |
1335 cxBufferInit(&buf, NULL, 32, NULL, 0); |
| 1336 CX_TEST_DO { |
1336 CX_TEST_DO { |
| 1337 // test default settings (6 digits) |
1337 // test default settings (6 digits) |
| 1338 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1338 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1339 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592")); |
1339 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592")); |
| 1340 |
1340 |
| 1341 // test too many digits |
1341 // test too many digits |
| 1342 cxBufferReset(&buf); |
1342 cxBufferReset(&buf); |
| 1343 writer.frac_max_digits = 200; |
1343 writer.frac_max_digits = 200; |
| 1344 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1344 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1345 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592653589793")); |
1345 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592653589793")); |
| 1346 |
1346 |
| 1347 // test 0 digits |
1347 // test 0 digits |
| 1348 cxBufferReset(&buf); |
1348 cxBufferReset(&buf); |
| 1349 writer.frac_max_digits = 0; |
1349 writer.frac_max_digits = 0; |
| 1350 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1350 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1351 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3")); |
1351 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3")); |
| 1352 |
1352 |
| 1353 // test 2 digits |
1353 // test 2 digits |
| 1354 cxBufferReset(&buf); |
1354 cxBufferReset(&buf); |
| 1355 writer.frac_max_digits = 2; |
1355 writer.frac_max_digits = 2; |
| 1356 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1356 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1357 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.14")); |
1357 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.14")); |
| 1358 |
1358 |
| 1359 // test 3 digits |
1359 // test 3 digits |
| 1360 cxBufferReset(&buf); |
1360 cxBufferReset(&buf); |
| 1361 writer.frac_max_digits = 3; |
1361 writer.frac_max_digits = 3; |
| 1362 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1362 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1363 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141")); |
1363 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141")); |
| 1364 |
1364 |
| 1365 // test 6 digits, but two are left of the decimal point |
1365 // test 6 digits, but two are left of the decimal point |
| 1366 num->number = 47.110815; |
1366 num->number = 47.110815; |
| 1367 cxBufferReset(&buf); |
1367 cxBufferReset(&buf); |
| 1368 writer.frac_max_digits = 6; |
1368 writer.frac_max_digits = 6; |
| 1369 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1369 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1370 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "47.110815")); |
1370 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "47.110815")); |
| 1371 |
1371 |
| 1372 // test 4 digits with exponent |
1372 // test 4 digits with exponent |
| 1373 num->number = 5.11223344e23; |
1373 num->number = 5.11223344e23; |
| 1374 cxBufferReset(&buf); |
1374 cxBufferReset(&buf); |
| 1375 writer.frac_max_digits = 4; |
1375 writer.frac_max_digits = 4; |
| 1376 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); |
1376 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer)); |
| 1377 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "5.1122e+23")); |
1377 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "5.1122e+23")); |
| 1378 } |
1378 } |
| 1379 cxBufferDestroy(&buf); |
1379 cxBufferDestroy(&buf); |
| 1380 cxJsonValueFree(num); |
1380 cxJsonValueFree(num); |
| 1381 } |
1381 } |
| 1398 "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"); |
1398 "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"); |
| 1399 CxJsonWriter writer = cxJsonWriterCompact(); |
1399 CxJsonWriter writer = cxJsonWriterCompact(); |
| 1400 CxBuffer buf; |
1400 CxBuffer buf; |
| 1401 cxBufferInit(&buf, NULL, 128, NULL, 0); |
1401 cxBufferInit(&buf, NULL, 128, NULL, 0); |
| 1402 CX_TEST_DO { |
1402 CX_TEST_DO { |
| 1403 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
1403 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer)); |
| 1404 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
1404 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
| 1405 "\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\"")); |
1405 "\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\"")); |
| 1406 } |
1406 } |
| 1407 cxBufferDestroy(&buf); |
1407 cxBufferDestroy(&buf); |
| 1408 cxJsonValueFree(str); |
1408 cxJsonValueFree(str); |
| 1414 "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f", CX_JSON_TRUE); |
1414 "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f", CX_JSON_TRUE); |
| 1415 CxJsonWriter writer = cxJsonWriterCompact(); |
1415 CxJsonWriter writer = cxJsonWriterCompact(); |
| 1416 CxBuffer buf; |
1416 CxBuffer buf; |
| 1417 cxBufferInit(&buf, NULL, 128, NULL, 0); |
1417 cxBufferInit(&buf, NULL, 128, NULL, 0); |
| 1418 CX_TEST_DO { |
1418 CX_TEST_DO { |
| 1419 cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer); |
1419 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer)); |
| 1420 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
1420 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), |
| 1421 "{\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}")); |
1421 "{\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}")); |
| 1422 } |
1422 } |
| 1423 cxBufferDestroy(&buf); |
1423 cxBufferDestroy(&buf); |
| 1424 cxJsonValueFree(obj); |
1424 cxJsonValueFree(obj); |
| 1429 CxJsonWriter writer = cxJsonWriterCompact(); |
1429 CxJsonWriter writer = cxJsonWriterCompact(); |
| 1430 CxBuffer buf; |
1430 CxBuffer buf; |
| 1431 cxBufferInit(&buf, NULL, 16, NULL, 0); |
1431 cxBufferInit(&buf, NULL, 16, NULL, 0); |
| 1432 CX_TEST_DO { |
1432 CX_TEST_DO { |
| 1433 // default: do not escape |
1433 // default: do not escape |
| 1434 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
1434 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer)); |
| 1435 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test/solidus\"")); |
1435 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test/solidus\"")); |
| 1436 |
1436 |
| 1437 // enable escaping |
1437 // enable escaping |
| 1438 writer.escape_slash = true; |
1438 writer.escape_slash = true; |
| 1439 cxBufferReset(&buf); |
1439 cxBufferReset(&buf); |
| 1440 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); |
1440 CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer)); |
| 1441 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test\\/solidus\"")); |
1441 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test\\/solidus\"")); |
| 1442 } |
1442 } |
| 1443 cxBufferDestroy(&buf); |
1443 cxBufferDestroy(&buf); |
| 1444 cxJsonValueFree(str); |
1444 cxJsonValueFree(str); |
| 1445 } |
1445 } |