Skip to content

Commit

Permalink
Use FastWriter for TlvJson (#18456)
Browse files Browse the repository at this point in the history
TlvJson now outputs a more RPC-friendly string, instead of a
human-readable string.
  • Loading branch information
CodeChronos928 authored May 17, 2022
1 parent 3127d23 commit 84a3783
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/lib/support/jsontlv/TlvJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ void InsertKeyValue(Json::Value & json, const KeyContext & keyContext, T val)

std::string JsonToString(Json::Value & json)
{
Json::StyledWriter writer;
Json::FastWriter writer;
writer.omitEndingLineFeed();
return writer.write(json);
}

Expand Down
47 changes: 11 additions & 36 deletions src/lib/support/tests/TestTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,30 @@ CHIP_ERROR SetupReader()

bool Matches(const char * referenceString, Json::Value & generatedValue)
{
Json::StyledWriter writer;
auto generatedStr = JsonToString(generatedValue);

auto matches = (generatedStr == std::string(referenceString));
// Normalize the reference string to the expected compact value.
Json::Reader reader;
Json::Value referenceValue;
reader.parse(referenceString, referenceValue);

Json::FastWriter writer;
writer.omitEndingLineFeed();
auto compactReferenceString = writer.write(referenceValue);

auto matches = (generatedStr == compactReferenceString);

if (!matches)
{
printf("Didn't match!\n");
printf("Reference:\n");
printf("%s\n", referenceString);
printf("%s\n", compactReferenceString.c_str());

printf("Generated:\n");
printf("%s\n", generatedStr.c_str());
}

return matches;

#if 0
//
// Converting the reference string to a JSON representation and comparing
// that against the generated JSON object would have been preferable. This avoids
// the need to have reference strings expressed precisely to match the generated string
// from the JSON converter, right down to the number of spaces,etc. This would have made
// the reference string less britle and coupled to the jsoncpp converter implementation.
//
// However, jsoncpp converter converts positive values in the JSON to a signed
// integer C type. This results in a mis-match with the generated JSON objects
// that are created from spec-compliant TLV that correctly represents them as unsigned
// integers in the JSON object.
//
// This mismatch nullifies this approach unfortunately.
//
// TODO: Investigate a way to compare using JSON objects.
//
Json::Reader reader;
Json::Value referenceValue;

bool ret = reader.parse(referenceString, referenceValue);
if (ret != true) {
return ret;
}

std::cout << generatedValue << "\n";
std::cout << referenceValue << "\n";

int rett = generatedValue.compare(referenceValue);
printf("%d\n", rett);
return (rett == 0);
#endif
}

template <typename T>
Expand Down

0 comments on commit 84a3783

Please sign in to comment.