Skip to content

Commit

Permalink
[jsontlv] TlvToJson returns null for empty array instead of an empty …
Browse files Browse the repository at this point in the history
…array (#23885)
  • Loading branch information
vivien-apple authored and pull[bot] committed Dec 9, 2022
1 parent df8a815 commit 970d357
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
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 @@ -116,8 +116,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 @@ -186,46 +184,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

0 comments on commit 970d357

Please sign in to comment.