Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsontlv] TlvToJson returns null for empty array instead of an empty … #23885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import("//build_overrides/chip.gni")
import("//build_overrides/jsoncpp.gni")

config("jsontlv_config") {
cflags = [ "-Wno-implicit-fallthrough" ]
}

static_library("jsontlv") {
Expand Down
62 changes: 30 additions & 32 deletions src/lib/support/jsontlv/TlvJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ std::string JsonToString(Json::Value & json)

CHIP_ERROR TlvToJson(TLV::TLVReader & reader, KeyContext context, Json::Value & parent)
{
bool isStruct = false;

switch (reader.GetType())
{
case TLV::kTLVType_UnsignedInteger: {
Expand Down Expand Up @@ -178,46 +176,46 @@ CHIP_ERROR TlvToJson(TLV::TLVReader & reader, KeyContext context, Json::Value &
break;
}

case TLV::kTLVType_Structure:
isStruct = true;
case TLV::kTLVType_Structure: {
TLV::TLVType containerType;
ReturnErrorOnFailure(reader.EnterContainer(containerType));

CHIP_ERROR err;
Json::Value value;

while ((err = reader.Next()) == CHIP_NO_ERROR)
{
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
KeyContext context2(static_cast<chip::FieldId>(TLV::TagNumFromTag(reader.GetTag())));

//
// Fall-through to the case below since
// arrays and structs are handled similarly with
// just a small difference in terms of handling of field IDs vs.
// list indices of the elements in the respective collections.
//
//
// Recursively convert to JSON the encompassing item within the struct.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}

VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(containerType));
InsertKeyValue(parent, context, value);
break;
}

case TLV::kTLVType_Array: {
TLV::TLVType containerType;

ReturnErrorOnFailure(reader.EnterContainer(containerType));

CHIP_ERROR err;
Json::Value value;
size_t listIndex = 0;
Json::Value value = Json::Value(Json::arrayValue);
size_t listIndex = 0;

while ((err = reader.Next()) == CHIP_NO_ERROR)
{
if (isStruct)
{
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
KeyContext context2(static_cast<chip::FieldId>(TLV::TagNumFromTag(reader.GetTag())));

//
// Recursively convert to JSON the encompassing item within the struct.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}
else
{
KeyContext context2(static_cast<chip::ListIndex>(listIndex++));

//
// Recursively convert to JSON the encompassing item within the array.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}
KeyContext context2(static_cast<chip::ListIndex>(listIndex++));

//
// Recursively convert to JSON the encompassing item within the array.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}

VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
Expand Down
12 changes: 12 additions & 0 deletions src/lib/support/tests/TestTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
" \"value\" : [ 1, 2, 3, 4 ]\n"
"}\n");

int8uList = {};
EncodeAndValidate(int8uList,
"{\n"
" \"value\" : []\n"
"}\n");

DataModel::Nullable<DataModel::List<uint8_t>> nullValueList;
EncodeAndValidate(nullValueList,
"{\n"
" \"value\" : null\n"
"}\n");

Clusters::UnitTesting::Structs::SimpleStruct::Type structListData[2] = { structVal, structVal };
DataModel::List<Clusters::UnitTesting::Structs::SimpleStruct::Type> structList;

Expand Down