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
  • Loading branch information
vivien-apple committed Dec 2, 2022
1 parent 5529d74 commit 00343e6
Showing 1 changed file with 30 additions and 32 deletions.
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

0 comments on commit 00343e6

Please sign in to comment.