diff --git a/scripts/tools/check_includes_config.py b/scripts/tools/check_includes_config.py index ae06b487cbaba8..bfd7f15c5ccf17 100644 --- a/scripts/tools/check_includes_config.py +++ b/scripts/tools/check_includes_config.py @@ -162,5 +162,6 @@ # Not intended for embedded clients 'src/lib/support/jsontlv/JsonToTlv.cpp': {'sstream'}, 'src/lib/support/jsontlv/JsonToTlv.h': {'string'}, - 'src/lib/support/jsontlv/TlvToJson.h': {'string'} + 'src/lib/support/jsontlv/TlvToJson.h': {'string'}, + 'src/lib/support/jsontlv/TextFormat.h': {'string'} } diff --git a/src/lib/support/jsontlv/BUILD.gn b/src/lib/support/jsontlv/BUILD.gn index 377b9be19d0656..4af096aa5e6174 100644 --- a/src/lib/support/jsontlv/BUILD.gn +++ b/src/lib/support/jsontlv/BUILD.gn @@ -22,12 +22,14 @@ static_library("jsontlv") { sources = [ "ElementTypes.h", "JsonToTlv.cpp", + "TextFormat.cpp", "TlvJson.cpp", "TlvToJson.cpp", ] public = [ "JsonToTlv.h", + "TextFormat.h", "TlvJson.h", "TlvToJson.h", ] diff --git a/src/lib/support/jsontlv/TextFormat.cpp b/src/lib/support/jsontlv/TextFormat.cpp new file mode 100644 index 00000000000000..c607df791be7cf --- /dev/null +++ b/src/lib/support/jsontlv/TextFormat.cpp @@ -0,0 +1,32 @@ +/* + * + * 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 + +namespace chip { +std::string PrettyPrintJsonString(const std::string & jsonString) +{ + Json::Reader reader; + Json::Value jsonObject; + reader.parse(jsonString, jsonObject); + Json::StyledWriter writer; + return writer.write(jsonObject); +} + +} // namespace chip diff --git a/src/lib/support/jsontlv/TextFormat.h b/src/lib/support/jsontlv/TextFormat.h new file mode 100644 index 00000000000000..7214a257d985bf --- /dev/null +++ b/src/lib/support/jsontlv/TextFormat.h @@ -0,0 +1,30 @@ +/* + * + * 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 + +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); + +} // 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..87f37ab4e052b1 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 82edf02d3f6c3b..b14d84ceed4e03 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 36231be5020c60..63dcf48c9fde16 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