Skip to content

Commit 1133708

Browse files
emargolisrobszewczykbzbarsky-applerestyled-commitsandreilitvin
authored andcommitted
Json to Tlv and Tlv to Json Converters Implementation in CPP (#27635)
* Json to Tlv and Tlv to Json Converters Implemented in CPP This implementation is equivalent to the Kotlin implementation in: src/controller/java/src/chip/jsontlv/ Note that NOT all TLV configurations are supported by the current implementation. Here is the list of limitations: - TLV Structure elements are expected to be sorted in a canonical tag order - TLV Lists are not supported - Multi-Dimensional TLV Arrays are not supported - All elements in an array MUST be of the same type - The top-level TLV element MUST be a single structure with AnonymousTag - The following tags are supported: - AnonymousTag are only used with TLV Array elements or a top-level structure. - ContextSpecificTag are used only with TLV Structure elements. - CommonProfileTag are used only with TLV Structure elements. - Infinity Float/Double values are not supported. Added README.md file that describing the format. Added unit tests for TLV to JSON, JSON to TLV, JSON to TLV back to JSON conversion cases. NOTE about the current implementation of the Tlv-to-Json converter in: src/lib/support/jsontlv/TlvJson.cpp I kept this implementation because it is currently used in a few places in the code for testing/logging purposes. As a follow up work item, this implementation should be replaced with the new one presented in this commit. * Update src/lib/support/jsontlv/JsonToTlv.cpp Co-authored-by: Robert Szewczyk <szewczyk@google.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Robert Szewczyk <szewczyk@google.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/JsonToTlv.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/JsonToTlv.h Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Addressed Review Comments * Restyled by clang-format * Restyled by prettier-markdown * Removed Debug Prints * Added Support for Float/Double Infinity Values Those values should be encoded as "Infinity" and "-Infinity" strings. * Restyled by clang-format * Restyled by prettier-markdown * Update src/lib/support/jsontlv/JsonToTlv.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Addressed Review Comments * Switch from CommonTag to ProfileTag with an implicit profile id * Documentation update * Restyle * Fix type of variable in unit test * Update src/lib/support/jsontlv/README.md Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Added more comments about kTemporaryImplicitProfileId not being actually used in stored values. Made the values consistent everywhere * Revert old code updates * Casing on json updated according to code review * Make the tlv element naming a bit more consistent, including invalid JSON in case element types are NOT as expected * Remove obsolete comment * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update src/lib/support/jsontlv/TlvToJson.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> --------- Co-authored-by: Robert Szewczyk <szewczyk@google.com> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Andrei Litvin <andy314@gmail.com> Co-authored-by: yunhanw-google <yunhanw@google.com>
1 parent 7c4334d commit 1133708

15 files changed

+3769
-122
lines changed

.github/.wordlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ blocklist
154154
blockquote
155155
bluetoothd
156156
bluez
157+
BOOL
157158
BooleanState
158159
bootable
159160
Bootloader

scripts/tools/check_includes_config.py

+5
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,9 @@
158158
# Library meant for non-embedded
159159
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
160160
'src/tracing/json/json_tracing.h': {'fstream'},
161+
162+
# Not intended for embedded clients
163+
'src/lib/support/jsontlv/JsonToTlv.cpp': {'sstream'},
164+
'src/lib/support/jsontlv/JsonToTlv.h': {'string'},
165+
'src/lib/support/jsontlv/TlvToJson.h': {'string'}
161166
}

src/lib/core/TLVReader.h

+5
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,11 @@ class DLL_EXPORT TLVReader
855855
*/
856856
TLVBackingStore * GetBackingStore() { return mBackingStore; }
857857

858+
/**
859+
* Returns true if the current TLV element type is a double.
860+
*/
861+
bool IsElementDouble() { return ElementType() == TLVElementType::FloatingPointNumber64; }
862+
858863
/**
859864
* Gets the point in the underlying input buffer that corresponds to the reader's current position.
860865
*

src/lib/support/jsontlv/BUILD.gn

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2020 Project CHIP Authors
1+
# Copyright (c) 2020-2023 Project CHIP Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -19,7 +19,18 @@ config("jsontlv_config") {
1919
}
2020

2121
static_library("jsontlv") {
22-
sources = [ "TlvJson.cpp" ]
22+
sources = [
23+
"ElementTypes.h",
24+
"JsonToTlv.cpp",
25+
"TlvJson.cpp",
26+
"TlvToJson.cpp",
27+
]
28+
29+
public = [
30+
"JsonToTlv.h",
31+
"TlvJson.h",
32+
"TlvToJson.h",
33+
]
2334

2435
public_configs = [ ":jsontlv_config" ]
2536

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <lib/core/TLV.h>
19+
20+
namespace {
21+
22+
const char kElementTypeInt[] = "INT";
23+
const char kElementTypeUInt[] = "UINT";
24+
const char kElementTypeBool[] = "BOOL";
25+
const char kElementTypeFloat[] = "FLOAT";
26+
const char kElementTypeDouble[] = "DOUBLE";
27+
const char kElementTypeBytes[] = "BYTES";
28+
const char kElementTypeString[] = "STRING";
29+
const char kElementTypeNull[] = "NULL";
30+
const char kElementTypeStruct[] = "STRUCT";
31+
const char kElementTypeArray[] = "ARRAY";
32+
const char kElementTypeEmpty[] = "?";
33+
34+
const char kFloatingPointPositiveInfinity[] = "Infinity";
35+
const char kFloatingPointNegativeInfinity[] = "-Infinity";
36+
37+
struct ElementTypeContext
38+
{
39+
chip::TLV::TLVType tlvType = chip::TLV::kTLVType_NotSpecified;
40+
bool isDouble = false;
41+
};
42+
43+
} // namespace

0 commit comments

Comments
 (0)