Skip to content

Commit

Permalink
Relocate json-related method to JsonUtilities file
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Sep 5, 2023
1 parent 54038c0 commit 080ee5b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static_library("jsontlv") {

public = [
"JsonToTlv.h",
"JsonUtilities.h",
"TlvJson.h",
"TlvToJson.h",
]
Expand Down
50 changes: 50 additions & 0 deletions src/lib/support/jsontlv/JsonUtilities.h
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <json/json.h>
#include <lib/core/TLV.h>

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
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/JsonUtilities.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/JsonUtilities.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/JsonUtilities.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <nlunit-test.h>
#include <system/SystemPacketBuffer.h>
Expand Down

0 comments on commit 080ee5b

Please sign in to comment.