Skip to content

Commit

Permalink
Json to Tlv and Tlv to Json Converters Implementation in CPP (#27635)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
7 people authored and pull[bot] committed Sep 26, 2023
1 parent 13fc389 commit 85f39a5
Show file tree
Hide file tree
Showing 15 changed files with 3,769 additions and 122 deletions.
1 change: 1 addition & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ blocklist
blockquote
bluetoothd
bluez
BOOL
BooleanState
bootable
Bootloader
Expand Down
5 changes: 5 additions & 0 deletions scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,9 @@
# Library meant for non-embedded
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
'src/tracing/json/json_tracing.h': {'fstream'},

# 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'}
}
5 changes: 5 additions & 0 deletions src/lib/core/TLVReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,11 @@ class DLL_EXPORT TLVReader
*/
TLVBackingStore * GetBackingStore() { return mBackingStore; }

/**
* Returns true if the current TLV element type is a double.
*/
bool IsElementDouble() { return ElementType() == TLVElementType::FloatingPointNumber64; }

/**
* Gets the point in the underlying input buffer that corresponds to the reader's current position.
*
Expand Down
15 changes: 13 additions & 2 deletions src/lib/support/jsontlv/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 Project CHIP Authors
# Copyright (c) 2020-2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,18 @@ config("jsontlv_config") {
}

static_library("jsontlv") {
sources = [ "TlvJson.cpp" ]
sources = [
"ElementTypes.h",
"JsonToTlv.cpp",
"TlvJson.cpp",
"TlvToJson.cpp",
]

public = [
"JsonToTlv.h",
"TlvJson.h",
"TlvToJson.h",
]

public_configs = [ ":jsontlv_config" ]

Expand Down
43 changes: 43 additions & 0 deletions src/lib/support/jsontlv/ElementTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* 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 <lib/core/TLV.h>

namespace {

const char kElementTypeInt[] = "INT";
const char kElementTypeUInt[] = "UINT";
const char kElementTypeBool[] = "BOOL";
const char kElementTypeFloat[] = "FLOAT";
const char kElementTypeDouble[] = "DOUBLE";
const char kElementTypeBytes[] = "BYTES";
const char kElementTypeString[] = "STRING";
const char kElementTypeNull[] = "NULL";
const char kElementTypeStruct[] = "STRUCT";
const char kElementTypeArray[] = "ARRAY";
const char kElementTypeEmpty[] = "?";

const char kFloatingPointPositiveInfinity[] = "Infinity";
const char kFloatingPointNegativeInfinity[] = "-Infinity";

struct ElementTypeContext
{
chip::TLV::TLVType tlvType = chip::TLV::kTLVType_NotSpecified;
bool isDouble = false;
};

} // namespace
Loading

0 comments on commit 85f39a5

Please sign in to comment.