Skip to content

Commit be58b3f

Browse files
committed
Fix JSON output with empty arrays or objects.
1 parent ba2c9da commit be58b3f

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ libcups v3.0.0 (YYYY-MM-DD)
1717
- Updated the `httpSetCookie` API to support multiple "Set-Cookie:" header
1818
values.
1919
- Updated `ippfind` to use the `cupsGetClock` API.
20+
- Fixed `cupsJSONExport` functions with empty arrays or objects.
2021
- Fixed `httpGetDateTime` for dates in the far future (Issue #124)
2122
- Fixed input checks for `cupsCreateCredentials` and
2223
`cupsCreateCredentialsRequest` APIs (Issue #125)

cups/json.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -428,32 +428,39 @@ cupsJSONExportString(cups_json_t *json) // I - JSON root node
428428
// Descend
429429
current = current->value.child;
430430
}
431-
else if (current->sibling)
432-
{
433-
// Visit silbling
434-
current = current->sibling;
435-
}
436-
else if ((current = current->parent) != NULL)
431+
else
437432
{
438-
// Ascend and continue...
433+
// Close out current array/object...
439434
if (current->type == CUPS_JTYPE_ARRAY)
440435
*ptr++ = ']';
441-
else
436+
else if (current->type == CUPS_JTYPE_OBJECT)
442437
*ptr++ = '}';
443438

444-
while (current)
439+
if (current->sibling)
445440
{
446-
if (current->sibling)
447-
{
448-
current = current->sibling;
449-
break;
450-
}
451-
else if ((current = current->parent) != NULL)
441+
// Visit silbling
442+
current = current->sibling;
443+
}
444+
else if ((current = current->parent) != NULL)
445+
{
446+
// Ascend and continue...
447+
while (current)
452448
{
449+
// Close out current array/object...
453450
if (current->type == CUPS_JTYPE_ARRAY)
454451
*ptr++ = ']';
455-
else
452+
else if (current->type == CUPS_JTYPE_OBJECT)
456453
*ptr++ = '}';
454+
455+
if (current->sibling)
456+
{
457+
current = current->sibling;
458+
break;
459+
}
460+
else
461+
{
462+
current = current->parent;
463+
}
457464
}
458465
}
459466
}

cups/testjson.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "cups.h"
1111
#include "json.h"
1212
#include "test-internal.h"
13+
#include <math.h>
1314

1415

1516
//
@@ -33,6 +34,7 @@ main(int argc, // I - Number of command-line arguments
3334
size_t count; // Number of children
3435
char *s; // JSON string
3536
time_t last_modified = 0; // Last-Modified value
37+
double number; // Number value
3638
static const char * const types[] = // Node types
3739
{
3840
"CUPS_JTYPE_NULL", // Null value
@@ -177,9 +179,29 @@ main(int argc, // I - Number of command-line arguments
177179
current = cupsJSONNewNumber(parent, current, 2);
178180
testEnd(current != NULL);
179181

182+
testBegin("cupsJSONNewKey('emptyArray')");
183+
current = cupsJSONNewKey(json, NULL, "emptyArray");
184+
testEnd(current != NULL);
185+
186+
testBegin("cupsJSONNew(array)");
187+
parent = cupsJSONNew(json, current, CUPS_JTYPE_ARRAY);
188+
testEnd(parent != NULL);
189+
190+
testBegin("cupsJSONNewKey('emptyObject')");
191+
current = cupsJSONNewKey(json, NULL, "emptyObject");
192+
testEnd(current != NULL);
193+
194+
testBegin("cupsJSONNew(object)");
195+
parent = cupsJSONNew(json, current, CUPS_JTYPE_OBJECT);
196+
testEnd(parent != NULL);
197+
180198
testBegin("cupsJSONGetCount(root)");
181199
count = cupsJSONGetCount(json);
182-
testEndMessage(count == 14, "%u", (unsigned)count);
200+
testEndMessage(count == 18, "%u", (unsigned)count);
201+
202+
testBegin("cupsJSONFind('number')");
203+
number = cupsJSONGetNumber(cupsJSONFind(json, "number"));
204+
testEndMessage(fabs(number-42.0) < 0.01, "%g", number);
183205

184206
testBegin("cupsJSONExportFile(root, 'test.json')");
185207
if (cupsJSONExportFile(json, "test.json"))
@@ -206,6 +228,10 @@ main(int argc, // I - Number of command-line arguments
206228
parent = cupsJSONImportString(s);
207229
testEnd(parent != NULL);
208230

231+
testBegin("cupsJSONGetCount(imported)");
232+
count = cupsJSONGetCount(parent);
233+
testEndMessage(count == 18, "%u", (unsigned)count);
234+
209235
cupsJSONDelete(parent);
210236
free(s);
211237
}
@@ -224,8 +250,16 @@ main(int argc, // I - Number of command-line arguments
224250
if (json)
225251
{
226252
char last_modified_date[256];// Last-Modified string value
253+
const char *jwks_uri; // jwks_uri value
227254

228255
testEnd(true);
256+
257+
testBegin("cupsJSONFind('jwks_uri')");
258+
if ((jwks_uri = cupsJSONGetString(cupsJSONFind(json, "jwks_uri"))) != NULL)
259+
testEndMessage(true, "%s", jwks_uri);
260+
else
261+
testEnd(false);
262+
229263
cupsJSONDelete(json);
230264

231265
testBegin("cupsJSONImportURL('https://accounts.google.com/.well-known/openid-configuration', since %s)", httpGetDateString(last_modified, last_modified_date, sizeof(last_modified_date)));

0 commit comments

Comments
 (0)