aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/json.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-01-18 00:40:38 +0100
committerJay Berkenbilt <ejb@ql.org>2022-01-30 19:11:03 +0100
commite8e8f6f43c760523520dfe7a5c76d88c959599f6 (patch)
treea000bcb5cbbde3e2833a57a28e761449b62165b2 /libtests/json.cc
parentb9af421ef788b94fcbf18ecf702cec3ba9a018b7 (diff)
downloadqpdf-e8e8f6f43c760523520dfe7a5c76d88c959599f6.tar.zst
Add JSON::parse
Diffstat (limited to 'libtests/json.cc')
-rw-r--r--libtests/json.cc136
1 files changed, 76 insertions, 60 deletions
diff --git a/libtests/json.cc b/libtests/json.cc
index d788ab4e..7bea5589 100644
--- a/libtests/json.cc
+++ b/libtests/json.cc
@@ -91,67 +91,83 @@ static void check_schema(JSON& obj, JSON& schema, bool exp,
static void test_schema()
{
- // Since we don't have a JSON parser, use the PDF parser as a
- // shortcut for creating a complex JSON structure.
- JSON schema = QPDFObjectHandle::parse(
- "<<"
- " /one <<"
- " /a <<"
- " /q (queue)"
- " /r <<"
- " /x (ecks)"
- " /y (why)"
- " >>"
- " /s [ (esses) ]"
- " >>"
- " >>"
- " /two ["
- " <<"
- " /goose (gander)"
- " /glarp (enspliel)"
- " >>"
- " ]"
- ">>").getJSON();
- JSON three = JSON::makeDictionary();
- three.addDictionaryMember(
- "<objid>",
- QPDFObjectHandle::parse("<< /z (ebra) >>").getJSON());
- schema.addDictionaryMember("/three", three);
- JSON a = QPDFObjectHandle::parse("[(not a) (dictionary)]").getJSON();
+ JSON schema = JSON::parse(R"(
+{
+ "one": {
+ "a": {
+ "q": "queue",
+ "r": {
+ "x": "ecks",
+ "y": "(bool) why"
+ },
+ "s": [
+ "esses"
+ ]
+ }
+ },
+ "two": [
+ {
+ "goose": "gander",
+ "glarp": "enspliel"
+ }
+ ],
+ "three": {
+ "<objid>": {
+ "z": "ebra",
+ "o": "(optional, string) optional"
+ }
+ }
+}
+)");
+
+ JSON a = JSON::parse(R"(["not a", "dictionary"])");
check_schema(a, schema, false, "top-level type mismatch");
- JSON b = QPDFObjectHandle::parse(
- "<<"
- " /one <<"
- " /a <<"
- " /t (oops)"
- " /r ["
- " /x (ecks)"
- " /y (why)"
- " ]"
- " /s << /z (esses) >>"
- " >>"
- " >>"
- " /two ["
- " <<"
- " /goose (0 gander)"
- " /glarp (0 enspliel)"
- " >>"
- " <<"
- " /goose (1 gander)"
- " /flarp (1 enspliel)"
- " >>"
- " 2"
- " [ (three) ]"
- " <<"
- " /goose (4 gander)"
- " /glarp (4 enspliel)"
- " >>"
- " ]"
- " /three <<"
- " /anything << /x (oops) >>"
- " /else << /z (okay) >>"
- " >>"
- ">>").getJSON();
+ JSON b = JSON::parse(R"(
+{
+ "one": {
+ "a": {
+ "t": "oops",
+ "r": [
+ "x",
+ "ecks",
+ "y",
+ "why"
+ ],
+ "s": {
+ "z": "esses"
+ }
+ }
+ },
+ "two": [
+ {
+ "goose": "0 gander",
+ "glarp": "0 enspliel"
+ },
+ {
+ "goose": "1 gander",
+ "flarp": "1 enspliel"
+ },
+ 2,
+ [
+ "three"
+ ],
+ {
+ "goose": "4 gander",
+ "glarp": 4
+ }
+ ],
+ "three": {
+ "anything": {
+ "x": "oops",
+ "o": "okay"
+ },
+ "else": {
+ "z": "okay"
+ }
+ }
+}
+)");
+
check_schema(b, schema, false, "missing items");
check_schema(a, a, false, "top-level schema array error");
check_schema(b, b, false, "lower-level schema array error");