Skip to content

Commit

Permalink
Added explicit null for json parser, and also added tests to verify n…
Browse files Browse the repository at this point in the history
…ull behavior. (#28)
  • Loading branch information
JonathanHenson authored Jun 12, 2019
1 parent c1e9cf5 commit fa59298
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/aws/crt/JsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ namespace Aws
*/
JsonObject &AsArray(Vector<JsonObject> &&array);

/**
* Sets the current JSON node as null.
*/
JsonObject &AsNull();

/**
* Adds a JSON object to the top level of this node at key.
* The object parameter is deep-copied.
Expand Down
6 changes: 6 additions & 0 deletions source/JsonObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ namespace Aws
return *this;
}

JsonObject &JsonObject::AsNull()
{
m_value = cJSON_CreateNull();
return *this;
}

JsonObject &JsonObject::WithObject(const char *key, const JsonObject &value)
{
if (m_value == nullptr)
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ add_test_case(ClientBootstrapResourceSafety)
add_test_case(TLSContextResourceSafety)
add_test_case(DateTimeBinding)
add_test_case(BasicJsonParsing)
add_test_case(JsonNullParsing)
add_test_case(JsonNullNestedObject)
add_test_case(JsonExplicitNull)
add_test_case(SHA256ResourceSafety)
add_test_case(MD5ResourceSafety)
add_test_case(SHA256HMACResourceSafety)
Expand Down
72 changes: 72 additions & 0 deletions tests/JsonParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,75 @@ static int s_BasicJsonParsing(struct aws_allocator *allocator, void *ctx)
}

AWS_TEST_CASE(BasicJsonParsing, s_BasicJsonParsing)

static int s_JsonNullParseTest(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
Aws::Crt::ApiHandle apiHandle(allocator);

const Aws::Crt::String jsonValue = "{\"testStringKey\":null,\"testIntKey\":10,"
"\"array\":[null,\"stringArrayEntry\"],"
"\"object\":{\"testObjectStringKey\":null}}";

Aws::Crt::JsonObject value(jsonValue);
ASSERT_TRUE(value.WasParseSuccessful());

auto str = value.View().WriteCompact(true);
ASSERT_STR_EQUALS(jsonValue.c_str(), str.c_str());
str = value.View().WriteCompact(false);
ASSERT_STR_EQUALS(jsonValue.c_str(), str.c_str());

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(JsonNullParsing, s_JsonNullParseTest)

static int s_JsonNullNestedObjectTest(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
Aws::Crt::ApiHandle apiHandle(allocator);

const Aws::Crt::String jsonValue = "{\"testStringKey\":null,\"testIntKey\":10,"
"\"array\":[null,\"stringArrayEntry\"],"
"\"object\":{\"testObjectStringKey\":null}}";

Aws::Crt::JsonObject value(jsonValue);
ASSERT_TRUE(value.WasParseSuccessful());

Aws::Crt::JsonObject doc;
doc.WithObject("null_members", jsonValue);

const Aws::Crt::String expectedValue = "{\"null_members\":{\"testStringKey\":null,\"testIntKey\":10,"
"\"array\":[null,\"stringArrayEntry\"],"
"\"object\":{\"testObjectStringKey\":null}}}";
auto str = doc.View().WriteCompact(true);
ASSERT_STR_EQUALS(expectedValue.c_str(), str.c_str());
str = doc.View().WriteCompact(false);
ASSERT_STR_EQUALS(expectedValue.c_str(), str.c_str());

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(JsonNullNestedObject, s_JsonNullNestedObjectTest)

static int s_JsonExplicitNullTest(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
Aws::Crt::ApiHandle apiHandle(allocator);

const Aws::Crt::String expectedValue = "{\"testKey\":null}";

Aws::Crt::JsonObject doc;
Aws::Crt::JsonObject nullObject;
nullObject.AsNull();
doc.WithObject("testKey", nullObject);

auto str = doc.View().WriteCompact(true);
ASSERT_STR_EQUALS(expectedValue.c_str(), str.c_str());
str = doc.View().WriteCompact(false);
ASSERT_STR_EQUALS(expectedValue.c_str(), str.c_str());

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(JsonExplicitNull, s_JsonExplicitNullTest)

0 comments on commit fa59298

Please sign in to comment.