summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2021-04-21 12:21:41 +0200
committerJulian Andres Klode <julian.klode@canonical.com>2021-04-23 12:25:40 +0200
commitb5211b9b213273c642a790ae3c3f3bbe1a4cf51e (patch)
tree64004aed27d7b5a4618aee12ed9b7df608ca343b
parent09e3c0c855e339216784a1d43715c3ae2d79ec23 (diff)
json: Encode NULL strings as null
This is the only nullable thing we have here.
-rw-r--r--apt-private/private-json-hooks.cc5
-rw-r--r--test/libapt/json_test.cc8
2 files changed, 12 insertions, 1 deletions
diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc
index ce1665b18..7991a7d77 100644
--- a/apt-private/private-json-hooks.cc
+++ b/apt-private/private-json-hooks.cc
@@ -141,7 +141,10 @@ class APT_HIDDEN JsonWriter
JsonWriter &value(const char *value)
{
maybeComma();
- encodeString(os, value);
+ if (value == nullptr)
+ os << "null";
+ else
+ encodeString(os, value);
return *this;
}
JsonWriter &value(int value)
diff --git a/test/libapt/json_test.cc b/test/libapt/json_test.cc
index 9c29936f6..ee8f3cebe 100644
--- a/test/libapt/json_test.cc
+++ b/test/libapt/json_test.cc
@@ -59,3 +59,11 @@ TEST(JsonTest, JsonStackRegression)
EXPECT_EQ("{\"a\":[{}],\"b\":[{}]}", os.str());
}
+TEST(JsonTest, JsonNull)
+{
+ std::ostringstream os;
+
+ JsonWriter(os).value(nullptr);
+
+ EXPECT_EQ("null", os.str());
+}