From 080ee5b99838761f1f6f985afc61153fd3086ef3 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Mon, 4 Sep 2023 18:33:35 -0700 Subject: [PATCH] Relocate json-related method to JsonUtilities file --- src/lib/support/jsontlv/BUILD.gn | 1 + src/lib/support/jsontlv/JsonUtilities.h | 50 +++++++++++++++++++ src/lib/support/jsontlv/TlvToJson.cpp | 17 ------- src/lib/support/jsontlv/TlvToJson.h | 15 ------ src/lib/support/tests/TestJsonToTlv.cpp | 1 + src/lib/support/tests/TestJsonToTlvToJson.cpp | 1 + src/lib/support/tests/TestTlvToJson.cpp | 1 + 7 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 src/lib/support/jsontlv/JsonUtilities.h diff --git a/src/lib/support/jsontlv/BUILD.gn b/src/lib/support/jsontlv/BUILD.gn index 377b9be19d0656..186a6d0a0a7522 100644 --- a/src/lib/support/jsontlv/BUILD.gn +++ b/src/lib/support/jsontlv/BUILD.gn @@ -28,6 +28,7 @@ static_library("jsontlv") { public = [ "JsonToTlv.h", + "JsonUtilities.h", "TlvJson.h", "TlvToJson.h", ] diff --git a/src/lib/support/jsontlv/JsonUtilities.h b/src/lib/support/jsontlv/JsonUtilities.h new file mode 100644 index 00000000000000..5e3544b8693ced --- /dev/null +++ b/src/lib/support/jsontlv/JsonUtilities.h @@ -0,0 +1,50 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2013-2017 Nest Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +namespace chip { +/* + * Pretty-prints the input Json string using standard library pretty-printer. + * This pretty-printer generates a Json string in a human friendly format with 3 space indentation + * and nice representation of arrays and objects. + * The input can be any string, as long as it's valid Json. + */ +std::string PrettyPrintJsonString(const std::string & jsonString) +{ + Json::Reader reader; + Json::Value jsonObject; + reader.parse(jsonString, jsonObject); + Json::StyledWriter writer; + return writer.write(jsonObject); +} + +/* + * Reformats the input Json string as a single-line string with no spaces or newlines. + * The input can be any string, as long as it's valid Json. + */ +std::string MakeJsonSingleLine(const std::string & jsonString) +{ + std::string str = PrettyPrintJsonString(jsonString); + str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end()); + return str; +} + +} // namespace chip diff --git a/src/lib/support/jsontlv/TlvToJson.cpp b/src/lib/support/jsontlv/TlvToJson.cpp index d9884f60af43fa..9bafeccf431b2c 100644 --- a/src/lib/support/jsontlv/TlvToJson.cpp +++ b/src/lib/support/jsontlv/TlvToJson.cpp @@ -362,21 +362,4 @@ CHIP_ERROR TlvToJson(TLV::TLVReader & reader, std::string & jsonString) jsonString = writer.write(jsonObject); return CHIP_NO_ERROR; } - -std::string PrettyPrintJsonString(const std::string & jsonString) -{ - Json::Reader reader; - Json::Value jsonObject; - reader.parse(jsonString, jsonObject); - Json::StyledWriter writer; - return writer.write(jsonObject); -} - -std::string MakeJsonSingleLine(const std::string & jsonString) -{ - std::string str = PrettyPrintJsonString(jsonString); - str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end()); - return str; -} - } // namespace chip diff --git a/src/lib/support/jsontlv/TlvToJson.h b/src/lib/support/jsontlv/TlvToJson.h index ea7589271e8d2e..d73b1859b5dad8 100644 --- a/src/lib/support/jsontlv/TlvToJson.h +++ b/src/lib/support/jsontlv/TlvToJson.h @@ -33,19 +33,4 @@ CHIP_ERROR TlvToJson(TLV::TLVReader & reader, std::string & jsonString); * Given a TLV encoded byte array, this function converts it into JSON object. */ CHIP_ERROR TlvToJson(const ByteSpan & tlv, std::string & jsonString); - -/* - * Pretty-prints the input Json string using standard library pretty-printer. - * This pretty-printer generates a Json string in a human friendly format with 3 space indentation - * and nice representation of arrays and objects. - * The input can be any string, as long as it's valid Json. - */ -std::string PrettyPrintJsonString(const std::string & jsonString); - -/* - * Reformats the input Json string as a single-line string with no spaces or newlines. - * The input can be any string, as long as it's valid Json. - */ -std::string MakeJsonSingleLine(const std::string & jsonString); - } // namespace chip diff --git a/src/lib/support/tests/TestJsonToTlv.cpp b/src/lib/support/tests/TestJsonToTlv.cpp index c08ba978360173..999d6ac506f27a 100644 --- a/src/lib/support/tests/TestJsonToTlv.cpp +++ b/src/lib/support/tests/TestJsonToTlv.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/src/lib/support/tests/TestJsonToTlvToJson.cpp b/src/lib/support/tests/TestJsonToTlvToJson.cpp index d494521b62537e..2dfc7d031a28ee 100644 --- a/src/lib/support/tests/TestJsonToTlvToJson.cpp +++ b/src/lib/support/tests/TestJsonToTlvToJson.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include diff --git a/src/lib/support/tests/TestTlvToJson.cpp b/src/lib/support/tests/TestTlvToJson.cpp index 5e4bb77cb9ef13..cf98ecdc0d3346 100644 --- a/src/lib/support/tests/TestTlvToJson.cpp +++ b/src/lib/support/tests/TestTlvToJson.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include