Skip to content

Commit 1751160

Browse files
CodeChronos928pull[bot]
authored andcommitted
Use FastWriter for TlvJson (#18456)
TlvJson now outputs a more RPC-friendly string, instead of a human-readable string.
1 parent e87a748 commit 1751160

File tree

2 files changed

+13
-37
lines changed

2 files changed

+13
-37
lines changed

src/lib/support/jsontlv/TlvJson.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ void InsertKeyValue(Json::Value & json, const KeyContext & keyContext, T val)
106106

107107
std::string JsonToString(Json::Value & json)
108108
{
109-
Json::StyledWriter writer;
109+
Json::FastWriter writer;
110+
writer.omitEndingLineFeed();
110111
return writer.write(json);
111112
}
112113

src/lib/support/tests/TestTlvToJson.cpp

+11-36
Original file line numberDiff line numberDiff line change
@@ -54,55 +54,30 @@ CHIP_ERROR SetupReader()
5454

5555
bool Matches(const char * referenceString, Json::Value & generatedValue)
5656
{
57-
Json::StyledWriter writer;
5857
auto generatedStr = JsonToString(generatedValue);
5958

60-
auto matches = (generatedStr == std::string(referenceString));
59+
// Normalize the reference string to the expected compact value.
60+
Json::Reader reader;
61+
Json::Value referenceValue;
62+
reader.parse(referenceString, referenceValue);
63+
64+
Json::FastWriter writer;
65+
writer.omitEndingLineFeed();
66+
auto compactReferenceString = writer.write(referenceValue);
67+
68+
auto matches = (generatedStr == compactReferenceString);
6169

6270
if (!matches)
6371
{
6472
printf("Didn't match!\n");
6573
printf("Reference:\n");
66-
printf("%s\n", referenceString);
74+
printf("%s\n", compactReferenceString.c_str());
6775

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

7280
return matches;
73-
74-
#if 0
75-
//
76-
// Converting the reference string to a JSON representation and comparing
77-
// that against the generated JSON object would have been preferable. This avoids
78-
// the need to have reference strings expressed precisely to match the generated string
79-
// from the JSON converter, right down to the number of spaces,etc. This would have made
80-
// the reference string less britle and coupled to the jsoncpp converter implementation.
81-
//
82-
// However, jsoncpp converter converts positive values in the JSON to a signed
83-
// integer C type. This results in a mis-match with the generated JSON objects
84-
// that are created from spec-compliant TLV that correctly represents them as unsigned
85-
// integers in the JSON object.
86-
//
87-
// This mismatch nullifies this approach unfortunately.
88-
//
89-
// TODO: Investigate a way to compare using JSON objects.
90-
//
91-
Json::Reader reader;
92-
Json::Value referenceValue;
93-
94-
bool ret = reader.parse(referenceString, referenceValue);
95-
if (ret != true) {
96-
return ret;
97-
}
98-
99-
std::cout << generatedValue << "\n";
100-
std::cout << referenceValue << "\n";
101-
102-
int rett = generatedValue.compare(referenceValue);
103-
printf("%d\n", rett);
104-
return (rett == 0);
105-
#endif
10681
}
10782

10883
template <typename T>

0 commit comments

Comments
 (0)