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

Use FastWriter for TlvJson #18456

Merged
merged 1 commit into from
May 17, 2022
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
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)
CodeChronos928 marked this conversation as resolved.
Show resolved Hide resolved
{
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);
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

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