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

Relocate json-related method to TextFormat file #29060

Merged
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 scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
2 changes: 2 additions & 0 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
32 changes: 32 additions & 0 deletions src/lib/support/jsontlv/TextFormat.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <json/json.h>

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
30 changes: 30 additions & 0 deletions src/lib/support/jsontlv/TextFormat.h
Original file line number Diff line number Diff line change
@@ -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 <string>

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
17 changes: 0 additions & 17 deletions src/lib/support/jsontlv/TlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 0 additions & 15 deletions src/lib/support/jsontlv/TlvToJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/lib/support/tests/TestJsonToTlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <lib/core/TLVReader.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/jsontlv/JsonToTlv.h>
#include <lib/support/jsontlv/TextFormat.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <nlunit-test.h>

Expand Down
1 change: 1 addition & 0 deletions src/lib/support/tests/TestJsonToTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <app/data-model/Encode.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/jsontlv/JsonToTlv.h>
#include <lib/support/jsontlv/TextFormat.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <nlunit-test.h>

Expand Down
1 change: 1 addition & 0 deletions src/lib/support/tests/TestTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <app/data-model/Decode.h>
#include <app/data-model/Encode.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/jsontlv/TextFormat.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <nlunit-test.h>
#include <system/SystemPacketBuffer.h>
Expand Down
Loading