Skip to content

Commit 6642068

Browse files
committed
Fix JSON output with empty arrays or objects (Issue #1452)
1 parent 5bee93f commit 6642068

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

cups/json.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -434,32 +434,39 @@ cupsJSONExportString(cups_json_t *json) // I - JSON root node
434434
// Descend
435435
current = current->value.child;
436436
}
437-
else if (current->sibling)
438-
{
439-
// Visit silbling
440-
current = current->sibling;
441-
}
442-
else if ((current = current->parent) != NULL)
437+
else
443438
{
444-
// Ascend and continue...
439+
// Close out current array/object...
445440
if (current->type == CUPS_JTYPE_ARRAY)
446441
*ptr++ = ']';
447-
else
442+
else if (current->type == CUPS_JTYPE_OBJECT)
448443
*ptr++ = '}';
449444

450-
while (current)
445+
if (current->sibling)
451446
{
452-
if (current->sibling)
453-
{
454-
current = current->sibling;
455-
break;
456-
}
457-
else if ((current = current->parent) != NULL)
447+
// Visit silbling
448+
current = current->sibling;
449+
}
450+
else if ((current = current->parent) != NULL)
451+
{
452+
// Ascend and continue...
453+
while (current)
458454
{
455+
// Close out current array/object...
459456
if (current->type == CUPS_JTYPE_ARRAY)
460457
*ptr++ = ']';
461-
else
458+
else if (current->type == CUPS_JTYPE_OBJECT)
462459
*ptr++ = '}';
460+
461+
if (current->sibling)
462+
{
463+
current = current->sibling;
464+
break;
465+
}
466+
else
467+
{
468+
current = current->parent;
469+
}
463470
}
464471
}
465472
}

cups/testjson.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// JSON API unit tests for CUPS.
33
//
4-
// Copyright © 2022-2024 by OpenPrinting.
4+
// Copyright © 2022-2025 by OpenPrinting.
55
//
66
// Licensed under Apache License v2.0. See the file "LICENSE" for more
77
// information.
@@ -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)", httpGetDateString2(last_modified, last_modified_date, sizeof(last_modified_date)));

0 commit comments

Comments
 (0)