// Descend
current = current->value.child;
}
- else if (current->sibling)
- {
- // Visit silbling
- current = current->sibling;
- }
- else if ((current = current->parent) != NULL)
+ else
{
- // Ascend and continue...
+ // Close out current array/object...
if (current->type == CUPS_JTYPE_ARRAY)
*ptr++ = ']';
- else
+ else if (current->type == CUPS_JTYPE_OBJECT)
*ptr++ = '}';
- while (current)
+ if (current->sibling)
{
- if (current->sibling)
- {
- current = current->sibling;
- break;
- }
- else if ((current = current->parent) != NULL)
+ // Visit silbling
+ current = current->sibling;
+ }
+ else if ((current = current->parent) != NULL)
+ {
+ // Ascend and continue...
+ while (current)
{
+ // Close out current array/object...
if (current->type == CUPS_JTYPE_ARRAY)
*ptr++ = ']';
- else
+ else if (current->type == CUPS_JTYPE_OBJECT)
*ptr++ = '}';
+
+ if (current->sibling)
+ {
+ current = current->sibling;
+ break;
+ }
+ else
+ {
+ current = current->parent;
+ }
}
}
}
//
// JSON API unit tests for CUPS.
//
-// Copyright © 2022-2024 by OpenPrinting.
+// Copyright © 2022-2025 by OpenPrinting.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
#include "cups.h"
#include "json.h"
#include "test-internal.h"
+#include <math.h>
//
size_t count; // Number of children
char *s; // JSON string
time_t last_modified = 0; // Last-Modified value
+ double number; // Number value
static const char * const types[] = // Node types
{
"CUPS_JTYPE_NULL", // Null value
current = cupsJSONNewNumber(parent, current, 2);
testEnd(current != NULL);
+ testBegin("cupsJSONNewKey('emptyArray')");
+ current = cupsJSONNewKey(json, NULL, "emptyArray");
+ testEnd(current != NULL);
+
+ testBegin("cupsJSONNew(array)");
+ parent = cupsJSONNew(json, current, CUPS_JTYPE_ARRAY);
+ testEnd(parent != NULL);
+
+ testBegin("cupsJSONNewKey('emptyObject')");
+ current = cupsJSONNewKey(json, NULL, "emptyObject");
+ testEnd(current != NULL);
+
+ testBegin("cupsJSONNew(object)");
+ parent = cupsJSONNew(json, current, CUPS_JTYPE_OBJECT);
+ testEnd(parent != NULL);
+
testBegin("cupsJSONGetCount(root)");
count = cupsJSONGetCount(json);
- testEndMessage(count == 14, "%u", (unsigned)count);
+ testEndMessage(count == 18, "%u", (unsigned)count);
+
+ testBegin("cupsJSONFind('number')");
+ number = cupsJSONGetNumber(cupsJSONFind(json, "number"));
+ testEndMessage(fabs(number-42.0) < 0.01, "%g", number);
testBegin("cupsJSONExportFile(root, 'test.json')");
if (cupsJSONExportFile(json, "test.json"))
parent = cupsJSONImportString(s);
testEnd(parent != NULL);
+ testBegin("cupsJSONGetCount(imported)");
+ count = cupsJSONGetCount(parent);
+ testEndMessage(count == 18, "%u", (unsigned)count);
+
cupsJSONDelete(parent);
free(s);
}
if (json)
{
char last_modified_date[256];// Last-Modified string value
+ const char *jwks_uri; // jwks_uri value
testEnd(true);
+
+ testBegin("cupsJSONFind('jwks_uri')");
+ if ((jwks_uri = cupsJSONGetString(cupsJSONFind(json, "jwks_uri"))) != NULL)
+ testEndMessage(true, "%s", jwks_uri);
+ else
+ testEnd(false);
+
cupsJSONDelete(json);
testBegin("cupsJSONImportURL('https://accounts.google.com/.well-known/openid-configuration', since %s)", httpGetDateString2(last_modified, last_modified_date, sizeof(last_modified_date)));