From 17511600e1f56633b16e7454ca50c3a8cc1315f8 Mon Sep 17 00:00:00 2001 From: CodeChronos <1197535+CodeChronos928@users.noreply.github.com> Date: Tue, 17 May 2022 07:41:55 -0700 Subject: [PATCH] Use FastWriter for TlvJson (#18456) TlvJson now outputs a more RPC-friendly string, instead of a human-readable string. --- src/lib/support/jsontlv/TlvJson.cpp | 3 +- src/lib/support/tests/TestTlvToJson.cpp | 47 ++++++------------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/src/lib/support/jsontlv/TlvJson.cpp b/src/lib/support/jsontlv/TlvJson.cpp index 0cc84c33d7d80b..2639b743733908 100644 --- a/src/lib/support/jsontlv/TlvJson.cpp +++ b/src/lib/support/jsontlv/TlvJson.cpp @@ -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); } diff --git a/src/lib/support/tests/TestTlvToJson.cpp b/src/lib/support/tests/TestTlvToJson.cpp index 6636e417c73d81..21e4113c12fa69 100644 --- a/src/lib/support/tests/TestTlvToJson.cpp +++ b/src/lib/support/tests/TestTlvToJson.cpp @@ -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