From 98a707330508a3db19bbca6265f315270c127c39 Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 22 May 2026 16:27:56 +0200 Subject: [PATCH 01/21] Adapt bazel infrastructure to enable choosing json library --- score/json/internal/parser/nlohmann/BUILD | 1 + .../internal/parser/nlohmann/nlohmann_parser.cpp | 15 +++++++++++++++ score/json/internal/parser/vajson/BUILD | 1 + .../json/internal/parser/vajson/vajson_parser.cpp | 15 +++++++++++++++ score/json/json_parser.cpp | 14 -------------- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/score/json/internal/parser/nlohmann/BUILD b/score/json/internal/parser/nlohmann/BUILD index 65d58f57f8..3b07154e69 100644 --- a/score/json/internal/parser/nlohmann/BUILD +++ b/score/json/internal/parser/nlohmann/BUILD @@ -36,6 +36,7 @@ cc_library( deps = [ ":json_builder", "@nlohmann_json//:json", + "@score_baselibs//score/json:parser_interface", "@score_baselibs//score/json/internal/model", "@score_baselibs//score/result", ], diff --git a/score/json/internal/parser/nlohmann/nlohmann_parser.cpp b/score/json/internal/parser/nlohmann/nlohmann_parser.cpp index 1219e2f3ce..0508a526cf 100644 --- a/score/json/internal/parser/nlohmann/nlohmann_parser.cpp +++ b/score/json/internal/parser/nlohmann/nlohmann_parser.cpp @@ -28,6 +28,8 @@ #include #include +#include "score/json/json_parser.h" + auto score::json::NlohmannParser::FromFile(const std::string_view file_path) -> score::Result { score::Result result = MakeUnexpected(Error::kParsingError); @@ -72,3 +74,16 @@ auto score::json::NlohmannParser::FromBuffer(const std::string_view buffer) -> s return json_builder.GetData(); } + +// Suppress "AUTOSAR C++14 A15-5-3" rule findings: "The std::terminate() function shall not be called implicitly". +// Calling std::terminate() if any exceptions are thrown is expected as per safety requirements +// coverity[autosar_cpp14_a15_5_3_violation] +auto score::json::JsonParser::FromFile(const std::string_view file_path) const noexcept -> score::Result +{ + return NlohmannParser::FromFile(file_path); +} + +auto score::json::JsonParser::FromBuffer(const std::string_view buffer) const noexcept -> score::Result +{ + return NlohmannParser::FromBuffer(buffer); +} diff --git a/score/json/internal/parser/vajson/BUILD b/score/json/internal/parser/vajson/BUILD index 24e80286ce..764abb976a 100644 --- a/score/json/internal/parser/vajson/BUILD +++ b/score/json/internal/parser/vajson/BUILD @@ -27,6 +27,7 @@ cc_library( }), visibility = ["@score_baselibs//score/json:__subpackages__"], deps = [ + "@score_baselibs//score/json:parser_interface", "@score_baselibs//score/json/internal/model", "@score_baselibs//score/json/internal/parser/vajson/vajson_impl", "@score_baselibs//score/result", diff --git a/score/json/internal/parser/vajson/vajson_parser.cpp b/score/json/internal/parser/vajson/vajson_parser.cpp index 0a6d7f9eeb..5d29132538 100644 --- a/score/json/internal/parser/vajson/vajson_parser.cpp +++ b/score/json/internal/parser/vajson/vajson_parser.cpp @@ -12,6 +12,8 @@ ********************************************************************************/ #include "score/json/internal/parser/vajson/vajson_parser.h" +#include "score/json/json_parser.h" + auto score::json::VajsonParser::FromFile(const std::string_view file_path) -> score::Result { score::Result result = MakeUnexpected(Error::kParsingError); @@ -185,3 +187,16 @@ auto score::json::VajsonParser::OnUnexpectedEvent() noexcept -> score::json::vaj return score::json::vajson::MakeErrorResult( score::json::vajson::JsonErrc::kUserValidationFailed); } + +// Suppress "AUTOSAR C++14 A15-5-3" rule findings: "The std::terminate() function shall not be called implicitly". +// Calling std::terminate() if any exceptions are thrown is expected as per safety requirements +// coverity[autosar_cpp14_a15_5_3_violation] +auto score::json::JsonParser::FromFile(const std::string_view file_path) const noexcept -> score::Result +{ + return score::json::VajsonParser::FromFile(file_path); +} + +auto score::json::JsonParser::FromBuffer(const std::string_view buffer) const noexcept -> score::Result +{ + return score::json::VajsonParser::FromBuffer(buffer); +} diff --git a/score/json/json_parser.cpp b/score/json/json_parser.cpp index d75094b6e8..da7d6afc79 100644 --- a/score/json/json_parser.cpp +++ b/score/json/json_parser.cpp @@ -13,26 +13,12 @@ #include "score/json/json_parser.h" -#include "score/json/internal/parser/nlohmann/nlohmann_parser.h" - #include namespace score { namespace json { -// Suppress "AUTOSAR C++14 A15-5-3" rule findings: "The std::terminate() function shall not be called implicitly". -// Calling std::terminate() if any exceptions are thrown is expected as per safety requirements -// coverity[autosar_cpp14_a15_5_3_violation] -auto JsonParser::FromFile(const std::string_view file_path) const noexcept -> score::Result -{ - return NlohmannParser::FromFile(file_path); -} - -auto JsonParser::FromBuffer(const std::string_view buffer) const noexcept -> score::Result -{ - return NlohmannParser::FromBuffer(buffer); -} // False positive, user defined literal operator is used to perform conversion. // coverity[autosar_cpp14_a13_1_3_violation : FALSE] From 8891022ed46d91b03604808dba9aa44fa3d7c9f3 Mon Sep 17 00:00:00 2001 From: visjui Date: Tue, 26 May 2026 09:30:19 +0200 Subject: [PATCH 02/21] Add serializer files --- .../internal/parser/vajson/vajson_impl/BUILD | 20 + .../vajson/vajson_impl/writer/serializers.h | 33 + .../vajson_impl/writer/serializers/stl.h | 30 + .../serializers/stl/associative_containers.h | 303 ++++++++++ .../writer/serializers/stl/primitives.h | 200 ++++++ .../serializers/stl/sequence_containers.h | 138 +++++ .../structures/generic_value_serializer.h | 463 ++++++++++++++ .../generic_value_serializer_impl.h | 66 ++ .../serializers/structures/key_serializer.h | 225 +++++++ .../serializers/structures/serializer.h | 151 +++++ .../serializers/util/escaped_json_string.h | 192 ++++++ .../serializers/util/length_serializer.h | 70 +++ .../vajson_impl/writer/serializers/vac.h | 30 + .../writer/serializers/vac/primitives.h | 131 ++++ .../serializers/vac/sequence_containers.h | 136 +++++ .../writer/serializers/vac/variant.h | 124 ++++ .../vajson_impl/writer/types/array_type.h | 179 ++++++ .../vajson_impl/writer/types/basic_types.h | 568 ++++++++++++++++++ .../vajson_impl/writer/types/bin_types.h | 374 ++++++++++++ .../vajson_impl/writer/types/object_type.h | 217 +++++++ .../writer/types/object_type_impl.h | 60 ++ 21 files changed, 3710 insertions(+) create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h create mode 100644 score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h diff --git a/score/json/internal/parser/vajson/vajson_impl/BUILD b/score/json/internal/parser/vajson/vajson_impl/BUILD index c518029488..7aa56fc245 100644 --- a/score/json/internal/parser/vajson/vajson_impl/BUILD +++ b/score/json/internal/parser/vajson/vajson_impl/BUILD @@ -54,6 +54,26 @@ cc_library( "util/json_error_domain.h", "util/number.h", "util/types.h", + "writer/serializers.h", + "writer/serializers/stl.h", + "writer/serializers/stl/associative_containers.h", + "writer/serializers/stl/primitives.h", + "writer/serializers/stl/sequence_containers.h", + "writer/serializers/vac.h", + "writer/serializers/vac/primitives.h", + "writer/serializers/vac/sequence_containers.h", + "writer/serializers/vac/variant.h", + "writer/serializers/structures/generic_value_serializer.h", + "writer/serializers/structures/generic_value_serializer_impl.h", + "writer/serializers/structures/key_serializer.h", + "writer/serializers/structures/serializer.h", + "writer/serializers/util/escaped_json_string.h", + "writer/serializers/util/length_serializer.h", + "writer/types/array_type.h", + "writer/types/basic_types.h", + "writer/types/bin_types.h", + "writer/types/object_type.h", + "writer/types/object_type_impl.h", ], includes = [ "include", diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h new file mode 100644 index 0000000000..ffa325b203 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h @@ -0,0 +1,33 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of all available serializers. + * + * \details Provides serializers for native JSON, libVac & STL/STD types. + * \unit Serializer + * \complexity The sum metrics of unit is high. It includes multiple template classes, which could be put into + * separate units. But the classes are closely related, and splitting the unit up into smaller pieces + * would not make it simpler. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include "amsr/json/writer/serializers/stl.h" +#include "amsr/json/writer/serializers/vac.h" + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h new file mode 100644 index 0000000000..4ed6b5a98a --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h @@ -0,0 +1,30 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for native STD & STL types. + * + * \details Single include for all STD &STL types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include "amsr/json/writer/serializers/stl/associative_containers.h" +#include "amsr/json/writer/serializers/stl/primitives.h" +#include "amsr/json/writer/serializers/stl/sequence_containers.h" + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h new file mode 100644 index 0000000000..8899379ff7 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h @@ -0,0 +1,303 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for associative STL containers. + * + * \details Provides serializers for (unordered) set, (unordered) map, and (unordered) multimap types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include +#include +#include "amsr/json/writer/types/array_type.h" +#include "amsr/json/writer/types/object_type.h" + +namespace amsr { +namespace json { + +/*! + * \brief Serializes a set of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Value + * Type of value. + * \tparam Cmp + * Type of comparison function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] set + * Set to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::set const& set) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JArray(set); +} + +/*! + * \brief Serializes an unordered set of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Value + * Type of value. + * \tparam Hash + * Type of hash function. + * \tparam Pred + * Type of predicate function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] set + * Unordered set to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, + std::unordered_set const& set) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JArray(set); +} + +/*! + * \brief Serializes a map of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Key + * Type of key. Must be convertible to a JKey. + * \tparam Value + * Type of value. + * \tparam Cmp + * Type of comparison function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] map + * Map to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::map const& map) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JObject(map); +} + +/*! + * \brief Serializes a map of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Key + * Type of key. Must be convertible to a JKey. + * \tparam Value + * Type of value. + * \tparam Cmp + * Type of comparison function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] map + * Map to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::map const& map) noexcept + -> typename GenericValueSerializer::Next { + return std::move(serializer) << JObject(map); +} + +/*! + * \brief Serializes an unordered map of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Key + * Type of key. Must be convertible to a JKey. + * \tparam Value + * Type of value. + * \tparam Hash + * Type of hash function. + * \tparam Pred + * Type of predicate function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] map + * Unordered map to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, + std::unordered_map const& map) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JObject(map); +} + +/*! + * \brief Serializes a multimap of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Key + * Type of key. Must be convertible to a JKey. + * \tparam Value + * Type of value. + * \tparam Cmp + * Type of comparison function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] map + * Multimap to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::multimap const& map) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JObject(map); +} + +/*! + * \brief Serializes an unordered multimap of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Key + * Type of key. Must be convertible to a JKey. + * \tparam Value + * Type of value. + * \tparam Hash + * Type of hash function. + * \tparam Pred + * Type of predicate function. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] map + * Unordered multimap to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, + std::unordered_multimap const& map) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JObject(map); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h new file mode 100644 index 0000000000..7d3e63ca22 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h @@ -0,0 +1,200 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for primitive data types. + * + * \details Provides serializers for pointer, bool, number, and string types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include + +#include "amsr/json/util/types.h" +#include "amsr/json/writer/types/basic_types.h" + +namespace amsr { +namespace json { +/*! + * \brief Forward declaration for the GenericValueSerializer + * \vpublic + * \tparam Return + * type after using operator<<(). + */ +template +class GenericValueSerializer; + +/*! + * \brief Serializes a null value directly from a nullptr literal + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::nullptr_t) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JNull(); +} + +/*! + * \brief Serializes a value directly from a pointer + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] ptr + * The pointer whose value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * + * \spec + * requires true; + * \endspec + * + * \internal + * - If the given pointer is a nullptr: + * - Serialize it as a null type. + * - Otherwise: + * - Serialize the pointer's value. + * \endinternal + */ +template +auto operator<<(GenericValueSerializer&& serializer, T const* ptr) noexcept -> + typename GenericValueSerializer::Next { + // VCA_VAJSON_EXTERNAL_CALL + return (ptr == nullptr) ? (std::move(serializer) << JNull()) : (std::move(serializer) << *ptr); +} + +/*! + * \brief Serializes a bool value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] b + * Bool value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, bool b) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JBool(b); +} + +/*! + * \brief Serializes a number value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam N + * Type of number. + * \param[in] serializer + * instance to write into. + * \param[in] number + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template ::value>> +auto operator<<(GenericValueSerializer&& serializer, N number) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JNumber(number); +} + +/*! + * \brief Serializes a string value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] string + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::string const& string) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JString(string); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h new file mode 100644 index 0000000000..e18a0c1871 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h @@ -0,0 +1,138 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for STD sequence containers. + * + * \details Provides serializers for std::array, std::vector, and std::deque types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include +#include +#include "amsr/json/writer/types/array_type.h" + +namespace amsr { +namespace json { + +/*! + * \brief Serializes an array of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Value + * Type of value. + * \tparam N + * Size of the array. + * \param[in] serializer + * instance to write into. + * \param[in] array + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::array const& array) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JArray(array); +} + +/*! + * \brief Serializes a vector of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Value + * Type of value. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] vector + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::vector const& vector) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JArray(vector); +} + +/*! + * \brief Serializes a deque of serializable elements + * \vpublic + * + * \tparam Next + * type of serializer. + * \tparam Value + * Type of value. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] deque + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::deque const& deque) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JArray(deque); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h new file mode 100644 index 0000000000..62445e9013 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h @@ -0,0 +1,463 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Serializer for generic JSON value types. + * + * \details Provides serializers for Null, Bool, Number, String, Array, and Object types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include + +#include "score/language/futurecpp/charconv.hpp" +#include "amsr/json/util/json_error_domain.h" +#include "amsr/json/util/types.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/serializers/util/escaped_json_string.h" +#include "amsr/json/writer/serializers/util/length_serializer.h" +#include "amsr/json/writer/types/array_type.h" +#include "amsr/json/writer/types/basic_types.h" +#include "amsr/json/writer/types/bin_types.h" +#include "amsr/json/writer/types/object_type.h" + +namespace amsr { +namespace json { +/*! + * \brief A serializer for JSON value types + * \vpublic + * + * \tparam Return + * Type of the return value of a << operation. Must be one of the following types: + * - Unit: Serializer has no follow-up state (outermost element). + * - Self or GenericValueSerializer: Next element is another value (e.g. inside arrays). + * - KeySerializer: Next element is a key. + * + * \trace DSGN-JSON-Writer-Serialization + */ +template +class GenericValueSerializer final { + public: + /*! + * \brief Type of the return value + * + * \details Set the type of the return value to be either its own type GenericValueSerializer (for arrays or a + * specified type) or the type specified by Return. + * \vpublic + */ + using Next = typename std::conditional_t::value, GenericValueSerializer, Return>; + + /*! + * \brief Constructs a GenericValueSerializer from an output stream + * \details Do not create an instance of GenericValueSerializer directly, use the vpublic aliases in + * amsr/json/writer/serializer.h + * \vpublic + * + * \param[in] os + * Output stream to write into. + * \param[in] state + * of the Serializer. + * \param[in] bom + * The BOM type to write. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + explicit GenericValueSerializer(WriterType os, SerializerState state = SerializerState::kEmpty, + EncodingType bom = EncodingType::kNone) noexcept + : os_(os), serializer_state_{state} { + this->WriteBom(bom); + } + + /*! + * \brief Default move constructor + * + * \vpublic + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + GenericValueSerializer(GenericValueSerializer&&) noexcept = default; + + /*! + * \brief Default move assignment + * \vpublic + * + * \return A reference to the moved into object. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + auto operator=(GenericValueSerializer&&) & noexcept -> GenericValueSerializer& = default; + + // Deleted copy constructor/assignment operator. + GenericValueSerializer(GenericValueSerializer const&) = delete; + auto operator=(GenericValueSerializer const&) -> GenericValueSerializer& = delete; + + /*! + * \brief Default DTOR + * \vpublic + */ + ~GenericValueSerializer() noexcept = default; + + /*! + * \brief Serializes a null value + * \vpublic + * + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JNullType) && noexcept -> Next { + return this->Serialize([this]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + constexpr auto null_str = "null"sv; + this->os_.get().write(null_str.data(), null_str.size()); + }); + } + + /*! + * \brief Serializes a boolean value + * \vpublic + * + * \param[in] b + * Boolean value to serialize. + * \return The succeeding serializer. + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JBoolType b) && noexcept -> Next { + score::safecpp::zstring_view const value{b.value ? "true" : "false"}; + // NOLINTNEXTLINE(whitespace/line_length) + // VECTOR NL AutosarC++17_10-M8.5.1: MD_JSON_AutosarC++17_10-M8.5.1_use_of_possibly_unintialized_value_false_positive + return this->Serialize([this, value]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().write(value.data(), value.size()); + }); + } + + /*! + * \brief Serializes a number value + * \vpublic + * + * \tparam T + * Type of number. + * \param[in] number + * value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + template + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JNumberType number) && noexcept -> Next { + return this->Serialize([this, number]() noexcept { + // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety + std::array buffer{}; + T value = static_cast(number.GetValue()); + + // Convert number to string using std::to_chars + score::cpp::to_chars_result conversion_result{score::cpp::to_chars(buffer.data(), buffer.data() + buffer.size(), value)}; + + AssertCondition(ec == std::errc{}, + "GenericValueSerializer: Could not convert number to textual representation."); + + // Create a span from the buffer to the end of written data + score::cpp::span result_span{buffer.data(), static_cast(ptr - buffer.data())}; + + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().write(result_span.data(), result_span.size()); + }); + } + + /*! + * \brief Serializes a string value + * \vpublic + * + * \param[in] string + * value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * + * \internal + * - Add quotes to the begin and end of the string. + * - Serialize the escaped string as a JSON string value. + * \endinternal + */ + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JStringType string) && noexcept -> Next { + return this->Serialize([this, string]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('"'); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get() << internal::EscapedJsonString(string); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('"'); + }); + } + + /*! + * \brief Serializes a binary string value + * \vpublic + * + * \param[in] string + * value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * + * \internal + * - Add a 's' to denote the following value as a string. + * - Serialize the length of the string value as four bytes big endian. + * - Write the string value. + * \endinternal + */ + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JBinStringType string) && noexcept -> Next { + return this->Serialize([this, string]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().put('s'); + // VCA_VAJSON_OUTPUTSTREAM + internal::SerializeLength(this->os_.get(), string.GetLength()); + // VCA_VAJSON_OUTPUTSTREAM + auto const& value = string.GetValue(); + this->os_.get().write(value.data(), value.size()); + }); + } + + /*! + * \brief Serializes a series of serializable values + * \vpublic + * + * \tparam Fn + * Type of serializer function. + * \param[in] tuple + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre The function contained in the argument does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * \internal + * - Add an opening square bracket. + * - Serialize every value as a JSON value. + * - Add a closing square bracket. + * \endinternal + */ + template + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JArrayType tuple) && noexcept -> Next { + return this->Serialize([this, tuple]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('['); + static_cast(tuple.fn(ArrayStart(this->os_.get()))); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put(']'); + }); + } + + /*! + * \brief Serializes a binary value + * \vpublic + * + * \param[in] bin + * Value to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * + * \internal + * - Add a 'b' to denote the following value as binary. + * - Serialize the length of the binary value as four bytes big endian. + * - Write the binary value. + * \endinternal + */ + // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const + auto operator<<(JBinType bin) && noexcept -> Next { + return this->Serialize([this, bin]() noexcept { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().put('b'); + // VCA_VAJSON_OUTPUTSTREAM + internal::SerializeLength(this->os_.get(), bin.GetLength()); + // VCA_VAJSON_OUTPUTSTREAM + auto const& value = bin.GetValue(); + this->os_.get().write(value.data(), value.size()); + }); + } + + /*! + * \brief Serializes an object + * \vpublic + * + * \tparam Fn + * Type of serializer function. + * \param[in] object + * to serialize. + * \return The succeeding serializer. + * \context ANY + * \pre The function contained in the argument does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + template + auto operator<<(JObjectType object) && noexcept -> Next; + + private: + /*! + * \brief Serializes a value + * \tparam Fn + * Type of function. + * \param[in] fn + * Serializer call function. + * \return The succeeding serializer. + * + * \context ANY + * \pre The passed function does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * + * \internal + * - If another element was serialized before: + * - Add a comma. + * - Execute the given serializer function. + * \endinternal + */ + template + auto Serialize(Fn&& fn) const noexcept -> Next { + if (this->serializer_state_ == SerializerState::kNonEmpty) { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put(','); + } + std::forward(fn)(); + return Next(this->os_.get(), SerializerState::kNonEmpty); + } + + /*! + * \brief Writes the requested BOM type + * \param[in] type + * The BOM type to write. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + * + * \internal + * - Write the requested BOM to the stream. + * \endinternal + */ + void WriteBom(EncodingType type) const noexcept { + if (type == EncodingType::kUtf8) { + constexpr std::string_view kUtf8Bom{"\xEF\xBB\xBF"sv}; + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().write(kUtf8Bom.data(), kUtf8Bom.size()); + } + } + + /*! + * \brief Output stream to write into + */ + WriterType os_; + + /*! + * \brief Serializer state + */ + SerializerState serializer_state_; +}; + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h new file mode 100644 index 0000000000..57bdbb8296 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h @@ -0,0 +1,66 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Implementation of methods for generic value serializer type. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include "amsr/json/writer/serializers/structures/generic_value_serializer.h" + +namespace amsr { +namespace json { +/*! + * \brief Serializes another object into the output stream + * \vprivate component private + * + * \spec + * requires true; + * \endspec + * \internal + * - Assert that the current state allows adding an object. + * - Add an opening curly bracket. + * - Serialize the object as a JSON object. + * - Add a closing curly bracket. + * \endinternal + */ +template +template +// VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const +auto GenericValueSerializer::operator<<(JObjectType object) && noexcept + -> GenericValueSerializer::Next { + return this->Serialize([this, &object]() noexcept { + /*! + * \brief Defines the return value type + */ + using ReturnType = decltype(object.fn(ObjectStart(this->os_.get()))); + static_assert(std::is_same::value, "Cannot close object in current state"); + + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('{'); + // VCA_VAJSON_OUTPUTSTREAM + static_cast(object.fn(ObjectStart(this->os_.get()))); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('}'); + }); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h new file mode 100644 index 0000000000..a49367d017 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h @@ -0,0 +1,225 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Serializer for JSON keys. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include "amsr/json/util/types.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/serializers/util/escaped_json_string.h" +#include "amsr/json/writer/serializers/util/length_serializer.h" +#include "amsr/json/writer/types/array_type.h" +#include "amsr/json/writer/types/basic_types.h" +#include "amsr/json/writer/types/object_type.h" + +namespace amsr { +namespace json { +/*! + * \brief A serializer for JSON keys + * + * \details This class only allows adding a key into the object and always returns a value serializer to only + * allow a value for the next concatenation operation. + * \vpublic + */ +class KeySerializer final { + public: + /*! + * \brief Serializer state after adding a key + * \vpublic + */ + using Next = ObjectSerializerValue; + + /*! + * \brief Constructs a KeySerializer from an output stream + * \details Do not create an instance of KeySerializer directly, use the vpublic aliases in + * amsr/json/writer/serializer.h + * \vpublic + * + * \param[in] os + * Output stream to write into. + * \param[in] state + * of the Serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + explicit KeySerializer(WriterType os, SerializerState state = SerializerState::kEmpty) noexcept + : os_(os), serializer_state_{state} {} + + /*! + * \brief Default move constructor + * + * \vpublic + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + KeySerializer(KeySerializer&&) noexcept = default; + + /*! + * \brief Default move assignment + * \vpublic + * + * \return A reference to the moved into object. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ + auto operator=(KeySerializer&&) & noexcept -> KeySerializer& = default; + + // Deleted copy constructor copy assignment operator. + KeySerializer(KeySerializer const&) = delete; + auto operator=(KeySerializer const&) -> KeySerializer& = delete; + + /*! + * \brief Default DTOR + * \vpublic + */ + ~KeySerializer() noexcept = default; + + /*! + * \brief Serializes a key + * \vpublic + * + * \param[in] key + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * + * \internal + * - Add a comma, if necessary. + * - Serialize the key. + * \endinternal + */ + auto operator<<(JKeyType key) const&& noexcept -> Next { + this->WriteComma(); + + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().put('"'); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get() << internal::EscapedJsonString(key); + // VCA_VAJSON_OUTPUTSTREAM + constexpr auto colon_str = R"(":)"sv; + this->os_.get().write(colon_str.data(), colon_str.size()); + + return Next(this->os_.get()); + } + + /*! + * \brief Serializes a binary key + * \vpublic + * + * \param[in] key + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + * + * \internal + * - Add a comma, if necessary. + * - Serialize the length of the key as four bytes big endian. + * - Write the key. + * \endinternal + */ + auto operator<<(JBinKeyType key) const&& noexcept -> Next { + this->WriteComma(); + + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put('k'); + // VCA_VAJSON_OUTPUTSTREAM + internal::SerializeLength(this->os_.get(), key.GetLength()); + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Write(key.GetValue()); + + return Next(this->os_.get()); + } + + private: + /*! + * \brief Adds a comma to the stream, if necessary + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + * + * \internal + * - If another element was serialized before, add a comma. + * \endinternal + */ + void WriteComma() const noexcept { + if (this->serializer_state_ == SerializerState::kNonEmpty) { + // VCA_VAJSON_OUTPUTSTREAM + this->os_.get().Put(','); + } + } + + /*! + * \brief Output stream to write into + */ + WriterType os_; + + /*! + * \brief Serializer state + */ + SerializerState serializer_state_; +}; + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h new file mode 100644 index 0000000000..5bcbc1b0a5 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h @@ -0,0 +1,151 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Contains common types and forward declarations for JSON serializers. + * + *********************************************************************************************************************/ +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include "amsr/json/util/types.h" + +namespace amsr { +namespace json { +/*! + * \brief State of the object to be serialized + * + * \details Indicates if the object is empty or not. Commas should only appended if the object is not empty. + * \vpublic + */ +enum class SerializerState : bool { kEmpty, kNonEmpty }; + +/*! + * \brief An empty type that signifies that the serializer has no follow-up state + * + * \vprivate component private + * + * \trace DSGN-JSON-Writer-Error-Prevention + */ +class Unit { + public: + /*! + * \brief Constructs a Unit type from an output stream + * + * \details This satisfies the 'Next' state interface for serializers. + * \vprivate Vector component internal API + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + explicit Unit(std::reference_wrapper, + SerializerState = SerializerState::kEmpty) noexcept {} +}; + +/*! + * \brief A marker struct that only tells the GenericValueSerializer to return itself after using operator<<() + * + * \vprivate component private + * + * \trace DSGN-JSON-Writer-Error-Prevention + */ +class Self {}; + +/*! + * \brief Forward declaration for the GenericValueSerializer + * + * \vprivate component private + */ +template +class GenericValueSerializer; + +/*! + * \brief A serializer type for single values + * + * \vprivate component private + */ +using ValueSerializer = GenericValueSerializer; + +/*! + * \brief A serializer type for JSON documents + * + * \details Intentionally a using to make it obvious that a JSON document must start with a single value. + * \vpublic + */ +using DocumentSerializer = ValueSerializer; + +/********************************************************************************************************************** + * Object Declarations * + *********************************************************************************************************************/ + +/*! + * \brief Forward declaration for the KeySerializer + */ +class KeySerializer; + +/*! + * \brief A serializer type for the start of JSON objects + * + * \details Typedef for the initial Object serializer state where only a key is allowed. + * \vpublic + */ +using ObjectStart = KeySerializer; + +/*! + * \brief A serializer type for JSON objects + * + * \details This class only allows adding a value into the object and the next concatenation will only allow a + * key. + * \vprivate component private + */ +using ObjectSerializerValue = GenericValueSerializer; + +/********************************************************************************************************************** + * Tuple Declarations * + *********************************************************************************************************************/ + +/*! + * \brief A serializer type for JSON arrays + * + * \details Serializes multiple, potentially inhomogeneous values. + * \vprivate component private + */ +using ArraySerializer = GenericValueSerializer<>; + +/*! + * \brief A serializer type for the start of JSON arrays + * + * \details Typedef for the initial Array serializer state. + * \vpublic + */ +using ArrayStart = ArraySerializer; + +/*! + * \brief Type of the output writer + * + * \vpublic + */ +using WriterType = std::reference_wrapper; +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h new file mode 100644 index 0000000000..3311e529ff --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h @@ -0,0 +1,192 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Serializer for JSON string literals. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include + +#include "amsr/json/util/types.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/types/basic_types.h" + +namespace amsr { +namespace json { +namespace internal { +/*! + * \brief An escaped JSON string type + * \vprivate component private + */ +class EscapedJsonString { + public: + /*! + * \brief Constructs an EscapedJsonString from a JSON key + * \vprivate Vector component internal API + * + * \param[in] key + * to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + explicit EscapedJsonString(JKeyType key) noexcept : EscapedJsonString(key.GetValue()) {} + + /*! + * \brief Constructs an EscapedJsonString from a JSON string + * \vprivate Vector component internal API + * + * \param[in] string + * to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + explicit EscapedJsonString(JStringType string) noexcept : EscapedJsonString(string.GetValue()) {} + + /*! + * \brief Returns the contained string + * \vprivate Vector component internal API + * + * \return The contained string. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::string_view { return this->value_; } + + private: + /*! + * \brief Constructs an EscapedJsonString from a StringView + * \param[in] value + * The value to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + */ + explicit EscapedJsonString(std::string_view value) noexcept : value_{value} {} + + /*! + * \brief Value to write as a JSON string literal + */ + std::string_view value_; +}; + +// NOLINTNEXTLINE(whitespace/line_length) +// VECTOR NCL AutosarC++17_10-M5.0.16, Metric-HIS.VG: MD_JSON_AutosarC++17_10-M5.0.16_pointer_arithmetic, MD_JSON_Metric-HIS.VG_json_value +/*! + * \brief Serializes an escaped string literal type + * \vprivate component private + * + * \param[in] os + * Output stream to write into. + * \param[in] string + * to escape and serialize. + * \return A reference to the output stream. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + * + * \internal + * - If the string contains a character that needs to be escaped in JSON: + * - Serialize the escaped character. + * - Otherwise: + * - Serialize the character directly. + * \endinternal + */ +auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> std::ostream& { + for (char const ch : string.GetValue()) { + switch (std::char_traits::to_int_type(ch)) { + case std::char_traits::to_int_type('"'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\")", 1); + break; + } + case std::char_traits::to_int_type('\\'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\\)", 1); + break; + } + case std::char_traits::to_int_type('\b'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\b)", 1); + break; + } + case std::char_traits::to_int_type('\f'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\f)", 1); + break; + } + case std::char_traits::to_int_type('\n'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\n)", 1); + break; + } + case std::char_traits::to_int_type('\r'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\r)", 1); + break; + } + case std::char_traits::to_int_type('\t'): { + // VCA_VAJSON_OUTPUTSTREAM + os.write(R"(\t)", 1); + break; + } + default: + // VCA_VAJSON_OUTPUTSTREAM + os.put(ch); + break; + } + } + + return os; +} + +} // namespace internal +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h new file mode 100644 index 0000000000..6204b79c33 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h @@ -0,0 +1,70 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Serializer for length tags. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include "amsr/json/writer/serializers/structures/serializer.h" + +namespace amsr { +namespace json { +namespace internal { +/*! + * \brief Serializes a length value as big endian + * \vprivate component private + * + * \param[in] os + * Stream to write to. + * \param[in] length + * to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + * + * \internal + * - Copy each byte of the value to byte buffer, big-endian byte order, + * - Write byte buffer to the stream. + * \endinternal + */ +inline void SerializeLength(WriterType os, std::uint32_t const length) noexcept { + constexpr std::size_t kPrefixSize{sizeof(std::uint32_t)}; + + std::array arr{}; + arr[0] = static_cast((length >> 24) & 0xffu); + arr[1] = static_cast((length >> 16) & 0xffu); + arr[2] = static_cast((length >> 8) & 0xffu); + arr[3] = static_cast(length & 0xffu); + + // VCA_VAJSON_OUTPUTSTREAM + os.get().write(reinterpret_cast(arr.data()), kPrefixSize); +} + +} // namespace internal +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h new file mode 100644 index 0000000000..70cfea97cc --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h @@ -0,0 +1,30 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for native libVac types. + * + * \details Single include for all libVac types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include "amsr/json/writer/serializers/vac/primitives.h" +#include "amsr/json/writer/serializers/vac/sequence_containers.h" +#include "amsr/json/writer/serializers/vac/variant.h" + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h new file mode 100644 index 0000000000..0a250fb198 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h @@ -0,0 +1,131 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for libVac primitive data types. + * + * \details Provides serializers for std::string, std::string_view, and std::uint8_t types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include + +#include "amsr/json/util/types.h" +#include "amsr/json/writer/types/basic_types.h" + +namespace amsr { +namespace json { +/*! + * \brief Forward declaration for the GenericValueSerializer + * + * \vprivate component private + */ +template +class GenericValueSerializer; + +/*! + * \brief Serializes a string value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] string + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, std::string const& string) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JString(string); +} + +/*! + * \brief Serializes a string value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] string + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, ::std::string_view string) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << JString(string); +} + +/*! + * \brief Serializes a std::uint8_t value directly + * \vpublic + * + * \tparam Next + * type of serializer. + * \param[in] serializer + * instance to write into. + * \param[in] byte + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(GenericValueSerializer&& serializer, ::std::uint8_t byte) noexcept -> + typename GenericValueSerializer::Next { + return std::move(serializer) << static_cast(byte); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h new file mode 100644 index 0000000000..fbde47bf49 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h @@ -0,0 +1,136 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for libVac sequence containers. + * + * \details Provides serializers for score::cpp::span, score::cpp::static_vector + * types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include + +#include "amsr/json/writer/types/array_type.h" + +#include "score/span.hpp" +#include "score/vector.hpp" + +#include "score/static_vector.h" + +namespace amsr { +namespace json { + +/*! + * \brief Serializes an array view of serializable elements + * \vpublic + * + * \tparam Serializer + * Type of serializer. + * \tparam Value + * Type of value. + * \param[in] serializer + * instance to write into. + * \param[in] view + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(Serializer&& serializer, ::score::cpp::span const& view) noexcept -> typename Serializer::Next { + return std::forward(serializer) << JArray(view); +} + +/*! + * \brief Serializes a vector of serializable elements + * \vpublic + * + * \tparam Serializer + * Type of serializer. + * \tparam Value + * Type of value. + * \tparam Alloc + * Vector allocator. + * \param[in,out] serializer + * instance to write into. + * \param[in] vector + * Data to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(Serializer&& serializer, ::score::cpp::pmr::vector const& vector) noexcept -> + typename Serializer::Next { + return std::forward(serializer) << JArray(vector); +} + +/*! + * \brief Serializes a static vector of serializable elements + * \vpublic + * + * \tparam Serializer + * Type of serializer. + * \tparam Value + * Type of value. + * \tparam Alloc + * Type of allocator. + * \param[in] serializer + * instance to write into. + * \param[in] vector + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + * \spec + * requires true; + * \endspec + */ +template +auto operator<<(Serializer&& serializer, ::score::cpp::static_vector const& vector) noexcept -> + typename Serializer::Next { + return std::forward(serializer) << JArray(vector); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h new file mode 100644 index 0000000000..7583aea9e0 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h @@ -0,0 +1,124 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Provides a serializer for the score::cpp::variant type. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include + +#include "score/variant.hpp" +namespace amsr { +namespace json { +/*! + * \brief A generic variant visitor + * \details Serializes the children according to their implemented serializers. + * \vprivate component private + * + * \tparam Serializer + * Type of serializer. Must be noexcept move constructible and noexcept move assignable. + * + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + */ +template +class VariantVisitor { + static_assert(std::is_nothrow_move_constructible::value, "Serializer must be noexcept movable"); + static_assert(std::is_nothrow_move_assignable::value, "Serializer must be noexcept movable"); + + public: + /*! + * \brief Constructs the visitor + * \details Visitor must only be used once. + * \vprivate Vector component internal API + * + * \param[in] serializer + * to use. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + explicit VariantVisitor(Serializer&& serializer) noexcept : serializer_(std::move(serializer)) {} + + /*! + * \brief Call operator + * \vprivate Vector component internal API + * + * \tparam T + * Type of the variant. + * \param[in] value + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + template + auto operator()(T const& value) noexcept -> typename Serializer::Next { + return std::move(this->serializer_) << value; + } + + private: + /*! + * \brief Serializer of the variant + */ + Serializer serializer_; +}; + +/*! + * \brief Serializes a variant of serializable elements + * \vpublic + * + * \tparam Serializer + * Type of serializer. + * \tparam Types + * of values in the variant. + * \param[in] s + * Serializer instance to write into. + * \param[in] variant + * to serialize. + * \return The succeeding serializer. + * + * \context ANY + * \pre All alternatives contained in the variant are noexcept movable + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \trace DSGN-JSON-Writer-Serializable-Data-Structures + */ +template +auto operator<<(Serializer s, score::cpp::variant& variant) noexcept -> typename Serializer::Next { + VariantVisitor ser{std::move(s)}; + // VCA_VAJSON_EXTERNAL_CALL + return score::cpp::visit(std::move(ser), variant); +} + +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h new file mode 100644 index 0000000000..b591f3d953 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h @@ -0,0 +1,179 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for range-based containers. + * + * \details Provides serializers for arrays and tuples. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include + +#include "amsr/json/writer/serializers/stl/primitives.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/serializers/vac/primitives.h" +#include "amsr/json/writer/types/basic_types.h" + +namespace amsr { +namespace json { +inline namespace types { +/*! + * \brief A serializer type for a JSON array from a homogeneous C++ range + * \vprivate Vector component internal API + * \tparam Range + * Type of range. + * \tparam Fn + * The function type for this serializer. + */ +template +class RangeSerializer final { + public: + /*! + * \brief Constructs a RangeSerializer + * \vprivate Vector component internal API + * \tparam Fn1 + * Type of function. + * \param[in] range + * to serialize. + * \param[in] fn + * Function used to serialize. Must not throw exceptions. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + template + RangeSerializer(Range const& range, Fn1&& fn) noexcept : container_{range}, function_{std::forward(fn)} {} + + /*! + * \brief Call operator + * \vprivate Vector component internal API + * + * \tparam AS + * Type of array serializer. + * \param[in] as + * Array serializer to write into. + * + * \context ANY + * \pre The function contained in the class does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \internal + * - Serialize every element of the array as a JSON value. + * \endinternal + */ + template + void operator()(AS as) const noexcept { + // VCA_VAJSON_THIS_DEREF + for (auto const& value : this->container_.get()) { + as = std::move(as) << this->function_(value); + } // VCA_VAJSON_EXTERNAL_CALL + } + + private: + /*! + * \brief Container instance to be serialized + */ + std::reference_wrapper container_; + + /*! + * \brief Function to serialize single items with + */ + Fn function_; +}; + +/*! + * \brief Serialize an ad-hoc defined Tuple as heterogeneous array + * \vprivate Vector component internal API + * \tparam Fn + * The function type that defines the serialization. + */ +template +struct JArrayType final { + /*! + * \brief Wrapped function value + */ + Fn fn; +}; + +/*! + * \brief Serializes an ad-hoc defined Tuple as a heterogeneous array + * \details The function can be used to define a tuple by adding values. + * \vpublic + * \tparam Fn + * Type of serializer function. Must take an ArrayStart&& and return the follow-up serializer. + * \param[in] fn + * Function used to serialize the tuple. + * \return A serializable Tuple type. + * + * \context ANY + * \pre The passed function does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +template ::value>> +auto JArray(Fn&& fn) noexcept -> JArrayType { // VECTOR SL AutosarC++17_10-A13.3.1: MD_JSON_rvalue_ref + return {std::forward(fn)}; +} + +/*! + * \brief Serializes a homogeneous C++ range as a JSON array + * \vpublic + * \tparam Range + * Type of range. + * \tparam Fn + * Type of value serializer function. Must take the range's value type and return a JSON type. + * \param[in] range + * instance to be serialized. + * \param[in] fn + * Function used to serialize single elements. + * \return A serializable JSON array. + * + * \context ANY + * \pre The passed range & function do not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +template > +auto JArray(Range const& range, Fn&& fn = IdSerializer{}) noexcept -> JArrayType> { + return {RangeSerializer{range, std::forward(fn)}}; +} + +// clang-format off +} // inline namespace types +// clang-format off +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h new file mode 100644 index 0000000000..bb3eda6405 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h @@ -0,0 +1,568 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for basic JSON types. + * + * \details Provides serializers for Null, Bool, Key, Number, and String types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include +#include + +#include "amsr/json/util/types.h" + +namespace amsr { +namespace json { +inline namespace types { +/*! + * \brief A Null type + * + * \vprivate Vector component internal API + */ +struct JNullType final {}; + +/*! + * \brief Serializes a Null value + * \vpublic + * \return The serializable null type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr inline auto JNull() noexcept -> JNullType { return JNullType{}; } + +/*! + * \brief A Bool type + * + * \vprivate Vector component internal API + */ +struct JBoolType final { + /*! + * \brief Wrapped bool value + */ + bool value; +}; + +/*! + * \brief Serializes a Bool value + * \vpublic + * \param[in] b + * Bool value to serialize. + * \return The serializable bool type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr inline auto JBool(bool b) noexcept -> JBoolType { return {b}; } + +/*! + * \brief A Key type + * + * \vprivate Vector component internal API + */ +class JKeyType final { + public: + /*! + * \brief Constructs a Key type + * \param[in] s + * Key to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + */ + explicit constexpr JKeyType(std::string_view s) noexcept : value_(s) {} + + /*! + * \brief Returns the contained value + * \return The value. + * + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::string_view { return this->value_; } + + private: + /*! + * \brief Wrapped string value + */ + std::string_view value_; +}; + +/*! + * \brief Serializes a Key value + * \vpublic + * \param[in] s + * Key to serialize. + * \return The serializable key type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s}; } + +/*! + * \brief Serializes a Key value + * \vpublic + * \param[in] s + * Key to serialize. + * \return The serializable key type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view{s}); } + +/*! + * \brief Serializes a Key value + * \vpublic + * \param[in] s + * Key to serialize. + * \return The serializable key type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } + +inline namespace literals { +/*! + * \brief Serializes a Key value + * \vpublic + * \param[in] s + * String literal to serialize. + * \param[in] size + * Size of the pointer. + * \return The serializable key type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \synchronous - + * \spec + * requires true; + * \endspec + */ +// VECTOR NCL AutosarC++17_10-A13.1.3: MD_JSON_A13-1-3_false_positive +constexpr auto operator""_key(char const* s, std::size_t size) noexcept -> JKeyType { + return JKey(score::safecpp::zstring_view{s, size}); +} + +// clang-format off +} // namespace literals +// clang-format on + +/*! + * \brief A Number type + * \vprivate Vector component internal API + * \tparam N + * Type of number. + */ +template ::value>::type> +class JNumberType final { + public: + /*! + * \brief Constructs a Number type + * \vprivate Vector component internal API + * + * \param[in] num + * Number to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JNumberType(N num) noexcept : value_(num) {} + + /*! + * \brief Returns the contained value + * \return The value. + * + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> N { return this->value_; } + + private: + /*! + * \brief Wrapped number value + */ + N value_; +}; + +/*! + * \brief A char Number type + * + * \vprivate Vector component internal API + */ +template <> +class JNumberType final { + public: + /*! + * \brief Constructs a Number type + * \vprivate Vector component internal API + * + * \param[in] num + * Number to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JNumberType(char num) noexcept : value_(std::char_traits::to_int_type(num)) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::int32_t { return this->value_; } + + private: + /*! + * \brief Wrapped number value + */ + std::int32_t value_; +}; + +/*! + * \brief A std::uint8_t Number type + * \vprivate Vector component internal API + */ +template <> +class JNumberType final { + public: + /*! + * \brief Constructs a Number type + * \vprivate Vector component internal API + * + * \param[in] num + * Number to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JNumberType(std::uint8_t num) noexcept : value_(static_cast(num)) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::uint32_t { return this->value_; } + + private: + /*! + * \brief Wrapped number value + */ + std::uint32_t value_; +}; + +/*! + * \brief A std::int8_t Number type + * + * \vprivate Vector component internal API + */ +template <> +class JNumberType final { + public: + /*! + * \brief Constructs a Number type + * \vprivate Vector component internal API + * + * \param[in] num + * Number to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JNumberType(std::int8_t num) noexcept : value_(static_cast(num)) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::int32_t { return this->value_; } + + private: + /*! + * \brief Wrapped number value + */ + std::int32_t value_; +}; + +/*! + * \brief Serializes a Number value + * \vpublic + * \tparam N + * Type of number. + * \param[in] n + * The number to serialize. + * \return The serializable number type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +template ::value>::type> +constexpr auto JNumber(N n) noexcept -> JNumberType { + return JNumberType{n}; +} + +/*! + * \brief A String type + * + * \vprivate Vector component internal API + */ +class JStringType final { + public: + /*! + * \brief Constructs a String type + * \vprivate Vector component internal API + * + * \param[in] s + * String to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JStringType(std::string_view s) noexcept : value_(s) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> std::string_view { return this->value_; } + + private: + /*! + * \brief Wrapped string value + */ + std::string_view value_; +}; + +/*! + * \brief Serializes a String value + * \vpublic + * + * \param[in] s + * String to serialize. + * \return The serializable string type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JString(std::string_view s) noexcept -> JStringType { return JStringType(s); } + +/*! + * \brief Serializes a String value + * \vpublic + * + * \param[in] s + * String to serialize. + * \return The serializable string type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JString(std::string const& s) noexcept -> JStringType { return JString(std::string_view{s}); } + +/*! + * \brief Serializes a String value + * \vpublic + * + * \param[in] s + * String to serialize. + * \return The serializable string type. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JString(std::string const& s) noexcept -> JStringType { return JString(std::string_view{s}); } + +/*! + * \brief A function object used to serialize predefined serializers + * \vprivate Vector component internal API + * \tparam Container + * Type of container to serialize. + */ +template +class IdSerializer { + public: + /*! + * \brief Value Type of container + * \vprivate Vector component internal API + */ + using value_type = typename Container::value_type; + + /*! + * \brief Returns the unchanged value + * \vprivate Vector component internal API + * + * \param[in] v + * Value to return. + * \return The unchanged value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + template + auto operator()(Value&& v) const noexcept -> Value { + return std::forward(v); + } +}; + +// clang-format off +} // namespace types +// // clang-format on +} // namespace json +} // namespace amsr +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h new file mode 100644 index 0000000000..9d15451f57 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h @@ -0,0 +1,374 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Implementation of methods for Bin type. + * + * \details Provides serializers for arrays and tuples. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include "amsr/json/util/types.h" +#include "amsr/msra_always_assert.h" + +namespace amsr { +namespace json { +namespace internal { +/*! + * \brief Safely convert a length + * \param[in] length + * to convert. + * \return The converted length. + * + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ +inline auto ConvertLength(std::size_t const length) noexcept -> std::uint32_t { + msra_always_assert(length <= std::numeric_limits::max()); + + // VECTOR NCL AutosarC++17_10-A4.7.1: MD_JSON_AutosarC++17_10-A4.7.1_truncating_cast + // VECTOR NL AutosarC++17_10-M0.3.1: MD_JSON_AutosarC++17_10-M0.3.1_OOB + return static_cast(length); +} + +} // namespace internal + +inline namespace types { +/*! + * \brief A binary type + * + * \vprivate Vector component internal API + */ +class JBinType final { + public: + /*! + * \brief Constructs a binary type + * \vprivate Vector component internal API + * \param[in] b + * Bytes to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + */ + constexpr explicit JBinType(score::cpp::span b) noexcept : value_(b) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> score::cpp::span { return this->value_; } + + /*! + * \brief Returns the length + * \vprivate Vector component internal API + * + * \return The length. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } + + private: + /*! + * \brief Wrapped binary value + */ + score::cpp::span value_; +}; + +/*! + * \brief Serializes a binary value + * \vpublic + * + * \param[in] b + * Bytes to serialize. + * \return The serializable binary value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JBin(score::cpp::span b) noexcept -> JBinType { return JBinType{b}; } + +/*! + * \brief A binary string type + * + * \vprivate Vector component internal API + */ +class JBinStringType final { + public: + /*! + * \brief Constructs a binary string + * \vprivate Vector component internal API + * + * \param[in] s + * String to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JBinStringType(StringView s) noexcept : value_(s) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> StringView { return this->value_; } + + /*! + * \brief Returns the length + * \vprivate Vector component internal API + * + * \return The length. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } + + private: + /*! + * \brief Wrapped string value + */ + StringView value_; +}; + +/*! + * \brief Serializes a string as binary + * \vpublic + * + * \param[in] s + * The string to serialize. + * \return The serializable string value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JBinString(StringView s) noexcept -> JBinStringType { return JBinStringType{s}; } + +/*! + * \brief Serializes a string as binary + * \vpublic + * + * \param[in] s + * The string to serialize. + * \return The serializable string value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JBinString(std::string const& s) noexcept -> JBinStringType { + return JBinString(std::string_view{s}); +} + +/*! + * \brief Serializes a string as binary + * \vpublic + * + * \param[in] s + * The string to serialize. + * \return The serializable string value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JBinString(std::string const& s) noexcept -> JBinStringType { return JBinString(std::string_view{s}); } + +/*! + * \brief A binary key type + * + * \vprivate Vector component internal API + */ +class JBinKeyType final { + public: + /*! + * \brief Constructs a binary key + * \vprivate Vector component internal API + * + * \param[in] s + * Key to serialize. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \spec + * requires true; + * \endspec + */ + constexpr explicit JBinKeyType(StringView s) noexcept : value_(s) {} + + /*! + * \brief Returns the contained value + * \vprivate Vector component internal API + * + * \return The value. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetValue() const noexcept -> StringView { return this->value_; } + + /*! + * \brief Returns the length + * \vprivate Vector component internal API + * + * \return The length. + * \context ANY + * \pre - + * \threadsafe TRUE, for different this pointer + * \spec + * requires true; + * \endspec + */ + auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } + + private: + /*! + * \brief Wrapped key value + */ + StringView value_; +}; + +/*! + * \brief Serializes a key as binary + * \vpublic + * + * \param[in] s + * The key to serialize. + * \return The serializable key value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +constexpr auto JBinKey(StringView s) noexcept -> JBinKeyType { return JBinKeyType{s}; } + +/*! + * \brief Serializes a key as binary + * \vpublic + * + * \param[in] s + * The key to serialize. + * \return The serializable key value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JBinKey(std::string const& s) noexcept -> JBinKeyType { return JBinKey(std::string_view{s}); } + +/*! + * \brief Serializes a key as binary + * \vpublic + * + * \param[in] s + * The key to serialize. + * \return The serializable key value. + * + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +inline auto JBinKey(std::string const& s) noexcept -> JBinKeyType { return JBinKey(std::string_view{s}); } +// clang-format off +} // inline namespace types +// clang-format off +} // namespace json +} // namespace amsr +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h new file mode 100644 index 0000000000..d4d1d11541 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h @@ -0,0 +1,217 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief A collection of serializers for objects. + * + * \details Provides serializers for homogeneous C++ pair-ranges and Object types. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include +#include + +#include "amsr/json/util/types.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/types/basic_types.h" + +namespace amsr { +namespace json { +inline namespace types { +/*! + * \brief A serializer type for predefined keys + * + * \vprivate Vector component internal API + */ +class DefaultKeySerializer { + public: + /*! + * \brief Call operator + * \vprivate Vector component internal API + * + * \tparam T + * Type of value. + * \param[in] value + * to serialize as a key. Must be convertible to a StringView type. + * \return The serializable key type. + * \context ANY + * \pre - + * \threadsafe FALSE + * \reentrant FALSE + * + * \spec + * requires true; + * \endspec + */ + template + auto operator()(T const& value) const noexcept -> JKeyType { + static_assert(std::is_constructible::value, "Keys must be convertible to a StringView"); + return JKeyType{std::string_view{value}}; + } +}; + +/*! + * \brief A serializer type for a JSON array from a homogeneous C++ pair-range + * \vprivate Vector component internal API + * \tparam Range + * Type of range to serialize. + * \tparam KeyFn + * Type of key function. + * \tparam ValueFn + * Type of value function. + */ +template +class PairRangeSerializer final { + public: + /*! + * \brief Constructs a PairRangeSerializer + * \vprivate Vector component internal API + * \tparam KeyFn1 + * Type of key function. + * \tparam ValueFn1 + * Type of value function. + * \param[in] range + * to serialize. + * \param[in] key_fn + * Function used to serialize single keys. + * \param[in] value_fn + * Function used to serialize single values. + * + * \context ANY + * \pre The passed functions & range do not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + */ + template + PairRangeSerializer(Range const& range, KeyFn1&& key_fn, ValueFn1&& value_fn) noexcept + : map_{range}, key_function_{std::forward(key_fn)}, value_function_{std::forward(value_fn)} {} + + /*! + * \brief Call operator + * \vprivate Vector component internal API + * \tparam KS + * Type of key serializer. + * \param[in] os + * Object serializer to write into. + * \return The serializer state after serializing the range. + * + * \context ANY + * \pre The functions contained in the class do not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + */ + template + auto operator()(KS os) const noexcept -> KS; + + private: + /*! + * \brief Range instance to be serialized + */ + std::reference_wrapper map_; + + /*! + * \brief Function used to serialize single keys + */ + KeyFn key_function_; + + /*! + * \brief Function used to serialize single values + */ + ValueFn value_function_; +}; + +/*! + * \brief An Object type + * \vprivate Vector component internal API + * \tparam Fn + * Type of serializer function. + */ +template +// VCA_VAJSON_MOLE_1298 +struct JObjectType final { + /*! + * \brief Function used to serialize the object + */ + Fn fn; +}; + +/*! + * \brief Serializes an object value + * \vpublic + * \tparam Fn + * Type of serializer function. Must take an ObjectStart&& and return the follow-up serializer. + * \param[in] fn + * Function used to serialize the object. + * \return The serializable object type. + * + * \context ANY + * \pre The passed function does not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +template ::value>> +auto JObject(Fn&& fn) noexcept -> JObjectType { // VECTOR SL AutosarC++17_10-A13.3.1: MD_JSON_rvalue_ref + return {std::forward(fn)}; +} + +/*! + * \brief Serializes a homogeneous C++ pair-range (e.g. a map) as a JSON object + * \vpublic + * \tparam Range + * Type of range. + * \tparam KeyFn + * Type of key serializer function. Must take the range's key type and return a JSON type. + * \tparam ValueFn + * Type of value serializer function. Must take the range's value type and return a JSON type. + * \param[in] range + * instance to be serialized. + * \param[in] key_fn + * Function used to serialize single keys. + * \param[in] value_fn + * Function used to serialize single values. + * \return The serializable JSON object. + * + * \context ANY + * \pre The passed range & functions do not throw any exceptions + * \threadsafe FALSE + * \reentrant FALSE + * \synchronous - + * \spec + * requires true; + * \endspec + */ +template > +auto JObject(Range const& range, KeyFn&& key_fn = DefaultKeySerializer{}, + ValueFn&& value_fn = IdSerializer{}) noexcept + -> JObjectType> { + return { + PairRangeSerializer{range, std::forward(key_fn), std::forward(value_fn)}}; +} + +// clang-format off +} // inline namespace types +// clang-format on +} // namespace json +} // namespace amsr + +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h new file mode 100644 index 0000000000..ef87c1c928 --- /dev/null +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h @@ -0,0 +1,60 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +/*! \file + * \brief Implementation of methods for object type. + * + *********************************************************************************************************************/ + +#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ +#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ + +/********************************************************************************************************************** + * INCLUDES + *********************************************************************************************************************/ +#include +#include "amsr/json/writer/serializers/structures/generic_value_serializer.h" +#include "amsr/json/writer/serializers/structures/serializer.h" +#include "amsr/json/writer/types/object_type.h" + +namespace amsr { +namespace json { + +inline namespace types { +/*! + * \brief Serialize every key-value pair as a JSON key followed by a JSON value + * \vprivate Vector component internal API + * + * \spec + * requires true; + * \endspec + */ +template +template +auto PairRangeSerializer::operator()(KS os) const noexcept -> KS { + // VCA_VAJSON_THIS_DEREF + // VECTOR NCL MisraC++2023-11.6.1: MD_JSON_MISRAC++2023-11.6.1_range_based_initialization + for (auto const& pair : this->map_.get()) { + // VCA_VAJSON_THIS_DEREF + ObjectSerializerValue osv{std::move(os) << this->key_function_(pair.first)}; + // VCA_VAJSON_THIS_DEREF + os = std::move(osv) << this->value_function_(pair.second); + } + return os; +} + +// clang-format off +} // namespace types +// clang-format on +} // namespace json +} // namespace amsr +#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ From 1a15f81e81d85a1137373b03a162a31f1f3a1e42 Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 27 May 2026 14:50:38 +0200 Subject: [PATCH 03/21] Move serializer --- .../internal/parser/vajson/vajson_impl/BUILD | 20 -- .../vajson_impl/util/json_error_domain.h | 10 + .../parser/vajson/vajson_impl/util/types.h | 19 ++ score/json/internal/writer/vajson/BUILD | 43 +++++ .../writer/vajson/vajson_serialize.cpp | 75 ++++++++ .../internal/writer/vajson/vajson_serialize.h | 173 ++++++++++++++++++ .../writer/vajson/vajson_serialize_test.cpp | 57 ++++++ .../vajson}/writer/serializers.h | 4 +- .../vajson}/writer/serializers/stl.h | 6 +- .../serializers/stl/associative_containers.h | 4 +- .../writer/serializers/stl/primitives.h | 4 +- .../serializers/stl/sequence_containers.h | 2 +- .../structures/generic_value_serializer.h | 49 +++-- .../generic_value_serializer_impl.h | 7 +- .../serializers/structures/key_serializer.h | 21 ++- .../serializers/structures/serializer.h | 3 +- .../serializers/util/escaped_json_string.h | 20 +- .../serializers/util/length_serializer.h | 3 +- .../vajson}/writer/serializers/vac.h | 6 +- .../writer/serializers/vac/primitives.h | 4 +- .../serializers/vac/sequence_containers.h | 2 +- .../vajson}/writer/serializers/vac/variant.h | 0 .../vajson}/writer/types/array_type.h | 6 +- .../vajson}/writer/types/basic_types.h | 40 +--- .../vajson}/writer/types/bin_types.h | 51 +----- .../vajson}/writer/types/object_type.h | 6 +- .../vajson}/writer/types/object_type_impl.h | 7 +- 27 files changed, 460 insertions(+), 182 deletions(-) create mode 100644 score/json/internal/writer/vajson/BUILD create mode 100644 score/json/internal/writer/vajson/vajson_serialize.cpp create mode 100644 score/json/internal/writer/vajson/vajson_serialize.h create mode 100644 score/json/internal/writer/vajson/vajson_serialize_test.cpp rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers.h (91%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl.h (83%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/associative_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/primitives.h (97%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/sequence_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/generic_value_serializer.h (90%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/generic_value_serializer_impl.h (90%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/key_serializer.h (89%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/serializer.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/util/escaped_json_string.h (92%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/util/length_serializer.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac.h (83%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/primitives.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/sequence_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/variant.h (100%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/array_type.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/basic_types.h (93%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/bin_types.h (87%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/object_type.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/object_type_impl.h (89%) diff --git a/score/json/internal/parser/vajson/vajson_impl/BUILD b/score/json/internal/parser/vajson/vajson_impl/BUILD index 7aa56fc245..c518029488 100644 --- a/score/json/internal/parser/vajson/vajson_impl/BUILD +++ b/score/json/internal/parser/vajson/vajson_impl/BUILD @@ -54,26 +54,6 @@ cc_library( "util/json_error_domain.h", "util/number.h", "util/types.h", - "writer/serializers.h", - "writer/serializers/stl.h", - "writer/serializers/stl/associative_containers.h", - "writer/serializers/stl/primitives.h", - "writer/serializers/stl/sequence_containers.h", - "writer/serializers/vac.h", - "writer/serializers/vac/primitives.h", - "writer/serializers/vac/sequence_containers.h", - "writer/serializers/vac/variant.h", - "writer/serializers/structures/generic_value_serializer.h", - "writer/serializers/structures/generic_value_serializer_impl.h", - "writer/serializers/structures/key_serializer.h", - "writer/serializers/structures/serializer.h", - "writer/serializers/util/escaped_json_string.h", - "writer/serializers/util/length_serializer.h", - "writer/types/array_type.h", - "writer/types/basic_types.h", - "writer/types/bin_types.h", - "writer/types/object_type.h", - "writer/types/object_type_impl.h", ], includes = [ "include", diff --git a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h index 05a1645868..3cb1d396fa 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h @@ -286,4 +286,14 @@ inline void AssertCondition(bool value, CStr message = "") noexcept } // namespace json } // namespace score +namespace amsr +{ +namespace json +{ + +using score::json::vajson::AssertCondition; + +} // namespace json +} // namespace amsr + #endif // SCORE_LIB_JSON_INTERNAL_PARSER_VAJSON_JSON_UTIL_JSON_ERROR_DOMAIN_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/util/types.h b/score/json/internal/parser/vajson/vajson_impl/util/types.h index 2d831ca3c0..157643af70 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/types.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/types.h @@ -65,4 +65,23 @@ enum class EncodingType : std::uint8_t } // namespace json } // namespace score +namespace amsr +{ +namespace json +{ + +template +using Optional = score::json::vajson::Optional; + +using CStringView = score::json::vajson::CStringView; +using StringView = score::json::vajson::StringView; +using String = score::json::vajson::String; +using Bytes = score::json::vajson::Bytes; +using EncodingType = score::json::vajson::EncodingType; + +using score::json::vajson::operator""sv; + +} // namespace json +} // namespace amsr + #endif // SCORE_LIB_JSON_INTERNAL_PARSER_VAJSON_JSON_UTIL_TYPES_H_ diff --git a/score/json/internal/writer/vajson/BUILD b/score/json/internal/writer/vajson/BUILD new file mode 100644 index 0000000000..273e6ec161 --- /dev/null +++ b/score/json/internal/writer/vajson/BUILD @@ -0,0 +1,43 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") +load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") + +cc_library( + name = "vajson_serialize", + srcs = ["vajson_serialize.cpp"], + hdrs = ["vajson_serialize.h"] + glob([ + "writer/**/*.h", + ]), + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["@score_baselibs//score/json:__subpackages__"], + deps = [ + "@score_baselibs//score/json/internal/model", + "@score_baselibs//score/json/internal/parser/vajson/vajson_impl", + "@score_baselibs//score/language/futurecpp", + "@score_baselibs//score/result", + ], +) + +cc_test( + name = "vajson_serialize_test", + srcs = ["vajson_serialize_test.cpp"], + features = COMPILER_WARNING_FEATURES + ["aborts_upon_exception"], + tags = ["unit"], + deps = [ + ":vajson_serialize", + "@googletest//:gtest_main", + ], +) diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp new file mode 100644 index 0000000000..2a1ac7c784 --- /dev/null +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -0,0 +1,75 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#include "score/json/internal/writer/vajson/vajson_serialize.h" + +#include +#include +#include +namespace +{ +template +score::Result SerializeToStream(std::ostream& out_stream, const T& json_data) +{ + auto serializer = amsr::json::DocumentSerializer{std::ref(out_stream)}; + static_cast(std::move(serializer) << json_data); + if (out_stream.fail()) + { + return score::Result{score::unexpect, + score::json::MakeError(score::json::Error::kUnknownError, + "vaJSON serializer failed to write to stream")}; + } + return {}; +} +template +score::Result SerializeToBuffer(const T& json_data) +{ + std::ostringstream out_stream{}; + auto result = SerializeToStream(out_stream, json_data); + if (!result.has_value()) + { + return score::Unexpected{result.error()}; + } + return out_stream.str(); +} +} // namespace +namespace score +{ +namespace json +{ +VajsonSerialize::VajsonSerialize(std::ostream& out_stream) noexcept : out_stream_{out_stream} {} +score::Result VajsonSerialize::operator<<(const score::json::Object& json_data) +{ + return SerializeToStream(out_stream_, json_data); +} +score::Result VajsonSerialize::operator<<(const score::json::List& json_data) +{ + return SerializeToStream(out_stream_, json_data); +} +score::Result VajsonSerialize::operator<<(const score::json::Any& json_data) +{ + return SerializeToStream(out_stream_, json_data); +} +score::Result VajsonToBuffer(const score::json::Object& json_data) +{ + return SerializeToBuffer(json_data); +} +score::Result VajsonToBuffer(const score::json::List& json_data) +{ + return SerializeToBuffer(json_data); +} +score::Result VajsonToBuffer(const score::json::Any& json_data) +{ + return SerializeToBuffer(json_data); +} +} // namespace json +} // namespace score diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h new file mode 100644 index 0000000000..1427652c9c --- /dev/null +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -0,0 +1,173 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H +#include "score/json/internal/model/any.h" +#include "score/json/internal/model/error.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" +#include "score/result/result.h" +#include +#include +#include +#include +#include +#include +namespace score +{ +namespace json +{ +namespace internal +{ +namespace writer +{ +namespace vajson +{ +class ObjectKeySerializer final +{ + public: + auto operator()(const score::memory::StringComparisonAdaptor& key) const noexcept -> amsr::json::JKeyType + { + return amsr::json::JKey(key.GetAsStringView()); + } +}; +template +auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, + const score::json::Any& value) noexcept -> typename amsr::json::GenericValueSerializer::Next; +template +auto SerializeNumber(amsr::json::GenericValueSerializer&& serializer, + const score::json::Number& value) noexcept -> typename amsr::json::GenericValueSerializer::Next +{ + if (const auto unsigned_value = value.As(); unsigned_value.has_value()) + { + return std::move(serializer) << amsr::json::JNumber(*unsigned_value); + } + if (const auto signed_value = value.As(); signed_value.has_value()) + { + return std::move(serializer) << amsr::json::JNumber(*signed_value); + } + if (const auto float_value = value.As(); float_value.has_value()) + { + return std::move(serializer) << amsr::json::JNumber(*float_value); + } + if (const auto double_value = value.As(); double_value.has_value()) + { + return std::move(serializer) << amsr::json::JNumber(*double_value); + } + SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(false, "Unable to serialize score::json::Number with vaJSON adapter."); + return std::move(serializer) << amsr::json::JNull(); +} +template +auto SerializeList(amsr::json::GenericValueSerializer&& serializer, + const score::json::List& list) noexcept -> typename amsr::json::GenericValueSerializer::Next +{ + return std::move(serializer) << amsr::json::JArray([&list](amsr::json::ArrayStart array_serializer) noexcept { + auto next = std::move(array_serializer); + for (const auto& value : list) + { + next = SerializeValue(std::move(next), value); + } + }); +} +template +auto SerializeObject(amsr::json::GenericValueSerializer&& serializer, + const score::json::Object& object) noexcept -> typename amsr::json::GenericValueSerializer::Next +{ + return std::move(serializer) << amsr::json::JObject([&object](amsr::json::ObjectStart object_serializer) noexcept { + auto next = std::move(object_serializer); + for (const auto& element : object) + { + auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); + next = SerializeValue(std::move(value_serializer), element.second); + } + return std::move(next); + }); +} +template +auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, + const score::json::Any& value) noexcept -> typename amsr::json::GenericValueSerializer::Next +{ + if (const auto object = value.As(); object.has_value()) + { + return SerializeObject(std::move(serializer), object->get()); + } + if (const auto list = value.As(); list.has_value()) + { + return SerializeList(std::move(serializer), list->get()); + } + if (const auto string_value = value.As(); string_value.has_value()) + { + return std::move(serializer) << amsr::json::JString(string_value->get()); + } + if (const auto null_value = value.As(); null_value.has_value()) + { + score::cpp::ignore = null_value; + return std::move(serializer) << amsr::json::JNull(); + } + if (const auto number = value.As(); number.has_value()) + { + return SerializeNumber(std::move(serializer), number->get()); + } + const auto boolean = value.As(); + SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(boolean.has_value(), + "Unable to determine score::json::Any type for vaJSON serialization."); + return std::move(serializer) << amsr::json::JBool(*boolean); +} +} // namespace vajson +} // namespace writer +} // namespace internal +class VajsonSerialize final +{ + public: + explicit VajsonSerialize(std::ostream& out_stream) noexcept; + ~VajsonSerialize() noexcept = default; + VajsonSerialize(const VajsonSerialize&) = delete; + VajsonSerialize(VajsonSerialize&&) noexcept = default; + VajsonSerialize& operator=(const VajsonSerialize&) = delete; + VajsonSerialize& operator=(VajsonSerialize&&) = delete; + score::Result operator<<(const score::json::Object& json_data); + score::Result operator<<(const score::json::List& json_data); + score::Result operator<<(const score::json::Any& json_data); + private: + std::ostream& out_stream_; +}; +score::Result VajsonToBuffer(const score::json::Object& json_data); +score::Result VajsonToBuffer(const score::json::List& json_data); +score::Result VajsonToBuffer(const score::json::Any& json_data); +} // namespace json +} // namespace score +namespace amsr +{ +namespace json +{ +template +auto operator<<(GenericValueSerializer&& serializer, + const score::json::Object& value) noexcept -> typename GenericValueSerializer::Next +{ + return score::json::internal::writer::vajson::SerializeObject(std::move(serializer), value); +} +template +auto operator<<(GenericValueSerializer&& serializer, + const score::json::List& value) noexcept -> typename GenericValueSerializer::Next +{ + return score::json::internal::writer::vajson::SerializeList(std::move(serializer), value); +} +template +auto operator<<(GenericValueSerializer&& serializer, + const score::json::Any& value) noexcept -> typename GenericValueSerializer::Next +{ + return score::json::internal::writer::vajson::SerializeValue(std::move(serializer), value); +} +} // namespace json +} // namespace amsr +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp new file mode 100644 index 0000000000..175a468e17 --- /dev/null +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -0,0 +1,57 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#include "score/json/internal/writer/vajson/vajson_serialize.h" +#include +namespace score +{ +namespace json +{ +namespace +{ +TEST(VajsonSerializeTest, SerializesNestedAnyToCompactJson) +{ + Object nested_object{}; + nested_object["number"] = Any{std::int32_t{7}}; + List list{}; + list.emplace_back(Any{Null{}}); + list.emplace_back(Any{std::move(nested_object)}); + Object root{}; + root["boolean"] = Any{true}; + root["list"] = Any{std::move(list)}; + root["string"] = Any{std::string{"line1\n\"quoted\"\\line2"}}; + const auto result = VajsonToBuffer(Any{std::move(root)}); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, + std::string{"{\"boolean\":true,\"list\":[null,{\"number\":7}],\"string\":\"line1\\n\\\"quoted\\\"\\\\line2\"}"}); +} +TEST(VajsonSerializeTest, SerializesObjectKeysUsingStringComparisonAdaptor) +{ + Object object{}; + object[std::string_view{"alpha"}] = Any{std::string{"a"}}; + object["beta"] = Any{std::uint32_t{2U}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"alpha\":\"a\",\"beta\":2}"}); +} +TEST(VajsonSerializeTest, SerializesTopLevelList) +{ + List list{}; + list.emplace_back(Any{std::uint8_t{5U}}); + list.emplace_back(Any{std::string{"value"}}); + const auto result = VajsonToBuffer(list); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"[5,\"value\"]"}); +} +} // namespace +} // namespace json +} // namespace score diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h b/score/json/internal/writer/vajson/writer/serializers.h similarity index 91% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers.h rename to score/json/internal/writer/vajson/writer/serializers.h index ffa325b203..4791634e20 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h +++ b/score/json/internal/writer/vajson/writer/serializers.h @@ -27,7 +27,7 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "amsr/json/writer/serializers/stl.h" -#include "amsr/json/writer/serializers/vac.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h b/score/json/internal/writer/vajson/writer/serializers/stl.h similarity index 83% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h rename to score/json/internal/writer/vajson/writer/serializers/stl.h index 4ed6b5a98a..4a3fc62186 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "amsr/json/writer/serializers/stl/associative_containers.h" -#include "amsr/json/writer/serializers/stl/primitives.h" -#include "amsr/json/writer/serializers/stl/sequence_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/primitives.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h rename to score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h index 8899379ff7..22f77d412e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h @@ -28,8 +28,8 @@ #include #include #include -#include "amsr/json/writer/types/array_type.h" -#include "amsr/json/writer/types/object_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h similarity index 97% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h rename to score/json/internal/writer/vajson/writer/serializers/stl/primitives.h index 7d3e63ca22..666b82638e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h @@ -28,8 +28,8 @@ #include #include -#include "amsr/json/util/types.h" -#include "amsr/json/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h rename to score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h index e18a0c1871..4577fafc14 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h @@ -28,7 +28,7 @@ #include #include #include -#include "amsr/json/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h similarity index 90% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 62445e9013..8409fcce11 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -27,16 +27,15 @@ #include #include -#include "score/language/futurecpp/charconv.hpp" -#include "amsr/json/util/json_error_domain.h" -#include "amsr/json/util/types.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/serializers/util/escaped_json_string.h" -#include "amsr/json/writer/serializers/util/length_serializer.h" -#include "amsr/json/writer/types/array_type.h" -#include "amsr/json/writer/types/basic_types.h" -#include "amsr/json/writer/types/bin_types.h" -#include "amsr/json/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/bin_types.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { @@ -177,12 +176,12 @@ class GenericValueSerializer final { */ // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const auto operator<<(JBoolType b) && noexcept -> Next { - score::safecpp::zstring_view const value{b.value ? "true" : "false"}; + const std::string_view value{b.value ? "true" : "false"}; // NOLINTNEXTLINE(whitespace/line_length) // VECTOR NL AutosarC++17_10-M8.5.1: MD_JSON_AutosarC++17_10-M8.5.1_use_of_possibly_unintialized_value_false_positive return this->Serialize([this, value]() noexcept { // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().write(value.data(), value.size()); + this->os_.get().write(value.data(), static_cast(value.size())); }); } @@ -213,17 +212,13 @@ class GenericValueSerializer final { std::array buffer{}; T value = static_cast(number.GetValue()); - // Convert number to string using std::to_chars - score::cpp::to_chars_result conversion_result{score::cpp::to_chars(buffer.data(), buffer.data() + buffer.size(), value)}; - - AssertCondition(ec == std::errc{}, + const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); + + AssertCondition(conversion_result.ec == std::errc{}, "GenericValueSerializer: Could not convert number to textual representation."); - // Create a span from the buffer to the end of written data - score::cpp::span result_span{buffer.data(), static_cast(ptr - buffer.data())}; - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().write(result_span.data(), result_span.size()); + this->os_.get().write(buffer.data(), static_cast(conversion_result.ptr - buffer.data())); }); } @@ -253,11 +248,11 @@ class GenericValueSerializer final { auto operator<<(JStringType string) && noexcept -> Next { return this->Serialize([this, string]() noexcept { // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('"'); + this->os_.get().put('"'); // VCA_VAJSON_OUTPUTSTREAM this->os_.get() << internal::EscapedJsonString(string); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('"'); + this->os_.get().put('"'); }); } @@ -293,7 +288,7 @@ class GenericValueSerializer final { internal::SerializeLength(this->os_.get(), string.GetLength()); // VCA_VAJSON_OUTPUTSTREAM auto const& value = string.GetValue(); - this->os_.get().write(value.data(), value.size()); + this->os_.get().write(value.data(), static_cast(value.size())); }); } @@ -326,10 +321,10 @@ class GenericValueSerializer final { auto operator<<(JArrayType tuple) && noexcept -> Next { return this->Serialize([this, tuple]() noexcept { // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('['); + this->os_.get().put('['); static_cast(tuple.fn(ArrayStart(this->os_.get()))); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put(']'); + this->os_.get().put(']'); }); } @@ -365,7 +360,7 @@ class GenericValueSerializer final { internal::SerializeLength(this->os_.get(), bin.GetLength()); // VCA_VAJSON_OUTPUTSTREAM auto const& value = bin.GetValue(); - this->os_.get().write(value.data(), value.size()); + this->os_.get().write(value.data(), static_cast(value.size())); }); } @@ -414,7 +409,7 @@ class GenericValueSerializer final { auto Serialize(Fn&& fn) const noexcept -> Next { if (this->serializer_state_ == SerializerState::kNonEmpty) { // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put(','); + this->os_.get().put(','); } std::forward(fn)(); return Next(this->os_.get(), SerializerState::kNonEmpty); diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h similarity index 90% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h rename to score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h index 57bdbb8296..ddead882ac 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h @@ -21,7 +21,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "amsr/json/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" namespace amsr { namespace json { @@ -52,11 +53,11 @@ auto GenericValueSerializer::operator<<(JObjectType object) && noexc static_assert(std::is_same::value, "Cannot close object in current state"); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('{'); + this->os_.get().put('{'); // VCA_VAJSON_OUTPUTSTREAM static_cast(object.fn(ObjectStart(this->os_.get()))); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('}'); + this->os_.get().put('}'); }); } diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h similarity index 89% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h index a49367d017..b58253c827 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h @@ -21,13 +21,13 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "amsr/json/util/types.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/serializers/util/escaped_json_string.h" -#include "amsr/json/writer/serializers/util/length_serializer.h" -#include "amsr/json/writer/types/array_type.h" -#include "amsr/json/writer/types/basic_types.h" -#include "amsr/json/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { @@ -175,11 +175,12 @@ class KeySerializer final { this->WriteComma(); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put('k'); + this->os_.get().put('k'); // VCA_VAJSON_OUTPUTSTREAM internal::SerializeLength(this->os_.get(), key.GetLength()); // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Write(key.GetValue()); + const auto value = key.GetValue(); + this->os_.get().write(value.data(), value.size()); return Next(this->os_.get()); } @@ -204,7 +205,7 @@ class KeySerializer final { void WriteComma() const noexcept { if (this->serializer_state_ == SerializerState::kNonEmpty) { // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().Put(','); + this->os_.get().put(','); } } diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/serializer.h index 5bcbc1b0a5..85ddd81ca8 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h @@ -22,7 +22,8 @@ *********************************************************************************************************************/ #include #include -#include "amsr/json/util/types.h" + +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h similarity index 92% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h rename to score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 3311e529ff..9084393003 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -26,9 +26,9 @@ #include #include -#include "amsr/json/util/types.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { @@ -142,37 +142,37 @@ auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> s switch (std::char_traits::to_int_type(ch)) { case std::char_traits::to_int_type('"'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\")", 1); + os.write("\\\"", 2); break; } case std::char_traits::to_int_type('\\'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\\)", 1); + os.write("\\\\", 2); break; } case std::char_traits::to_int_type('\b'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\b)", 1); + os.write("\\b", 2); break; } case std::char_traits::to_int_type('\f'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\f)", 1); + os.write("\\f", 2); break; } case std::char_traits::to_int_type('\n'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\n)", 1); + os.write("\\n", 2); break; } case std::char_traits::to_int_type('\r'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\r)", 1); + os.write("\\r", 2); break; } case std::char_traits::to_int_type('\t'): { // VCA_VAJSON_OUTPUTSTREAM - os.write(R"(\t)", 1); + os.write("\\t", 2); break; } default: diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h index 6204b79c33..1cffc95445 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h @@ -23,7 +23,8 @@ *********************************************************************************************************************/ #include #include -#include "amsr/json/writer/serializers/structures/serializer.h" + +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h b/score/json/internal/writer/vajson/writer/serializers/vac.h similarity index 83% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h rename to score/json/internal/writer/vajson/writer/serializers/vac.h index 70cfea97cc..c8a998571e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "amsr/json/writer/serializers/vac/primitives.h" -#include "amsr/json/writer/serializers/vac/sequence_containers.h" -#include "amsr/json/writer/serializers/vac/variant.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/primitives.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/variant.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h rename to score/json/internal/writer/vajson/writer/serializers/vac/primitives.h index 0a250fb198..d6d03f3446 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h @@ -28,8 +28,8 @@ #include #include -#include "amsr/json/util/types.h" -#include "amsr/json/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h rename to score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h index fbde47bf49..1f48c8b99b 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h @@ -26,7 +26,7 @@ *********************************************************************************************************************/ #include -#include "amsr/json/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" #include "score/span.hpp" #include "score/vector.hpp" diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h b/score/json/internal/writer/vajson/writer/serializers/vac/variant.h similarity index 100% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h rename to score/json/internal/writer/vajson/writer/serializers/vac/variant.h diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h b/score/json/internal/writer/vajson/writer/types/array_type.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h rename to score/json/internal/writer/vajson/writer/types/array_type.h index b591f3d953..64573cf0ac 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h +++ b/score/json/internal/writer/vajson/writer/types/array_type.h @@ -28,10 +28,8 @@ #include #include -#include "amsr/json/writer/serializers/stl/primitives.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/serializers/vac/primitives.h" -#include "amsr/json/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h similarity index 93% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h rename to score/json/internal/writer/vajson/writer/types/basic_types.h index bb3eda6405..fb065335f0 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -28,7 +28,7 @@ #include #include -#include "amsr/json/util/types.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" namespace amsr { namespace json { @@ -150,25 +150,6 @@ class JKeyType final { */ constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s}; } -/*! - * \brief Serializes a Key value - * \vpublic - * \param[in] s - * Key to serialize. - * \return The serializable key type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view{s}); } - /*! * \brief Serializes a Key value * \vpublic @@ -506,25 +487,6 @@ constexpr auto JString(std::string_view s) noexcept -> JStringType { return JStr */ inline auto JString(std::string const& s) noexcept -> JStringType { return JString(std::string_view{s}); } -/*! - * \brief Serializes a String value - * \vpublic - * - * \param[in] s - * String to serialize. - * \return The serializable string type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JString(std::string const& s) noexcept -> JStringType { return JString(std::string_view{s}); } - /*! * \brief A function object used to serialize predefined serializers * \vprivate Vector component internal API diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h b/score/json/internal/writer/vajson/writer/types/bin_types.h similarity index 87% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h rename to score/json/internal/writer/vajson/writer/types/bin_types.h index 9d15451f57..e735da2788 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h +++ b/score/json/internal/writer/vajson/writer/types/bin_types.h @@ -25,8 +25,9 @@ *********************************************************************************************************************/ #include #include -#include "amsr/json/util/types.h" -#include "amsr/msra_always_assert.h" + +#include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" namespace amsr { namespace json { @@ -45,7 +46,7 @@ namespace internal { * \endspec */ inline auto ConvertLength(std::size_t const length) noexcept -> std::uint32_t { - msra_always_assert(length <= std::numeric_limits::max()); + AssertCondition(length <= std::numeric_limits::max(), "Length exceeds std::uint32_t range."); // VECTOR NCL AutosarC++17_10-A4.7.1: MD_JSON_AutosarC++17_10-A4.7.1_truncating_cast // VECTOR NL AutosarC++17_10-M0.3.1: MD_JSON_AutosarC++17_10-M0.3.1_OOB @@ -77,7 +78,7 @@ class JBinType final { * requires true; * \endspec */ - constexpr explicit JBinType(score::cpp::span b) noexcept : value_(b) {} + explicit JBinType(score::cpp::span b) noexcept : value_(b) {} /*! * \brief Returns the contained value @@ -131,7 +132,7 @@ class JBinType final { * requires true; * \endspec */ -constexpr auto JBin(score::cpp::span b) noexcept -> JBinType { return JBinType{b}; } +inline auto JBin(score::cpp::span b) noexcept -> JBinType { return JBinType{b}; } /*! * \brief A binary string type @@ -211,27 +212,6 @@ class JBinStringType final { */ constexpr auto JBinString(StringView s) noexcept -> JBinStringType { return JBinStringType{s}; } -/*! - * \brief Serializes a string as binary - * \vpublic - * - * \param[in] s - * The string to serialize. - * \return The serializable string value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JBinString(std::string const& s) noexcept -> JBinStringType { - return JBinString(std::string_view{s}); -} - /*! * \brief Serializes a string as binary * \vpublic @@ -329,25 +309,6 @@ class JBinKeyType final { */ constexpr auto JBinKey(StringView s) noexcept -> JBinKeyType { return JBinKeyType{s}; } -/*! - * \brief Serializes a key as binary - * \vpublic - * - * \param[in] s - * The key to serialize. - * \return The serializable key value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JBinKey(std::string const& s) noexcept -> JBinKeyType { return JBinKey(std::string_view{s}); } - /*! * \brief Serializes a key as binary * \vpublic diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h rename to score/json/internal/writer/vajson/writer/types/object_type.h index d4d1d11541..967acdd6eb 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -27,9 +27,9 @@ #include #include -#include "amsr/json/util/types.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h b/score/json/internal/writer/vajson/writer/types/object_type_impl.h similarity index 89% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h rename to score/json/internal/writer/vajson/writer/types/object_type_impl.h index ef87c1c928..a287103dae 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h +++ b/score/json/internal/writer/vajson/writer/types/object_type_impl.h @@ -22,9 +22,10 @@ * INCLUDES *********************************************************************************************************************/ #include -#include "amsr/json/writer/serializers/structures/generic_value_serializer.h" -#include "amsr/json/writer/serializers/structures/serializer.h" -#include "amsr/json/writer/types/object_type.h" + +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { From ba4035b86fbc99a70289d809c496a483a545af65 Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 3 Jun 2026 13:33:23 +0200 Subject: [PATCH 04/21] Add adapter --- score/json/BUILD | 13 ++++ .../writer/vajson/vajson_serialize_test.cpp | 1 + score/json/vajson_json_writer.cpp | 74 +++++++++++++++++++ score/json/vajson_json_writer.h | 57 ++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 score/json/vajson_json_writer.cpp create mode 100644 score/json/vajson_json_writer.h diff --git a/score/json/BUILD b/score/json/BUILD index 0dfcaef2da..bc45fabe5a 100644 --- a/score/json/BUILD +++ b/score/json/BUILD @@ -119,6 +119,19 @@ cc_library( ], ) +cc_library( + name = "vajson_json_writer_impl", + srcs = ["vajson_json_writer.cpp"], + hdrs = ["vajson_json_writer.h"], + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["//visibility:public"], + deps = [ + ":writer_interface", + "@score_baselibs//score/json/internal/writer/vajson:vajson_serialize", + ], +) + cc_library( name = "json", features = COMPILER_WARNING_FEATURES, diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index 175a468e17..55f2423477 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -11,6 +11,7 @@ * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ #include "score/json/internal/writer/vajson/vajson_serialize.h" + #include namespace score { diff --git a/score/json/vajson_json_writer.cpp b/score/json/vajson_json_writer.cpp new file mode 100644 index 0000000000..32d10a96b0 --- /dev/null +++ b/score/json/vajson_json_writer.cpp @@ -0,0 +1,74 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +#include "score/json/vajson_json_writer.h" +#include "score/json/internal/model/error.h" +#include "score/json/internal/writer/vajson/vajson_serialize.h" +namespace +{ +template +score::Result ToFileInternal(const T& json_data, + const std::string_view& file_path, + score::filesystem::IFileFactory& file_factory) +{ + const std::string file_path_string{file_path.data(), file_path.size()}; + const auto file = file_factory.Open(file_path_string, std::ios::out | std::ios::trunc); + if (!file.has_value()) + { + return score::Result{score::unexpect, + score::json::MakeError(score::json::Error::kInvalidFilePath, "Failed to open file")}; + } + score::json::VajsonSerialize serializer{**file}; + return serializer << json_data; +} +template +score::Result ToBufferInternal(const T& json_data) +{ + return score::json::VajsonToBuffer(json_data); +} +} // namespace +namespace score +{ +namespace json +{ +score::Result VajsonJsonWriter::ToFile(const score::json::Object& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) +{ + return ToFileInternal(json_data, file_path, *file_factory); +} +score::Result VajsonJsonWriter::ToFile(const score::json::List& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) +{ + return ToFileInternal(json_data, file_path, *file_factory); +} +score::Result VajsonJsonWriter::ToFile(const score::json::Any& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) +{ + return ToFileInternal(json_data, file_path, *file_factory); +} +score::Result VajsonJsonWriter::ToBuffer(const score::json::Object& json_data) +{ + return ToBufferInternal(json_data); +} +score::Result VajsonJsonWriter::ToBuffer(const score::json::List& json_data) +{ + return ToBufferInternal(json_data); +} +score::Result VajsonJsonWriter::ToBuffer(const score::json::Any& json_data) +{ + return ToBufferInternal(json_data); +} +} // namespace json +} // namespace score diff --git a/score/json/vajson_json_writer.h b/score/json/vajson_json_writer.h new file mode 100644 index 0000000000..b8d17919dd --- /dev/null +++ b/score/json/vajson_json_writer.h @@ -0,0 +1,57 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#ifndef SCORE_LIB_JSON_VAJSON_JSON_WRITER_H +#define SCORE_LIB_JSON_VAJSON_JSON_WRITER_H + +#include "score/json/i_json_writer.h" + +#include + +namespace score +{ +namespace json +{ + +/// @brief Implementation of IJsonWriter using the vaJSON serializer (VajsonSerialize). +class VajsonJsonWriter final : public IJsonWriter +{ + public: + VajsonJsonWriter() noexcept = default; + VajsonJsonWriter(const VajsonJsonWriter&) = delete; + VajsonJsonWriter(VajsonJsonWriter&&) noexcept = delete; + VajsonJsonWriter& operator=(const VajsonJsonWriter&) = delete; + VajsonJsonWriter& operator=(VajsonJsonWriter&&) noexcept = delete; + ~VajsonJsonWriter() noexcept override = default; + + score::Result ToFile(const score::json::Object& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) override; + + score::Result ToFile(const score::json::List& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) override; + + score::Result ToFile(const score::json::Any& json_data, + const std::string_view& file_path, + std::shared_ptr file_factory) override; + + score::Result ToBuffer(const score::json::Object& json_data) override; + score::Result ToBuffer(const score::json::List& json_data) override; + score::Result ToBuffer(const score::json::Any& json_data) override; +}; + +} // namespace json +} // namespace score + +#endif // SCORE_LIB_JSON_VAJSON_JSON_WRITER_H From ca5628f28307ac935a88a5748e1473c163188191 Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 26 Aug 2026 13:06:56 +0200 Subject: [PATCH 05/21] Move vajson serializer to vajson parser directory --- score/json/BUILD | 2 +- .../internal/parser/vajson/unit_test/BUILD | 1 + .../writer}/vajson_serialize_test.cpp | 2 +- .../internal/parser/vajson/vajson_impl/BUILD | 37 ++++++++++++++++ .../vajson/vajson_impl}/vajson_serialize.cpp | 2 +- .../vajson/vajson_impl}/vajson_serialize.h | 6 +-- .../vajson/vajson_impl}/writer/serializers.h | 4 +- .../vajson_impl}/writer/serializers/stl.h | 6 +-- .../serializers/stl/associative_containers.h | 4 +- .../writer/serializers/stl/primitives.h | 2 +- .../serializers/stl/sequence_containers.h | 2 +- .../structures/generic_value_serializer.h | 14 +++--- .../generic_value_serializer_impl.h | 4 +- .../serializers/structures/key_serializer.h | 12 +++--- .../serializers/structures/serializer.h | 0 .../serializers/util/escaped_json_string.h | 4 +- .../serializers/util/length_serializer.h | 2 +- .../vajson_impl}/writer/serializers/vac.h | 6 +-- .../writer/serializers/vac/primitives.h | 2 +- .../serializers/vac/sequence_containers.h | 2 +- .../writer/serializers/vac/variant.h | 0 .../vajson_impl}/writer/types/array_type.h | 4 +- .../vajson_impl}/writer/types/basic_types.h | 2 +- .../vajson_impl}/writer/types/bin_types.h | 0 .../vajson_impl}/writer/types/object_type.h | 4 +- .../writer/types/object_type_impl.h | 6 +-- score/json/internal/writer/vajson/BUILD | 43 ------------------- score/json/vajson_json_writer.cpp | 2 +- 28 files changed, 85 insertions(+), 90 deletions(-) rename score/json/internal/{writer/vajson => parser/vajson/unit_test/writer}/vajson_serialize_test.cpp (96%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/vajson_serialize.cpp (97%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/vajson_serialize.h (96%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers.h (90%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/stl.h (81%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/stl/associative_containers.h (98%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/stl/primitives.h (98%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/stl/sequence_containers.h (98%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/structures/generic_value_serializer.h (95%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/structures/generic_value_serializer_impl.h (92%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/structures/key_serializer.h (91%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/structures/serializer.h (100%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/util/escaped_json_string.h (96%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/util/length_serializer.h (96%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/vac.h (81%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/vac/primitives.h (98%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/vac/sequence_containers.h (98%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/serializers/vac/variant.h (100%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/types/array_type.h (96%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/types/basic_types.h (99%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/types/bin_types.h (100%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/types/object_type.h (97%) rename score/json/internal/{writer/vajson => parser/vajson/vajson_impl}/writer/types/object_type_impl.h (87%) delete mode 100644 score/json/internal/writer/vajson/BUILD diff --git a/score/json/BUILD b/score/json/BUILD index bc45fabe5a..f9cfb0e132 100644 --- a/score/json/BUILD +++ b/score/json/BUILD @@ -128,7 +128,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":writer_interface", - "@score_baselibs//score/json/internal/writer/vajson:vajson_serialize", + "@score_baselibs//score/json/internal/parser/vajson/vajson_impl:vajson_serialize", ], ) diff --git a/score/json/internal/parser/vajson/unit_test/BUILD b/score/json/internal/parser/vajson/unit_test/BUILD index 945ee2e19a..f0932a0ba2 100644 --- a/score/json/internal/parser/vajson/unit_test/BUILD +++ b/score/json/internal/parser/vajson/unit_test/BUILD @@ -25,6 +25,7 @@ cc_test( ], deps = [ "//score/json/internal/parser/vajson:vajson_parser", + "//score/json/internal/parser/vajson/vajson_impl:vajson_serialize", "@googletest//:gtest_main", ], ) diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp similarity index 96% rename from score/json/internal/writer/vajson/vajson_serialize_test.cpp rename to score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp index 55f2423477..ed439d9a32 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -#include "score/json/internal/writer/vajson/vajson_serialize.h" +#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" #include namespace score diff --git a/score/json/internal/parser/vajson/vajson_impl/BUILD b/score/json/internal/parser/vajson/vajson_impl/BUILD index 38cb940b9b..65feff4be9 100644 --- a/score/json/internal/parser/vajson/vajson_impl/BUILD +++ b/score/json/internal/parser/vajson/vajson_impl/BUILD @@ -11,6 +11,9 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* +load("@rules_cc//cc:defs.bzl", "cc_library") +load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") + package(default_visibility = ["@score_baselibs//score/json:__subpackages__"]) cc_library( @@ -52,6 +55,26 @@ cc_library( "util/json_error_domain.h", "util/number.h", "util/types.h", + "writer/serializers.h", + "writer/serializers/stl.h", + "writer/serializers/stl/associative_containers.h", + "writer/serializers/stl/primitives.h", + "writer/serializers/stl/sequence_containers.h", + "writer/serializers/structures/generic_value_serializer.h", + "writer/serializers/structures/generic_value_serializer_impl.h", + "writer/serializers/structures/key_serializer.h", + "writer/serializers/structures/serializer.h", + "writer/serializers/util/escaped_json_string.h", + "writer/serializers/util/length_serializer.h", + "writer/serializers/vac.h", + "writer/serializers/vac/primitives.h", + "writer/serializers/vac/sequence_containers.h", + "writer/serializers/vac/variant.h", + "writer/types/array_type.h", + "writer/types/basic_types.h", + "writer/types/bin_types.h", + "writer/types/object_type.h", + "writer/types/object_type_impl.h", ], includes = [ "include", @@ -64,3 +87,17 @@ cc_library( "@score_baselibs//score/result", ], ) + +cc_library( + name = "vajson_serialize", + srcs = ["vajson_serialize.cpp"], + hdrs = ["vajson_serialize.h"], + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + deps = [ + ":vajson_impl", + "@score_baselibs//score/json/internal/model", + "@score_baselibs//score/language/futurecpp", + "@score_baselibs//score/result", + ], +) diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp similarity index 97% rename from score/json/internal/writer/vajson/vajson_serialize.cpp rename to score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp index 2a1ac7c784..f58d80aa15 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -#include "score/json/internal/writer/vajson/vajson_serialize.h" +#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" #include #include diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h similarity index 96% rename from score/json/internal/writer/vajson/vajson_serialize.h rename to score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h index 1427652c9c..92efddde0c 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h @@ -14,8 +14,8 @@ #define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H #include "score/json/internal/model/any.h" #include "score/json/internal/model/error.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h" #include "score/result/result.h" #include #include @@ -90,7 +90,7 @@ auto SerializeObject(amsr::json::GenericValueSerializer&& serializer, auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); next = SerializeValue(std::move(value_serializer), element.second); } - return std::move(next); + return next; }); } template diff --git a/score/json/internal/writer/vajson/writer/serializers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h similarity index 90% rename from score/json/internal/writer/vajson/writer/serializers.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers.h index 4791634e20..cf0921c751 100644 --- a/score/json/internal/writer/vajson/writer/serializers.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h @@ -27,7 +27,7 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/writer/vajson/writer/serializers/stl.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ diff --git a/score/json/internal/writer/vajson/writer/serializers/stl.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h similarity index 81% rename from score/json/internal/writer/vajson/writer/serializers/stl.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h index 4a3fc62186..96873511cc 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h" -#include "score/json/internal/writer/vajson/writer/serializers/stl/primitives.h" -#include "score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h similarity index 98% rename from score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h index 22f77d412e..90b44bb3bf 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h @@ -28,8 +28,8 @@ #include #include #include -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h similarity index 98% rename from score/json/internal/writer/vajson/writer/serializers/stl/primitives.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h index 666b82638e..d695919af2 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h @@ -29,7 +29,7 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h similarity index 98% rename from score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h index 4577fafc14..e46d683a18 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h @@ -28,7 +28,7 @@ #include #include #include -#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h similarity index 95% rename from score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h index 8409fcce11..ccb5c92c0f 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h @@ -29,13 +29,13 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" -#include "score/json/internal/writer/vajson/writer/types/bin_types.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h similarity index 92% rename from score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h index ddead882ac..050e61d236 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h @@ -21,8 +21,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h similarity index 91% rename from score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h index b58253c827..de85b4000c 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h @@ -22,12 +22,12 @@ * INCLUDES *********************************************************************************************************************/ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h similarity index 100% rename from score/json/internal/writer/vajson/writer/serializers/structures/serializer.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h similarity index 96% rename from score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h index 9084393003..22c4abd082 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h @@ -27,8 +27,8 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h similarity index 96% rename from score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h index 1cffc95445..89a0825eff 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h @@ -24,7 +24,7 @@ #include #include -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/vac.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h similarity index 81% rename from score/json/internal/writer/vajson/writer/serializers/vac.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h index c8a998571e..0cd758badc 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/writer/vajson/writer/serializers/vac/primitives.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac/variant.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h similarity index 98% rename from score/json/internal/writer/vajson/writer/serializers/vac/primitives.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h index d6d03f3446..12a5b007e3 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h @@ -29,7 +29,7 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h similarity index 98% rename from score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h index 1f48c8b99b..21a98818ce 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h @@ -26,7 +26,7 @@ *********************************************************************************************************************/ #include -#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" #include "score/span.hpp" #include "score/vector.hpp" diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/variant.h b/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h similarity index 100% rename from score/json/internal/writer/vajson/writer/serializers/vac/variant.h rename to score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h diff --git a/score/json/internal/writer/vajson/writer/types/array_type.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h similarity index 96% rename from score/json/internal/writer/vajson/writer/types/array_type.h rename to score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h index 64573cf0ac..02ea1f785d 100644 --- a/score/json/internal/writer/vajson/writer/types/array_type.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h @@ -28,8 +28,8 @@ #include #include -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h similarity index 99% rename from score/json/internal/writer/vajson/writer/types/basic_types.h rename to score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h index fb065335f0..7570da5a9c 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h @@ -167,7 +167,7 @@ constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s * requires true; * \endspec */ -constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } +inline auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } inline namespace literals { /*! diff --git a/score/json/internal/writer/vajson/writer/types/bin_types.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h similarity index 100% rename from score/json/internal/writer/vajson/writer/types/bin_types.h rename to score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h diff --git a/score/json/internal/writer/vajson/writer/types/object_type.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h similarity index 97% rename from score/json/internal/writer/vajson/writer/types/object_type.h rename to score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h index 967acdd6eb..8dde53c111 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h @@ -28,8 +28,8 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/writer/types/object_type_impl.h b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h similarity index 87% rename from score/json/internal/writer/vajson/writer/types/object_type_impl.h rename to score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h index a287103dae..57778b1f7e 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type_impl.h +++ b/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h @@ -23,9 +23,9 @@ *********************************************************************************************************************/ #include -#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/writer/vajson/BUILD b/score/json/internal/writer/vajson/BUILD deleted file mode 100644 index 273e6ec161..0000000000 --- a/score/json/internal/writer/vajson/BUILD +++ /dev/null @@ -1,43 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2026 Contributors to the Eclipse Foundation -# -# See the NOTICE file(s) distributed with this work for additional -# information regarding copyright ownership. -# -# This program and the accompanying materials are made available under the -# terms of the Apache License Version 2.0 which is available at -# https://www.apache.org/licenses/LICENSE-2.0 -# -# SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* - -load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") -load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") - -cc_library( - name = "vajson_serialize", - srcs = ["vajson_serialize.cpp"], - hdrs = ["vajson_serialize.h"] + glob([ - "writer/**/*.h", - ]), - features = COMPILER_WARNING_FEATURES, - tags = ["FFI"], - visibility = ["@score_baselibs//score/json:__subpackages__"], - deps = [ - "@score_baselibs//score/json/internal/model", - "@score_baselibs//score/json/internal/parser/vajson/vajson_impl", - "@score_baselibs//score/language/futurecpp", - "@score_baselibs//score/result", - ], -) - -cc_test( - name = "vajson_serialize_test", - srcs = ["vajson_serialize_test.cpp"], - features = COMPILER_WARNING_FEATURES + ["aborts_upon_exception"], - tags = ["unit"], - deps = [ - ":vajson_serialize", - "@googletest//:gtest_main", - ], -) diff --git a/score/json/vajson_json_writer.cpp b/score/json/vajson_json_writer.cpp index 32d10a96b0..62d43a03e8 100644 --- a/score/json/vajson_json_writer.cpp +++ b/score/json/vajson_json_writer.cpp @@ -12,7 +12,7 @@ ********************************************************************************/ #include "score/json/vajson_json_writer.h" #include "score/json/internal/model/error.h" -#include "score/json/internal/writer/vajson/vajson_serialize.h" +#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" namespace { template From a5961be970301540c2303b292a7485a4a0fcf0ed Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 26 Aug 2026 13:34:41 +0200 Subject: [PATCH 06/21] Revert "Move vajson serializer to vajson parser directory" This reverts commit ca5628f28307ac935a88a5748e1473c163188191. --- score/json/BUILD | 2 +- .../internal/parser/vajson/unit_test/BUILD | 1 - .../internal/parser/vajson/vajson_impl/BUILD | 37 ---------------- score/json/internal/writer/vajson/BUILD | 43 +++++++++++++++++++ .../vajson}/vajson_serialize.cpp | 2 +- .../vajson}/vajson_serialize.h | 6 +-- .../vajson}/vajson_serialize_test.cpp | 2 +- .../vajson}/writer/serializers.h | 4 +- .../vajson}/writer/serializers/stl.h | 6 +-- .../serializers/stl/associative_containers.h | 4 +- .../writer/serializers/stl/primitives.h | 2 +- .../serializers/stl/sequence_containers.h | 2 +- .../structures/generic_value_serializer.h | 14 +++--- .../generic_value_serializer_impl.h | 4 +- .../serializers/structures/key_serializer.h | 12 +++--- .../serializers/structures/serializer.h | 0 .../serializers/util/escaped_json_string.h | 4 +- .../serializers/util/length_serializer.h | 2 +- .../vajson}/writer/serializers/vac.h | 6 +-- .../writer/serializers/vac/primitives.h | 2 +- .../serializers/vac/sequence_containers.h | 2 +- .../vajson}/writer/serializers/vac/variant.h | 0 .../vajson}/writer/types/array_type.h | 4 +- .../vajson}/writer/types/basic_types.h | 2 +- .../vajson}/writer/types/bin_types.h | 0 .../vajson}/writer/types/object_type.h | 4 +- .../vajson}/writer/types/object_type_impl.h | 6 +-- score/json/vajson_json_writer.cpp | 2 +- 28 files changed, 90 insertions(+), 85 deletions(-) create mode 100644 score/json/internal/writer/vajson/BUILD rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/vajson_serialize.cpp (97%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/vajson_serialize.h (96%) rename score/json/internal/{parser/vajson/unit_test/writer => writer/vajson}/vajson_serialize_test.cpp (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers.h (90%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl.h (81%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/associative_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/primitives.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/stl/sequence_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/generic_value_serializer.h (95%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/generic_value_serializer_impl.h (92%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/key_serializer.h (91%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/structures/serializer.h (100%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/util/escaped_json_string.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/util/length_serializer.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac.h (81%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/primitives.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/sequence_containers.h (98%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/serializers/vac/variant.h (100%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/array_type.h (96%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/basic_types.h (99%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/bin_types.h (100%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/object_type.h (97%) rename score/json/internal/{parser/vajson/vajson_impl => writer/vajson}/writer/types/object_type_impl.h (87%) diff --git a/score/json/BUILD b/score/json/BUILD index f9cfb0e132..bc45fabe5a 100644 --- a/score/json/BUILD +++ b/score/json/BUILD @@ -128,7 +128,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":writer_interface", - "@score_baselibs//score/json/internal/parser/vajson/vajson_impl:vajson_serialize", + "@score_baselibs//score/json/internal/writer/vajson:vajson_serialize", ], ) diff --git a/score/json/internal/parser/vajson/unit_test/BUILD b/score/json/internal/parser/vajson/unit_test/BUILD index f0932a0ba2..945ee2e19a 100644 --- a/score/json/internal/parser/vajson/unit_test/BUILD +++ b/score/json/internal/parser/vajson/unit_test/BUILD @@ -25,7 +25,6 @@ cc_test( ], deps = [ "//score/json/internal/parser/vajson:vajson_parser", - "//score/json/internal/parser/vajson/vajson_impl:vajson_serialize", "@googletest//:gtest_main", ], ) diff --git a/score/json/internal/parser/vajson/vajson_impl/BUILD b/score/json/internal/parser/vajson/vajson_impl/BUILD index 65feff4be9..38cb940b9b 100644 --- a/score/json/internal/parser/vajson/vajson_impl/BUILD +++ b/score/json/internal/parser/vajson/vajson_impl/BUILD @@ -11,9 +11,6 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -load("@rules_cc//cc:defs.bzl", "cc_library") -load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") - package(default_visibility = ["@score_baselibs//score/json:__subpackages__"]) cc_library( @@ -55,26 +52,6 @@ cc_library( "util/json_error_domain.h", "util/number.h", "util/types.h", - "writer/serializers.h", - "writer/serializers/stl.h", - "writer/serializers/stl/associative_containers.h", - "writer/serializers/stl/primitives.h", - "writer/serializers/stl/sequence_containers.h", - "writer/serializers/structures/generic_value_serializer.h", - "writer/serializers/structures/generic_value_serializer_impl.h", - "writer/serializers/structures/key_serializer.h", - "writer/serializers/structures/serializer.h", - "writer/serializers/util/escaped_json_string.h", - "writer/serializers/util/length_serializer.h", - "writer/serializers/vac.h", - "writer/serializers/vac/primitives.h", - "writer/serializers/vac/sequence_containers.h", - "writer/serializers/vac/variant.h", - "writer/types/array_type.h", - "writer/types/basic_types.h", - "writer/types/bin_types.h", - "writer/types/object_type.h", - "writer/types/object_type_impl.h", ], includes = [ "include", @@ -87,17 +64,3 @@ cc_library( "@score_baselibs//score/result", ], ) - -cc_library( - name = "vajson_serialize", - srcs = ["vajson_serialize.cpp"], - hdrs = ["vajson_serialize.h"], - features = COMPILER_WARNING_FEATURES, - tags = ["FFI"], - deps = [ - ":vajson_impl", - "@score_baselibs//score/json/internal/model", - "@score_baselibs//score/language/futurecpp", - "@score_baselibs//score/result", - ], -) diff --git a/score/json/internal/writer/vajson/BUILD b/score/json/internal/writer/vajson/BUILD new file mode 100644 index 0000000000..273e6ec161 --- /dev/null +++ b/score/json/internal/writer/vajson/BUILD @@ -0,0 +1,43 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") +load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") + +cc_library( + name = "vajson_serialize", + srcs = ["vajson_serialize.cpp"], + hdrs = ["vajson_serialize.h"] + glob([ + "writer/**/*.h", + ]), + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["@score_baselibs//score/json:__subpackages__"], + deps = [ + "@score_baselibs//score/json/internal/model", + "@score_baselibs//score/json/internal/parser/vajson/vajson_impl", + "@score_baselibs//score/language/futurecpp", + "@score_baselibs//score/result", + ], +) + +cc_test( + name = "vajson_serialize_test", + srcs = ["vajson_serialize_test.cpp"], + features = COMPILER_WARNING_FEATURES + ["aborts_upon_exception"], + tags = ["unit"], + deps = [ + ":vajson_serialize", + "@googletest//:gtest_main", + ], +) diff --git a/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp similarity index 97% rename from score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp rename to score/json/internal/writer/vajson/vajson_serialize.cpp index f58d80aa15..2a1ac7c784 100644 --- a/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" +#include "score/json/internal/writer/vajson/vajson_serialize.h" #include #include diff --git a/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h rename to score/json/internal/writer/vajson/vajson_serialize.h index 92efddde0c..1427652c9c 100644 --- a/score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -14,8 +14,8 @@ #define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H #include "score/json/internal/model/any.h" #include "score/json/internal/model/error.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" #include "score/result/result.h" #include #include @@ -90,7 +90,7 @@ auto SerializeObject(amsr::json::GenericValueSerializer&& serializer, auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); next = SerializeValue(std::move(value_serializer), element.second); } - return next; + return std::move(next); }); } template diff --git a/score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp similarity index 96% rename from score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp rename to score/json/internal/writer/vajson/vajson_serialize_test.cpp index ed439d9a32..55f2423477 100644 --- a/score/json/internal/parser/vajson/unit_test/writer/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" +#include "score/json/internal/writer/vajson/vajson_serialize.h" #include namespace score diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h b/score/json/internal/writer/vajson/writer/serializers.h similarity index 90% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers.h rename to score/json/internal/writer/vajson/writer/serializers.h index cf0921c751..4791634e20 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers.h +++ b/score/json/internal/writer/vajson/writer/serializers.h @@ -27,7 +27,7 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h b/score/json/internal/writer/vajson/writer/serializers/stl.h similarity index 81% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h rename to score/json/internal/writer/vajson/writer/serializers/stl.h index 96873511cc..4a3fc62186 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/primitives.h" +#include "score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h rename to score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h index 90b44bb3bf..22f77d412e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/associative_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h @@ -28,8 +28,8 @@ #include #include #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h rename to score/json/internal/writer/vajson/writer/serializers/stl/primitives.h index d695919af2..666b82638e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h @@ -29,7 +29,7 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h rename to score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h index e46d683a18..4577fafc14 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/stl/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h @@ -28,7 +28,7 @@ #include #include #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h similarity index 95% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index ccb5c92c0f..8409fcce11 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -29,13 +29,13 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/bin_types.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h similarity index 92% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h rename to score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h index 050e61d236..ddead882ac 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer_impl.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h @@ -21,8 +21,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h similarity index 91% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h index de85b4000c..b58253c827 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h @@ -22,12 +22,12 @@ * INCLUDES *********************************************************************************************************************/ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" +#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h similarity index 100% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h rename to score/json/internal/writer/vajson/writer/serializers/structures/serializer.h diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h rename to score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 22c4abd082..9084393003 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -27,8 +27,8 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h rename to score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h index 89a0825eff..1cffc95445 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/util/length_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h @@ -24,7 +24,7 @@ #include #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h b/score/json/internal/writer/vajson/writer/serializers/vac.h similarity index 81% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h rename to score/json/internal/writer/vajson/writer/serializers/vac.h index 0cd758badc..c8a998571e 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac.h @@ -23,8 +23,8 @@ /********************************************************************************************************************** * INCLUDES *********************************************************************************************************************/ -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/primitives.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h" +#include "score/json/internal/writer/vajson/writer/serializers/vac/variant.h" #endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h rename to score/json/internal/writer/vajson/writer/serializers/vac/primitives.h index 12a5b007e3..d6d03f3446 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h @@ -29,7 +29,7 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h similarity index 98% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h rename to score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h index 21a98818ce..1f48c8b99b 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h @@ -26,7 +26,7 @@ *********************************************************************************************************************/ #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/array_type.h" #include "score/span.hpp" #include "score/vector.hpp" diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h b/score/json/internal/writer/vajson/writer/serializers/vac/variant.h similarity index 100% rename from score/json/internal/parser/vajson/vajson_impl/writer/serializers/vac/variant.h rename to score/json/internal/writer/vajson/writer/serializers/vac/variant.h diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h b/score/json/internal/writer/vajson/writer/types/array_type.h similarity index 96% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h rename to score/json/internal/writer/vajson/writer/types/array_type.h index 02ea1f785d..64573cf0ac 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/array_type.h +++ b/score/json/internal/writer/vajson/writer/types/array_type.h @@ -28,8 +28,8 @@ #include #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h similarity index 99% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h rename to score/json/internal/writer/vajson/writer/types/basic_types.h index 7570da5a9c..fb065335f0 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -167,7 +167,7 @@ constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s * requires true; * \endspec */ -inline auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } +constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } inline namespace literals { /*! diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h b/score/json/internal/writer/vajson/writer/types/bin_types.h similarity index 100% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/bin_types.h rename to score/json/internal/writer/vajson/writer/types/bin_types.h diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h similarity index 97% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h rename to score/json/internal/writer/vajson/writer/types/object_type.h index 8dde53c111..967acdd6eb 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -28,8 +28,8 @@ #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/basic_types.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/basic_types.h" namespace amsr { namespace json { diff --git a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h b/score/json/internal/writer/vajson/writer/types/object_type_impl.h similarity index 87% rename from score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h rename to score/json/internal/writer/vajson/writer/types/object_type_impl.h index 57778b1f7e..a287103dae 100644 --- a/score/json/internal/parser/vajson/vajson_impl/writer/types/object_type_impl.h +++ b/score/json/internal/writer/vajson/writer/types/object_type_impl.h @@ -23,9 +23,9 @@ *********************************************************************************************************************/ #include -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/generic_value_serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/serializers/structures/serializer.h" -#include "score/json/internal/parser/vajson/vajson_impl/writer/types/object_type.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" +#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace amsr { namespace json { diff --git a/score/json/vajson_json_writer.cpp b/score/json/vajson_json_writer.cpp index 62d43a03e8..32d10a96b0 100644 --- a/score/json/vajson_json_writer.cpp +++ b/score/json/vajson_json_writer.cpp @@ -12,7 +12,7 @@ ********************************************************************************/ #include "score/json/vajson_json_writer.h" #include "score/json/internal/model/error.h" -#include "score/json/internal/parser/vajson/vajson_impl/vajson_serialize.h" +#include "score/json/internal/writer/vajson/vajson_serialize.h" namespace { template From 21d0e52133727204907d41b73236cee958cf514f Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 26 Aug 2026 14:03:08 +0200 Subject: [PATCH 07/21] Add vajson as second serializer backend --- score/json/BUILD | 43 ++- score/json/README.md | 16 +- score/json/detailed_design/class_diagram.puml | 15 + score/json/internal/writer/BUILD | 38 +++ .../json/internal/writer/json_serialize/BUILD | 14 + .../json_serialize/json_serialize_backend.cpp | 262 ++++++++++++++++++ score/json/internal/writer/vajson/BUILD | 12 + .../internal/writer/vajson/vajson_backend.cpp | 72 +++++ .../internal/writer/vajson/vajson_serialize.h | 2 +- .../writer/vajson/writer/types/basic_types.h | 2 +- score/json/internal/writer/writer_backend.h | 61 ++++ score/json/json_writer.cpp | 194 +------------ score/json/json_writer_test.cpp | 51 +++- score/json/vajson_json_writer.cpp | 74 ----- score/json/vajson_json_writer.h | 57 ---- 15 files changed, 563 insertions(+), 350 deletions(-) create mode 100644 score/json/internal/writer/BUILD create mode 100644 score/json/internal/writer/json_serialize/json_serialize_backend.cpp create mode 100644 score/json/internal/writer/vajson/vajson_backend.cpp create mode 100644 score/json/internal/writer/writer_backend.h delete mode 100644 score/json/vajson_json_writer.cpp delete mode 100644 score/json/vajson_json_writer.h diff --git a/score/json/BUILD b/score/json/BUILD index bc45fabe5a..63d1b50d38 100644 --- a/score/json/BUILD +++ b/score/json/BUILD @@ -39,6 +39,27 @@ config_setting( visibility = ["@score_baselibs//score/json/internal/parser:__subpackages__"], ) +string_flag( + name = "writer_library", + build_setting_default = "json_serialize", + values = [ + "json_serialize", + "vajson", + ], +) + +config_setting( + name = "writer_library_json_serialize", + flag_values = {":writer_library": "json_serialize"}, + visibility = ["@score_baselibs//score/json/internal/writer:__subpackages__"], +) + +config_setting( + name = "writer_library_vajson", + flag_values = {":writer_library": "vajson"}, + visibility = ["@score_baselibs//score/json/internal/writer:__subpackages__"], +) + cc_library( name = "parser_interface", srcs = ["i_json_parser.cpp"], @@ -114,21 +135,8 @@ cc_library( visibility = ["@score_baselibs//score/json:__subpackages__"], deps = [ ":writer_interface", - "@score_baselibs//score/json/internal/writer/json_serialize", - "@score_baselibs//score/language/safecpp/safe_math", - ], -) - -cc_library( - name = "vajson_json_writer_impl", - srcs = ["vajson_json_writer.cpp"], - hdrs = ["vajson_json_writer.h"], - features = COMPILER_WARNING_FEATURES, - tags = ["FFI"], - visibility = ["//visibility:public"], - deps = [ - ":writer_interface", - "@score_baselibs//score/json/internal/writer/vajson:vajson_serialize", + "@score_baselibs//score/json/internal/writer", + "@score_baselibs//score/json/internal/writer:writer_backend", ], ) @@ -218,6 +226,10 @@ cc_test( "test_warnings", "aborts_upon_exception", ], + local_defines = select({ + ":writer_library_json_serialize": ["WRITER_JSON_SERIALIZE"], + ":writer_library_vajson": ["WRITER_VAJSON"], + }), tags = ["unit"], visibility = [ "@score_baselibs//score/json:__pkg__", @@ -250,6 +262,7 @@ cc_unit_test_suites_for_host_and_qnx( ":json_writer_unit_test", # workaround to include coverage for json_writer "@score_baselibs//score/json/internal/model:unit_test", "@score_baselibs//score/json/internal/writer/json_serialize:json_serialize_unit_test", # workaround to include coverage for json_serialize + "@score_baselibs//score/json/internal/writer/vajson:vajson_serialize_test", # workaround to include coverage for vajson_serialize ], test_suites_from_sub_packages = [ "@score_baselibs//score/json/internal/parser:unit_tests", diff --git a/score/json/README.md b/score/json/README.md index 4b05c323ca..55615bec52 100644 --- a/score/json/README.md +++ b/score/json/README.md @@ -15,10 +15,12 @@ - [Requirements](#requirements) - [Assumptions of Use](#assumptions-of-use) - [Selecting base library](#selecting-base-library) + - [Selecting writer library](#selecting-writer-library) This JSON library is designed as an abstraction layer which can switch to using other parsers/serializers under the hood. At the moment it uses vaJson from Vector for parsing, -which is ASIL D certified. For serialization this library uses a custom implementation. +which is ASIL D certified. For serialization a custom implementation is used by default, with vaJson +available as an alternative, [selectable via a feature flag](#selecting-writer-library). This library requires to be ASIL B certified, so it can be used in other ASIL B certified components. @@ -378,3 +380,15 @@ In order to make use of nlohmann json library, feature flag needs to be set. Ple bazel test --config=spp_host_clang //score/json/... --//platform/aas/lib/json:base_library="nlohmann" nlohmann json library do not supports hexadecimal. As it is not part of json standard. + +## Selecting writer library + +Independently of the parser, the serialization backend is selected by the `writer_library` flag. It accepts +`json_serialize` (the default, a custom implementation) and `vajson` (the vector json library). The parser flag +`base_library` has no influence on serialization. + +bazel test --config=spp_host_clang //score/json/... --//platform/aas/lib/json:writer_library="vajson" + +Mind that the two backends differ in the representation they emit: `json_serialize` pretty-prints with a four +space indentation, whereas `vajson` emits compact JSON without any insignificant whitespace between tokens. Both +produce valid, equivalent JSON, but consumers comparing serialized output byte-wise are affected by the choice. diff --git a/score/json/detailed_design/class_diagram.puml b/score/json/detailed_design/class_diagram.puml index 01b5c65022..9592addc77 100644 --- a/score/json/detailed_design/class_diagram.puml +++ b/score/json/detailed_design/class_diagram.puml @@ -76,6 +76,7 @@ class score::json::JsonWriter { score::json::JsonWriter --> score::json::Error : use score::json::JsonWriter --> score::json::JsonSerialize : use +score::json::JsonWriter --> score::json::VajsonSerialize : use interface score::json::IJsonParser { @@ -109,6 +110,20 @@ score::json::JsonSerialize --> score::json::Error : use score::json::JsonSerialize --> score::Result : use +class score::json::VajsonSerialize #LightSteelBlue { + +VajsonSerialize(out_stream: std::ostream) + +operator<<(json_data: score::json::Object): score::Result + +operator<<(json_data: score::json::List): score::Result + +operator<<(json_data: score::json::Any): score::Result +} + +score::json::VajsonSerialize --> score::json::Any : use +score::json::VajsonSerialize --> score::json::Object : use +score::json::VajsonSerialize --> score::json::List : use +score::json::VajsonSerialize --> score::json::Error : use +score::json::VajsonSerialize --> score::Result : use + + enum amsr::json::ParserState #LightSteelBlue diff --git a/score/json/internal/writer/BUILD b/score/json/internal/writer/BUILD new file mode 100644 index 0000000000..0942081923 --- /dev/null +++ b/score/json/internal/writer/BUILD @@ -0,0 +1,38 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +load("@rules_cc//cc:defs.bzl", "cc_library") +load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES") + +# Declarations of the serialization backend seam. Exactly one backend provides the definitions; which one is +# selected by the `//score/json:writer_library` flag below. +cc_library( + name = "writer_backend", + hdrs = ["writer_backend.h"], + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["@score_baselibs//score/json:__subpackages__"], + deps = [ + "@score_baselibs//score/json/internal/model", + "@score_baselibs//score/result", + ], +) + +alias( + name = "writer", + actual = select({ + "@score_baselibs//score/json:writer_library_json_serialize": "@score_baselibs//score/json/internal/writer/json_serialize:json_serialize_backend", + "@score_baselibs//score/json:writer_library_vajson": "@score_baselibs//score/json/internal/writer/vajson:vajson_backend", + }), + visibility = ["@score_baselibs//score/json:__pkg__"], +) diff --git a/score/json/internal/writer/json_serialize/BUILD b/score/json/internal/writer/json_serialize/BUILD index 310dda7963..57a0afe47b 100644 --- a/score/json/internal/writer/json_serialize/BUILD +++ b/score/json/internal/writer/json_serialize/BUILD @@ -31,6 +31,20 @@ cc_library( ], ) +cc_library( + name = "json_serialize_backend", + srcs = ["json_serialize_backend.cpp"], + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["@score_baselibs//score/json/internal/writer:__pkg__"], + deps = [ + ":json_serialize", + "@score_baselibs//score/json/internal/writer:writer_backend", + "@score_baselibs//score/language/futurecpp", + "@score_baselibs//score/language/safecpp/safe_math", + ], +) + cc_test( name = "json_serialize_unit_test", srcs = [ diff --git a/score/json/internal/writer/json_serialize/json_serialize_backend.cpp b/score/json/internal/writer/json_serialize/json_serialize_backend.cpp new file mode 100644 index 0000000000..7f34015f00 --- /dev/null +++ b/score/json/internal/writer/json_serialize/json_serialize_backend.cpp @@ -0,0 +1,262 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#include "score/json/internal/writer/writer_backend.h" + +#include "score/json/internal/writer/json_serialize/json_serialize.h" +#include "score/language/safecpp/safe_math/safe_math.h" + +#include +#include + +#include +#include +#include +#include +// std::locale provides functionalities (e.g. facets) to customize parts of iostream implementation. In this case it is +// used for serializing integers efficiently. An implementation without std::locale might require (massive) amounts of +// code duplication. Furthermore, this header is allowed to be used for character conversion purposes if and it must be +// ensured that libcatalog is not linked using the target toolchain, which is the case for libjson. +// Reference: broken_link_c/issue/4600528 +// NOLINTNEXTLINE(score-banned-include): see rationale above +#include +#include +#include + +namespace +{ + +template +// Coverity thinks this function is unused, but it is used in for calculating kIntBufLen. +// coverity[autosar_cpp14_a0_1_3_violation] +constexpr std::size_t max_dec_digits() noexcept +{ + U v = std::numeric_limits::max(); + std::size_t n = 1U; + while (v >= 10U) + { + v /= 10U; + // There is no known type that could represent enough digits to overflow std::size_t + // coverity[autosar_cpp14_a4_7_1_violation] + ++n; + } + return n; +} + +template +inline constexpr std::size_t kIntBufLen = max_dec_digits>>() + 1U; + +// Rationale: noexcept safe magnitude conversion; uses safe_math assertions; +// COVERITY: autosar_cpp14_a15_5_3_violation, uncaught_exception +// policy requires terminate on failure; no exceptions thrown. +template +// coverity[autosar_cpp14_a15_5_3_violation] +// coverity[uncaught_exception] +inline auto abs_magnitude_unsigned(T val) noexcept +{ + using U = std::make_unsigned_t>; + static_assert(std::is_integral_v, "integral only"); + static_assert(std::is_integral_v, "U must be integral"); + static_assert(std::numeric_limits::digits >= std::numeric_limits>::digits, + "U must represent full magnitude of T"); + + // Coverity doesn't know constexpr if statements + // coverity[autosar_cpp14_a7_1_8_violation] + if constexpr (std::is_signed_v) + { + const auto abs_val = score::safe_math::Abs(val); + const auto cast_res = score::safe_math::Cast(abs_val); + SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(cast_res.has_value(), + "Safe cast failed in abs_magnitude_unsigned (signed)"); + return cast_res.value(); + } + else + { + const auto cast_res = score::safe_math::Cast(val); + SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(cast_res.has_value(), + "Safe cast failed in abs_magnitude_unsigned (unsigned)"); + return cast_res.value(); + } +} + +template +[[nodiscard]] std::string_view integer_to_chars(std::array>& buffer, const T val) noexcept +{ + using U = std::make_unsigned_t>; + static_assert(std::is_integral_v && std::is_integral_v, "integral only"); + static_assert(!std::is_same_v, bool>, "bool not supported"); + static_assert(std::numeric_limits::digits >= std::numeric_limits>::digits, + "U must represent full magnitude of T"); + + const bool is_negative = (std::is_signed_v && (val < static_cast(0))); + U x = abs_magnitude_unsigned(val); + + // no range checks on it because the function declaration ensures enough buffer space for the given type + auto it = buffer.end(); + do + { + // Rationale: Converting unsigned digit (x % 10U) to signed int8_t is safe because + // the modulo operation guarantees the value is in range [0, 9], which fits in both + // uint8_t and int8_t. The signed type is needed for consistent char arithmetic with '0'. + // coverity[autosar_cpp14_m5_0_9_violation] + const auto digit = static_cast(x % static_cast(10U)); + it = std::prev(it); + // coverity[autosar_cpp14_m5_0_9_violation] + *it = static_cast(static_cast('0') + digit); + x /= 10U; + } while (x > static_cast(0U)); + if (is_negative) + { + it = std::prev(it); + *it = '-'; + } + + return std::string_view(&*it, static_cast(std::distance(it, buffer.end()))); +} + +// This specialization of std::num_put ignores parameter widths (see std::setw) as this feature is neither useful +// nor used for serializing JSON. +class OptimizedNumPut : public std::num_put +{ + public: + // Coverity thinks this function is unused, wheras it is used for std::locale + // coverity[autosar_cpp14_a0_1_3_violation] + using std::num_put::num_put; + + protected: + using std::num_put::do_put; + + // Coverity thinks this function is unused, wheras it is used for std::locale + // coverity[autosar_cpp14_a0_1_3_violation] + iter_type do_put(iter_type out, std::ios_base& s, char_type fill, long v) const override + { + return OptimizedPutForInts(out, s, fill, v); + } + // Coverity thinks this function is unused, wheras it is used for std::locale + // coverity[autosar_cpp14_a0_1_3_violation] + iter_type do_put(iter_type out, std::ios_base& s, char_type fill, unsigned long v) const override + { + return OptimizedPutForInts(out, s, fill, v); + } + // Coverity thinks this function is unused, wheras it is used for std::locale + // coverity[autosar_cpp14_a0_1_3_violation] + // LCOV_EXCL_START see SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE + iter_type do_put(iter_type out, std::ios_base& s, char_type fill, long long v) const override + { + SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( + "This code is unreachable with tested toolchains and target platforms"); + return OptimizedPutForInts(out, s, fill, v); + } + // LCOV_EXCL_STOP + // Coverity thinks this function is unused, wheras it is used for std::locale + // coverity[autosar_cpp14_a0_1_3_violation] + // LCOV_EXCL_START see SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE + iter_type do_put(iter_type out, std::ios_base& s, char_type fill, unsigned long long v) const override + { + SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( + "This code is unreachable with tested toolchains and target platforms"); + return OptimizedPutForInts(out, s, fill, v); + } + // LCOV_EXCL_STOP + + private: + template + iter_type OptimizedPutForInts(iter_type out, std::ios_base&, char_type, T val) const + { + std::array> buf{}; + const auto sv = integer_to_chars(buf, val); + return std::copy(sv.begin(), sv.end(), out); + } +}; + +template +score::Result SerializeToStreamInternal(std::ostream& out_stream, const T& json_data) +{ + score::json::JsonSerialize serializer{out_stream}; + return serializer << json_data; +} + +template +score::Result SerializeToBufferInternal(const T& json_data) +{ + // This line must be hit when the function is called. Since other parts of this function show line coverage, + // this line must also be hit. Missing coverage is due to a bug in the coverage tool + std::ostringstream string_stream{}; // LCOV_EXCL_LINE + + // NOLINTBEGIN(score-no-dynamic-raw-memory) See rationale below + // Rationale: std::num_put is reference counted and std::locale does manage it. + // Explanation for Coverity Suppression for AUTOSAR A3-3-2: Static locale with custom facet requires runtime + // initialization. std::locale constructor with facet cannot be constexpr. Thread-safe due to function-local static + // initialization guarantee (C++11 §6.7 [stmt.dcl]/4). coverity[autosar_cpp14_a3_3_2_violation] + const static std::locale loc(std::locale(), new OptimizedNumPut()); + // NOLINTEND(score-no-dynamic-raw-memory) See rationale above + + score::cpp::ignore = string_stream.imbue(loc); + + score::json::JsonSerialize serializer{string_stream}; + auto result = serializer << json_data; + // LCOV_EXCL_BR_START Error path currently not reachable + if (!result.has_value()) + // LCOV_EXCL_BR_STOP + { + return score::Unexpected{result.error()}; + } + + return string_stream.str(); +} + +} // namespace + +namespace score +{ +namespace json +{ +namespace internal +{ +namespace writer +{ + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Object& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::List& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Any& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToBuffer(const score::json::Object& json_data) +{ + return SerializeToBufferInternal(json_data); +} + +score::Result SerializeToBuffer(const score::json::List& json_data) +{ + return SerializeToBufferInternal(json_data); +} + +score::Result SerializeToBuffer(const score::json::Any& json_data) +{ + return SerializeToBufferInternal(json_data); +} + +} // namespace writer +} // namespace internal +} // namespace json +} // namespace score diff --git a/score/json/internal/writer/vajson/BUILD b/score/json/internal/writer/vajson/BUILD index 273e6ec161..1d5ecdbb6b 100644 --- a/score/json/internal/writer/vajson/BUILD +++ b/score/json/internal/writer/vajson/BUILD @@ -31,6 +31,18 @@ cc_library( ], ) +cc_library( + name = "vajson_backend", + srcs = ["vajson_backend.cpp"], + features = COMPILER_WARNING_FEATURES, + tags = ["FFI"], + visibility = ["@score_baselibs//score/json/internal/writer:__pkg__"], + deps = [ + ":vajson_serialize", + "@score_baselibs//score/json/internal/writer:writer_backend", + ], +) + cc_test( name = "vajson_serialize_test", srcs = ["vajson_serialize_test.cpp"], diff --git a/score/json/internal/writer/vajson/vajson_backend.cpp b/score/json/internal/writer/vajson/vajson_backend.cpp new file mode 100644 index 0000000000..9ee08ed255 --- /dev/null +++ b/score/json/internal/writer/vajson/vajson_backend.cpp @@ -0,0 +1,72 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#include "score/json/internal/writer/writer_backend.h" + +#include "score/json/internal/writer/vajson/vajson_serialize.h" + +namespace +{ + +template +score::Result SerializeToStreamInternal(std::ostream& out_stream, const T& json_data) +{ + score::json::VajsonSerialize serializer{out_stream}; + return serializer << json_data; +} + +} // namespace + +namespace score +{ +namespace json +{ +namespace internal +{ +namespace writer +{ + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Object& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::List& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Any& json_data) +{ + return SerializeToStreamInternal(out_stream, json_data); +} + +score::Result SerializeToBuffer(const score::json::Object& json_data) +{ + return score::json::VajsonToBuffer(json_data); +} + +score::Result SerializeToBuffer(const score::json::List& json_data) +{ + return score::json::VajsonToBuffer(json_data); +} + +score::Result SerializeToBuffer(const score::json::Any& json_data) +{ + return score::json::VajsonToBuffer(json_data); +} + +} // namespace writer +} // namespace internal +} // namespace json +} // namespace score diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h index 1427652c9c..599c8959de 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -90,7 +90,7 @@ auto SerializeObject(amsr::json::GenericValueSerializer&& serializer, auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); next = SerializeValue(std::move(value_serializer), element.second); } - return std::move(next); + return next; }); } template diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h index fb065335f0..7570da5a9c 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -167,7 +167,7 @@ constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s * requires true; * \endspec */ -constexpr auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } +inline auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } inline namespace literals { /*! diff --git a/score/json/internal/writer/writer_backend.h b/score/json/internal/writer/writer_backend.h new file mode 100644 index 0000000000..b7ce10ebc1 --- /dev/null +++ b/score/json/internal/writer/writer_backend.h @@ -0,0 +1,61 @@ +/******************************************************************************** + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_WRITER_BACKEND_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_WRITER_BACKEND_H + +#include "score/json/internal/model/any.h" +#include "score/result/result.h" + +#include +#include + +namespace score +{ +namespace json +{ +namespace internal +{ +namespace writer +{ + +/// \brief Backend seam for JSON serialization. +/// +/// Exactly one backend provides definitions for the declarations below. Which one is linked is decided at build +/// time by the `//score/json:writer_library` flag, resolved through the `//score/json/internal/writer:writer` +/// alias -- the same mechanism the parser uses via `//score/json:base_library`. +/// +/// Note that the emitted representation is backend specific: `json_serialize` pretty-prints with a four space +/// indentation, whereas `vajson` emits compact JSON without insignificant whitespace. + +/// \brief Serializes json_data into out_stream +/// \param out_stream The stream to write the serialized representation to +/// \param json_data The data to serialize +/// \return empty result on success, error otherwise +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Object& json_data); +score::Result SerializeToStream(std::ostream& out_stream, const score::json::List& json_data); +score::Result SerializeToStream(std::ostream& out_stream, const score::json::Any& json_data); + +/// \brief Serializes json_data into a string +/// \param json_data The data to serialize +/// \return the serialized representation on success, error otherwise +score::Result SerializeToBuffer(const score::json::Object& json_data); +score::Result SerializeToBuffer(const score::json::List& json_data); +score::Result SerializeToBuffer(const score::json::Any& json_data); + +} // namespace writer +} // namespace internal +} // namespace json +} // namespace score + +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_WRITER_BACKEND_H diff --git a/score/json/json_writer.cpp b/score/json/json_writer.cpp index 25543fd54f..1e2580153c 100644 --- a/score/json/json_writer.cpp +++ b/score/json/json_writer.cpp @@ -14,25 +14,11 @@ #include "score/json/json_writer.h" #include "score/json/i_json_writer.h" #include "score/json/internal/model/error.h" -#include "score/json/internal/writer/json_serialize/json_serialize.h" -#include "score/language/safecpp/safe_math/safe_math.h" +#include "score/json/internal/writer/writer_backend.h" -#include - -#include -#include -#include -#include -// std::locale provides functionalities (e.g. facets) to customize parts of iostream implementation. In this case it is -// used for serializing integers efficiently. An implementation without std::locale might require (massive) amounts of -// code duplication. Furthermore, this header is allowed to be used for character conversion purposes if and it must be -// ensured that libcatalog is not linked using the target toolchain, which is the case for libjson. -// Reference: broken_link_c/issue/4600528 -// NOLINTNEXTLINE(score-banned-include): see rationale above -#include -#include +#include +#include #include -#include namespace { @@ -50,153 +36,9 @@ score::Result ToFileInternal(const T& json_data, return score::Result{score::unexpect, error}; } - score::json::JsonSerialize serializer{**file}; - return serializer << json_data; -} - -template -// Coverity thinks this function is unused, but it is used in for calculating kIntBufLen. -// coverity[autosar_cpp14_a0_1_3_violation] -constexpr std::size_t max_dec_digits() noexcept -{ - U v = std::numeric_limits::max(); - std::size_t n = 1U; - while (v >= 10U) - { - v /= 10U; - // There is no known type that could represent enough digits to overflow std::size_t - // coverity[autosar_cpp14_a4_7_1_violation] - ++n; - } - return n; -} - -template -inline constexpr std::size_t kIntBufLen = max_dec_digits>>() + 1U; - -// Rationale: noexcept safe magnitude conversion; uses safe_math assertions; -// COVERITY: autosar_cpp14_a15_5_3_violation, uncaught_exception -// policy requires terminate on failure; no exceptions thrown. -template -// coverity[autosar_cpp14_a15_5_3_violation] -// coverity[uncaught_exception] -inline auto abs_magnitude_unsigned(T val) noexcept -{ - using U = std::make_unsigned_t>; - static_assert(std::is_integral_v, "integral only"); - static_assert(std::is_integral_v, "U must be integral"); - static_assert(std::numeric_limits::digits >= std::numeric_limits>::digits, - "U must represent full magnitude of T"); - - // Coverity doesn't know constexpr if statements - // coverity[autosar_cpp14_a7_1_8_violation] - if constexpr (std::is_signed_v) - { - const auto abs_val = score::safe_math::Abs(val); - const auto cast_res = score::safe_math::Cast(abs_val); - SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(cast_res.has_value(), - "Safe cast failed in abs_magnitude_unsigned (signed)"); - return cast_res.value(); - } - else - { - const auto cast_res = score::safe_math::Cast(val); - SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(cast_res.has_value(), - "Safe cast failed in abs_magnitude_unsigned (unsigned)"); - return cast_res.value(); - } -} - -template -[[nodiscard]] std::string_view integer_to_chars(std::array>& buffer, const T val) noexcept -{ - using U = std::make_unsigned_t>; - static_assert(std::is_integral_v && std::is_integral_v, "integral only"); - static_assert(!std::is_same_v, bool>, "bool not supported"); - static_assert(std::numeric_limits::digits >= std::numeric_limits>::digits, - "U must represent full magnitude of T"); - - const bool is_negative = (std::is_signed_v && (val < static_cast(0))); - U x = abs_magnitude_unsigned(val); - - // no range checks on it because the function declaration ensures enough buffer space for the given type - auto it = buffer.end(); - do - { - // Rationale: Converting unsigned digit (x % 10U) to signed int8_t is safe because - // the modulo operation guarantees the value is in range [0, 9], which fits in both - // uint8_t and int8_t. The signed type is needed for consistent char arithmetic with '0'. - // coverity[autosar_cpp14_m5_0_9_violation] - const auto digit = static_cast(x % static_cast(10U)); - it = std::prev(it); - // coverity[autosar_cpp14_m5_0_9_violation] - *it = static_cast(static_cast('0') + digit); - x /= 10U; - } while (x > static_cast(0U)); - if (is_negative) - { - it = std::prev(it); - *it = '-'; - } - - return std::string_view(&*it, static_cast(std::distance(it, buffer.end()))); + return score::json::internal::writer::SerializeToStream(**file, json_data); } -// This specialization of std::num_put ignores parameter widths (see std::setw) as this feature is neither useful -// nor used for serializing JSON. -class OptimizedNumPut : public std::num_put -{ - public: - // Coverity thinks this function is unused, wheras it is used for std::locale - // coverity[autosar_cpp14_a0_1_3_violation] - using std::num_put::num_put; - - protected: - using std::num_put::do_put; - - // Coverity thinks this function is unused, wheras it is used for std::locale - // coverity[autosar_cpp14_a0_1_3_violation] - iter_type do_put(iter_type out, std::ios_base& s, char_type fill, long v) const override - { - return OptimizedPutForInts(out, s, fill, v); - } - // Coverity thinks this function is unused, wheras it is used for std::locale - // coverity[autosar_cpp14_a0_1_3_violation] - iter_type do_put(iter_type out, std::ios_base& s, char_type fill, unsigned long v) const override - { - return OptimizedPutForInts(out, s, fill, v); - } - // Coverity thinks this function is unused, wheras it is used for std::locale - // coverity[autosar_cpp14_a0_1_3_violation] - // LCOV_EXCL_START see SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE - iter_type do_put(iter_type out, std::ios_base& s, char_type fill, long long v) const override - { - SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( - "This code is unreachable with tested toolchains and target platforms"); - return OptimizedPutForInts(out, s, fill, v); - } - // LCOV_EXCL_STOP - // Coverity thinks this function is unused, wheras it is used for std::locale - // coverity[autosar_cpp14_a0_1_3_violation] - // LCOV_EXCL_START see SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE - iter_type do_put(iter_type out, std::ios_base& s, char_type fill, unsigned long long v) const override - { - SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( - "This code is unreachable with tested toolchains and target platforms"); - return OptimizedPutForInts(out, s, fill, v); - } - // LCOV_EXCL_STOP - - private: - template - iter_type OptimizedPutForInts(iter_type out, std::ios_base&, char_type, T val) const - { - std::array> buf{}; - const auto sv = integer_to_chars(buf, val); - return std::copy(sv.begin(), sv.end(), out); - } -}; - template score::Result ToFileInternalAtomic(const T& json_data, const std::string_view& file_path, @@ -209,8 +51,7 @@ score::Result ToFileInternalAtomic(const T& json_data, return score::json::MakeError(score::json::Error::kInvalidFilePath, err.UserMessage()); }) .and_then([&json_data](auto filestream) -> score::Result { - score::json::JsonSerialize serializer{*filestream}; - auto serializer_result = serializer << json_data; + auto serializer_result = score::json::internal::writer::SerializeToStream(*filestream, json_data); return filestream->Close().and_then([serializer_result](auto&&...) noexcept { return serializer_result; }); @@ -220,30 +61,7 @@ score::Result ToFileInternalAtomic(const T& json_data, template score::Result ToBufferInternal(const T& json_data) { - // This line must be hit when the function is called. Since other parts of this function show line coverage, - // this line must also be hit. Missing coverage is due to a bug in the coverage tool - std::ostringstream string_stream{}; // LCOV_EXCL_LINE - - // NOLINTBEGIN(score-no-dynamic-raw-memory) See rationale below - // Rationale: std::num_put is reference counted and std::locale does manage it. - // Explanation for Coverity Suppression for AUTOSAR A3-3-2: Static locale with custom facet requires runtime - // initialization. std::locale constructor with facet cannot be constexpr. Thread-safe due to function-local static - // initialization guarantee (C++11 §6.7 [stmt.dcl]/4). coverity[autosar_cpp14_a3_3_2_violation] - const static std::locale loc(std::locale(), new OptimizedNumPut()); - // NOLINTEND(score-no-dynamic-raw-memory) See rationale above - - score::cpp::ignore = string_stream.imbue(loc); - - score::json::JsonSerialize serializer{string_stream}; - auto result = serializer << json_data; - // LCOV_EXCL_BR_START Error path currently not reachable - if (!result.has_value()) - // LCOV_EXCL_BR_STOP - { - return score::Unexpected{result.error()}; - } - - return string_stream.str(); + return score::json::internal::writer::SerializeToBuffer(json_data); } } // namespace diff --git a/score/json/json_writer_test.cpp b/score/json/json_writer_test.cpp index bedc5da246..93dd0e34be 100644 --- a/score/json/json_writer_test.cpp +++ b/score/json/json_writer_test.cpp @@ -35,6 +35,15 @@ using ::testing::ByMove; using ::testing::Return; using ::testing::StrEq; +// The selected serialization backend decides the emitted representation: json_serialize pretty-prints with a four +// space indentation, whereas vajson emits compact JSON without insignificant whitespace. Which backend is linked is +// chosen by the //score/json:writer_library flag and communicated here via local_defines. +#if defined(WRITER_VAJSON) +constexpr auto kKeySeparator = "\":"; +#else +constexpr auto kKeySeparator = "\": "; +#endif + class TestJsonList : public json::List { public: @@ -47,6 +56,9 @@ class TestJsonList : public json::List emplace_back(std::move(obj)); } +#if defined(WRITER_VAJSON) + static constexpr auto expected = R"([1234,"string",{"key":"value"}])"; +#else static constexpr auto expected = R"([ 1234, "string", @@ -54,6 +66,7 @@ class TestJsonList : public json::List "key": "value" } ])"; +#endif }; class TestJsonObject : public json::Object @@ -65,10 +78,14 @@ class TestJsonObject : public json::Object emplace("num", score::json::Any{1}); } +#if defined(WRITER_VAJSON) + static constexpr auto expected = R"({"num":1,"string":"foo"})"; +#else static constexpr auto expected = R"({ "num": 1, "string": "foo" })"; +#endif }; class TestJsonAny : public json::Any @@ -232,8 +249,8 @@ class JsonWriterIntegerTest : public ::testing::Test // The Number variant currently supports std::int{8,16,32,64}_t and std::uint{8,16,32,64}_t. // Types such as long long and unsigned long long can only be tested on platforms where the above typedefs resolve to // them (usually <64-bit platforms). -// This test suite covers all integral types not causing a -Wsign-promo error exercised by our num_put overrides and -// accepted by the production JSON implementation. +// This test suite covers all integral types not causing a -Wsign-promo error accepted by the production JSON +// implementation. using IntegralTypes = ::testing::Types; TYPED_TEST_SUITE(JsonWriterIntegerTest, IntegralTypes, ); @@ -276,26 +293,34 @@ TYPED_TEST(JsonWriterIntegerTest, FormatsIntegralValuesCorrectly) const std::string json_str = *result; // Verify all values are formatted correctly - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"zero\": "} + std::to_string(static_cast(0)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"positive\": "} + std::to_string(static_cast(12345)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"zero"} + kKeySeparator + std::to_string(static_cast(0)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"positive"} + kKeySeparator + std::to_string(static_cast(12345)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"p9\": "} + std::to_string(static_cast(9)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"p10\": "} + std::to_string(static_cast(10)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"p11\": "} + std::to_string(static_cast(11)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"p9"} + kKeySeparator + std::to_string(static_cast(9)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"p10"} + kKeySeparator + std::to_string(static_cast(10)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"p11"} + kKeySeparator + std::to_string(static_cast(11)))); if constexpr (std::numeric_limits::is_signed) { EXPECT_NE(std::string::npos, - json_str.find(std::string{"\"negative\": "} + std::to_string(static_cast(-12345)))); + json_str.find(std::string{"\"negative"} + kKeySeparator + std::to_string(static_cast(-12345)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"min"} + kKeySeparator + std::to_string(std::numeric_limits::min()))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"m9"} + kKeySeparator + std::to_string(static_cast(-9)))); + EXPECT_NE(std::string::npos, + json_str.find(std::string{"\"m10"} + kKeySeparator + std::to_string(static_cast(-10)))); EXPECT_NE(std::string::npos, - json_str.find(std::string{"\"min\": "} + std::to_string(std::numeric_limits::min()))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"m9\": "} + std::to_string(static_cast(-9)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"m10\": "} + std::to_string(static_cast(-10)))); - EXPECT_NE(std::string::npos, json_str.find(std::string{"\"m11\": "} + std::to_string(static_cast(-11)))); + json_str.find(std::string{"\"m11"} + kKeySeparator + std::to_string(static_cast(-11)))); } EXPECT_NE(std::string::npos, - json_str.find(std::string{"\"max\": "} + std::to_string(std::numeric_limits::max()))); + json_str.find(std::string{"\"max"} + kKeySeparator + std::to_string(std::numeric_limits::max()))); } } // namespace diff --git a/score/json/vajson_json_writer.cpp b/score/json/vajson_json_writer.cpp deleted file mode 100644 index 32d10a96b0..0000000000 --- a/score/json/vajson_json_writer.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -#include "score/json/vajson_json_writer.h" -#include "score/json/internal/model/error.h" -#include "score/json/internal/writer/vajson/vajson_serialize.h" -namespace -{ -template -score::Result ToFileInternal(const T& json_data, - const std::string_view& file_path, - score::filesystem::IFileFactory& file_factory) -{ - const std::string file_path_string{file_path.data(), file_path.size()}; - const auto file = file_factory.Open(file_path_string, std::ios::out | std::ios::trunc); - if (!file.has_value()) - { - return score::Result{score::unexpect, - score::json::MakeError(score::json::Error::kInvalidFilePath, "Failed to open file")}; - } - score::json::VajsonSerialize serializer{**file}; - return serializer << json_data; -} -template -score::Result ToBufferInternal(const T& json_data) -{ - return score::json::VajsonToBuffer(json_data); -} -} // namespace -namespace score -{ -namespace json -{ -score::Result VajsonJsonWriter::ToFile(const score::json::Object& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) -{ - return ToFileInternal(json_data, file_path, *file_factory); -} -score::Result VajsonJsonWriter::ToFile(const score::json::List& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) -{ - return ToFileInternal(json_data, file_path, *file_factory); -} -score::Result VajsonJsonWriter::ToFile(const score::json::Any& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) -{ - return ToFileInternal(json_data, file_path, *file_factory); -} -score::Result VajsonJsonWriter::ToBuffer(const score::json::Object& json_data) -{ - return ToBufferInternal(json_data); -} -score::Result VajsonJsonWriter::ToBuffer(const score::json::List& json_data) -{ - return ToBufferInternal(json_data); -} -score::Result VajsonJsonWriter::ToBuffer(const score::json::Any& json_data) -{ - return ToBufferInternal(json_data); -} -} // namespace json -} // namespace score diff --git a/score/json/vajson_json_writer.h b/score/json/vajson_json_writer.h deleted file mode 100644 index b8d17919dd..0000000000 --- a/score/json/vajson_json_writer.h +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ - -#ifndef SCORE_LIB_JSON_VAJSON_JSON_WRITER_H -#define SCORE_LIB_JSON_VAJSON_JSON_WRITER_H - -#include "score/json/i_json_writer.h" - -#include - -namespace score -{ -namespace json -{ - -/// @brief Implementation of IJsonWriter using the vaJSON serializer (VajsonSerialize). -class VajsonJsonWriter final : public IJsonWriter -{ - public: - VajsonJsonWriter() noexcept = default; - VajsonJsonWriter(const VajsonJsonWriter&) = delete; - VajsonJsonWriter(VajsonJsonWriter&&) noexcept = delete; - VajsonJsonWriter& operator=(const VajsonJsonWriter&) = delete; - VajsonJsonWriter& operator=(VajsonJsonWriter&&) noexcept = delete; - ~VajsonJsonWriter() noexcept override = default; - - score::Result ToFile(const score::json::Object& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) override; - - score::Result ToFile(const score::json::List& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) override; - - score::Result ToFile(const score::json::Any& json_data, - const std::string_view& file_path, - std::shared_ptr file_factory) override; - - score::Result ToBuffer(const score::json::Object& json_data) override; - score::Result ToBuffer(const score::json::List& json_data) override; - score::Result ToBuffer(const score::json::Any& json_data) override; -}; - -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_VAJSON_JSON_WRITER_H From 755faa95d8a1232a5aa0bb5cd589d3305128816a Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 26 Aug 2026 14:42:44 +0200 Subject: [PATCH 08/21] Remove amsr namespace; Remove VCA annotations --- score/json/README.md | 2 +- score/json/detailed_design/class_diagram.puml | 20 +- .../vajson/vajson_impl/reader/json_data.h | 2 +- .../vajson_impl/util/json_error_domain.h | 11 - .../parser/vajson/vajson_impl/util/number.h | 4 +- .../parser/vajson/vajson_impl/util/types.h | 19 - .../writer/vajson/vajson_serialize.cpp | 8 +- .../internal/writer/vajson/vajson_serialize.h | 103 +-- .../writer/vajson/vajson_serialize_test.cpp | 3 +- .../writer/vajson/writer/serializers.h | 22 +- .../writer/vajson/writer/serializers/stl.h | 18 +- .../serializers/stl/associative_containers.h | 364 +++------ .../writer/serializers/stl/primitives.h | 218 ++---- .../serializers/stl/sequence_containers.h | 154 ++-- .../structures/generic_value_serializer.h | 621 ++++++---------- .../generic_value_serializer_impl.h | 72 +- .../serializers/structures/key_serializer.h | 294 +++----- .../serializers/structures/serializer.h | 155 ++-- .../serializers/util/escaped_json_string.h | 248 +++---- .../serializers/util/length_serializer.h | 79 +- .../writer/vajson/writer/serializers/vac.h | 18 +- .../writer/serializers/vac/primitives.h | 133 +--- .../serializers/vac/sequence_containers.h | 146 ++-- .../vajson/writer/serializers/vac/variant.h | 144 ++-- .../writer/vajson/writer/types/array_type.h | 209 ++---- .../writer/vajson/writer/types/basic_types.h | 690 ++++++------------ .../writer/vajson/writer/types/bin_types.h | 435 ++++------- .../writer/vajson/writer/types/object_type.h | 263 +++---- .../vajson/writer/types/object_type_impl.h | 56 +- 29 files changed, 1495 insertions(+), 3016 deletions(-) diff --git a/score/json/README.md b/score/json/README.md index 55615bec52..887fb923f1 100644 --- a/score/json/README.md +++ b/score/json/README.md @@ -340,7 +340,7 @@ since they would require high amount of work. Please be aware and note: For the most up to date information on Requirements please follow the provided links to CodeBeamer. Adaptive Platform SW Safety Requirements -- [The users of the vaJson ibrary SHALL guarantee the integrity of the data used to initialize the `amsr::json::JsonData` buffer.](broken_link_c/issue/6576406) +- [The users of the vaJson Library SHALL guarantee the integrity of the data used to initialize the `score::json::vajson::JsonData` buffer.](broken_link_c/issue/6576406) [SW Component Requirements](broken_link_c/tracker/566325?workingSetId=-1&layout_name=document&subtreeRoot=5310849) - The JSON-Library shall support at least the functional set of RFC-8259. diff --git a/score/json/detailed_design/class_diagram.puml b/score/json/detailed_design/class_diagram.puml index 9592addc77..ed72f22e7b 100644 --- a/score/json/detailed_design/class_diagram.puml +++ b/score/json/detailed_design/class_diagram.puml @@ -124,21 +124,21 @@ score::json::VajsonSerialize --> score::json::Error : use score::json::VajsonSerialize --> score::Result : use -enum amsr::json::ParserState #LightSteelBlue +enum score::json::vajson::ParserState #LightSteelBlue -enum amsr::json::JsonErrc #LightSteelBlue +enum score::json::vajson::JsonErrc #LightSteelBlue -class amsr::json::ParserResult <> #LightSteelBlue +class score::json::vajson::ParserResult <> #LightSteelBlue -class amsr::json::v2::Parser #LightSteelBlue +class score::json::vajson::v2::Parser #LightSteelBlue -class amsr::json::JsonData #LightSteelBlue +class score::json::vajson::JsonData #LightSteelBlue -amsr::json::v2::Parser <|-- score::json::VajsonParser +score::json::vajson::v2::Parser <|-- score::json::VajsonParser class score::json::VajsonParser #LightSteelBlue { @@ -149,10 +149,10 @@ class score::json::VajsonParser #LightSteelBlue { score::json::VajsonParser *-- score::json::Any score::json::VajsonParser --> score::json::Error : use score::json::VajsonParser --> score::Result : use -score::json::VajsonParser --> amsr::json::ParserResult : use -score::json::VajsonParser --> amsr::json::ParserState : use -score::json::VajsonParser --> amsr::json::JsonErrc : use -score::json::VajsonParser --> amsr::json::JsonData : use +score::json::VajsonParser --> score::json::vajson::ParserResult : use +score::json::VajsonParser --> score::json::vajson::ParserState : use +score::json::VajsonParser --> score::json::vajson::JsonErrc : use +score::json::VajsonParser --> score::json::vajson::JsonData : use class score::json::NlohmannParser #LightSteelBlue { diff --git a/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h b/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h index ca486f5745..275cf5157f 100644 --- a/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h +++ b/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h @@ -65,7 +65,7 @@ class JsonData final public: /// \brief Class JsonOps must have access to the InputStream but no other classes - friend class internal::JsonOps; // VECTOR SL AutosarC++17_10-A11.3.1: MD_JSON_AutosarC++17_10-A11.3.1_json_ops + friend class internal::JsonOps; /// \brief Initializes a JSON data object using a constructed reader /// \param[in] input_stream diff --git a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h index 19e9355e83..95c35b60a8 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h @@ -281,7 +281,6 @@ inline auto MakeResult(bool value, score::result::Error error) noexcept -> score template inline auto MakeResult(Optional value, score::result::Error error) noexcept -> Result { - // VCA_VAJSON_EXTERNAL_CALL return value.has_value() ? Result{value.value()} : score::MakeUnexpected(error); } @@ -311,14 +310,4 @@ inline void AssertCondition(bool value, CStr message = "") noexcept } // namespace json } // namespace score -namespace amsr -{ -namespace json -{ - -using score::json::vajson::AssertCondition; - -} // namespace json -} // namespace amsr - #endif // SCORE_LIB_JSON_INTERNAL_PARSER_VAJSON_JSON_UTIL_JSON_ERROR_DOMAIN_H_ diff --git a/score/json/internal/parser/vajson/vajson_impl/util/number.h b/score/json/internal/parser/vajson/vajson_impl/util/number.h index c4262b55e6..f322752ea9 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/number.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/number.h @@ -58,11 +58,11 @@ using IsInt = std::conjunction>, std::is_int /// \brief Typedef for longlong /// \details Return type of std::strtoll and transformed to fixed-size type later. -using SignedLL = long long int; // VECTOR SL AutosarC++17_10-A3.9.1: MD_JSON_base_type +using SignedLL = long long int; /// \brief Typedef for unsigned longlong /// \details Return type of std::strtoull and transformed to fixed-size type later. -using UnsignedLL = unsigned long long int; // VECTOR SL AutosarC++17_10-A3.9.1: MD_JSON_base_type +using UnsignedLL = unsigned long long int; namespace util { diff --git a/score/json/internal/parser/vajson/vajson_impl/util/types.h b/score/json/internal/parser/vajson/vajson_impl/util/types.h index 157643af70..2d831ca3c0 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/types.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/types.h @@ -65,23 +65,4 @@ enum class EncodingType : std::uint8_t } // namespace json } // namespace score -namespace amsr -{ -namespace json -{ - -template -using Optional = score::json::vajson::Optional; - -using CStringView = score::json::vajson::CStringView; -using StringView = score::json::vajson::StringView; -using String = score::json::vajson::String; -using Bytes = score::json::vajson::Bytes; -using EncodingType = score::json::vajson::EncodingType; - -using score::json::vajson::operator""sv; - -} // namespace json -} // namespace amsr - #endif // SCORE_LIB_JSON_INTERNAL_PARSER_VAJSON_JSON_UTIL_TYPES_H_ diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp index 2a1ac7c784..e3b786a2bb 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -20,13 +20,13 @@ namespace template score::Result SerializeToStream(std::ostream& out_stream, const T& json_data) { - auto serializer = amsr::json::DocumentSerializer{std::ref(out_stream)}; + auto serializer = score::json::vajson::DocumentSerializer{std::ref(out_stream)}; static_cast(std::move(serializer) << json_data); if (out_stream.fail()) { - return score::Result{score::unexpect, - score::json::MakeError(score::json::Error::kUnknownError, - "vaJSON serializer failed to write to stream")}; + return score::Result{ + score::unexpect, + score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")}; } return {}; } diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h index 599c8959de..0ed14e9887 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -36,66 +36,73 @@ namespace vajson class ObjectKeySerializer final { public: - auto operator()(const score::memory::StringComparisonAdaptor& key) const noexcept -> amsr::json::JKeyType + auto operator()(const score::memory::StringComparisonAdaptor& key) const noexcept -> score::json::vajson::JKeyType { - return amsr::json::JKey(key.GetAsStringView()); + return score::json::vajson::JKey(key.GetAsStringView()); } }; template -auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, - const score::json::Any& value) noexcept -> typename amsr::json::GenericValueSerializer::Next; +auto SerializeValue(score::json::vajson::GenericValueSerializer&& serializer, + const score::json::Any& value) noexcept -> + typename score::json::vajson::GenericValueSerializer::Next; template -auto SerializeNumber(amsr::json::GenericValueSerializer&& serializer, - const score::json::Number& value) noexcept -> typename amsr::json::GenericValueSerializer::Next +auto SerializeNumber(score::json::vajson::GenericValueSerializer&& serializer, + const score::json::Number& value) noexcept -> + typename score::json::vajson::GenericValueSerializer::Next { if (const auto unsigned_value = value.As(); unsigned_value.has_value()) { - return std::move(serializer) << amsr::json::JNumber(*unsigned_value); + return std::move(serializer) << score::json::vajson::JNumber(*unsigned_value); } if (const auto signed_value = value.As(); signed_value.has_value()) { - return std::move(serializer) << amsr::json::JNumber(*signed_value); + return std::move(serializer) << score::json::vajson::JNumber(*signed_value); } if (const auto float_value = value.As(); float_value.has_value()) { - return std::move(serializer) << amsr::json::JNumber(*float_value); + return std::move(serializer) << score::json::vajson::JNumber(*float_value); } if (const auto double_value = value.As(); double_value.has_value()) { - return std::move(serializer) << amsr::json::JNumber(*double_value); + return std::move(serializer) << score::json::vajson::JNumber(*double_value); } SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(false, "Unable to serialize score::json::Number with vaJSON adapter."); - return std::move(serializer) << amsr::json::JNull(); + return std::move(serializer) << score::json::vajson::JNull(); } template -auto SerializeList(amsr::json::GenericValueSerializer&& serializer, - const score::json::List& list) noexcept -> typename amsr::json::GenericValueSerializer::Next -{ - return std::move(serializer) << amsr::json::JArray([&list](amsr::json::ArrayStart array_serializer) noexcept { - auto next = std::move(array_serializer); - for (const auto& value : list) - { - next = SerializeValue(std::move(next), value); - } - }); +auto SerializeList(score::json::vajson::GenericValueSerializer&& serializer, + const score::json::List& list) noexcept -> + typename score::json::vajson::GenericValueSerializer::Next +{ + return std::move(serializer) << score::json::vajson::JArray( + [&list](score::json::vajson::ArrayStart array_serializer) noexcept { + auto next = std::move(array_serializer); + for (const auto& value : list) + { + next = SerializeValue(std::move(next), value); + } + }); } template -auto SerializeObject(amsr::json::GenericValueSerializer&& serializer, - const score::json::Object& object) noexcept -> typename amsr::json::GenericValueSerializer::Next -{ - return std::move(serializer) << amsr::json::JObject([&object](amsr::json::ObjectStart object_serializer) noexcept { - auto next = std::move(object_serializer); - for (const auto& element : object) - { - auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); - next = SerializeValue(std::move(value_serializer), element.second); - } - return next; - }); +auto SerializeObject(score::json::vajson::GenericValueSerializer&& serializer, + const score::json::Object& object) noexcept -> + typename score::json::vajson::GenericValueSerializer::Next +{ + return std::move(serializer) << score::json::vajson::JObject( + [&object](score::json::vajson::ObjectStart object_serializer) noexcept { + auto next = std::move(object_serializer); + for (const auto& element : object) + { + auto value_serializer = std::move(next) << ObjectKeySerializer{}(element.first); + next = SerializeValue(std::move(value_serializer), element.second); + } + return next; + }); } template -auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, - const score::json::Any& value) noexcept -> typename amsr::json::GenericValueSerializer::Next +auto SerializeValue(score::json::vajson::GenericValueSerializer&& serializer, + const score::json::Any& value) noexcept -> + typename score::json::vajson::GenericValueSerializer::Next { if (const auto object = value.As(); object.has_value()) { @@ -107,12 +114,12 @@ auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, } if (const auto string_value = value.As(); string_value.has_value()) { - return std::move(serializer) << amsr::json::JString(string_value->get()); + return std::move(serializer) << score::json::vajson::JString(string_value->get()); } if (const auto null_value = value.As(); null_value.has_value()) { score::cpp::ignore = null_value; - return std::move(serializer) << amsr::json::JNull(); + return std::move(serializer) << score::json::vajson::JNull(); } if (const auto number = value.As(); number.has_value()) { @@ -121,7 +128,7 @@ auto SerializeValue(amsr::json::GenericValueSerializer&& serializer, const auto boolean = value.As(); SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(boolean.has_value(), "Unable to determine score::json::Any type for vaJSON serialization."); - return std::move(serializer) << amsr::json::JBool(*boolean); + return std::move(serializer) << score::json::vajson::JBool(*boolean); } } // namespace vajson } // namespace writer @@ -138,6 +145,7 @@ class VajsonSerialize final score::Result operator<<(const score::json::Object& json_data); score::Result operator<<(const score::json::List& json_data); score::Result operator<<(const score::json::Any& json_data); + private: std::ostream& out_stream_; }; @@ -146,28 +154,31 @@ score::Result VajsonToBuffer(const score::json::List& json_data); score::Result VajsonToBuffer(const score::json::Any& json_data); } // namespace json } // namespace score -namespace amsr +namespace score { namespace json { +namespace vajson +{ template -auto operator<<(GenericValueSerializer&& serializer, - const score::json::Object& value) noexcept -> typename GenericValueSerializer::Next +auto operator<<(GenericValueSerializer&& serializer, const score::json::Object& value) noexcept -> + typename GenericValueSerializer::Next { return score::json::internal::writer::vajson::SerializeObject(std::move(serializer), value); } template -auto operator<<(GenericValueSerializer&& serializer, - const score::json::List& value) noexcept -> typename GenericValueSerializer::Next +auto operator<<(GenericValueSerializer&& serializer, const score::json::List& value) noexcept -> + typename GenericValueSerializer::Next { return score::json::internal::writer::vajson::SerializeList(std::move(serializer), value); } template -auto operator<<(GenericValueSerializer&& serializer, - const score::json::Any& value) noexcept -> typename GenericValueSerializer::Next +auto operator<<(GenericValueSerializer&& serializer, const score::json::Any& value) noexcept -> + typename GenericValueSerializer::Next { return score::json::internal::writer::vajson::SerializeValue(std::move(serializer), value); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index 55f2423477..de705cd849 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -33,7 +33,8 @@ TEST(VajsonSerializeTest, SerializesNestedAnyToCompactJson) const auto result = VajsonToBuffer(Any{std::move(root)}); ASSERT_TRUE(result.has_value()); EXPECT_EQ(*result, - std::string{"{\"boolean\":true,\"list\":[null,{\"number\":7}],\"string\":\"line1\\n\\\"quoted\\\"\\\\line2\"}"}); + std::string{ + "{\"boolean\":true,\"list\":[null,{\"number\":7}],\"string\":\"line1\\n\\\"quoted\\\"\\\\line2\"}"}); } TEST(VajsonSerializeTest, SerializesObjectKeysUsingStringComparisonAdaptor) { diff --git a/score/json/internal/writer/vajson/writer/serializers.h b/score/json/internal/writer/vajson/writer/serializers.h index 4791634e20..aa8b6b8676 100644 --- a/score/json/internal/writer/vajson/writer/serializers.h +++ b/score/json/internal/writer/vajson/writer/serializers.h @@ -10,24 +10,14 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of all available serializers. - * - * \details Provides serializers for native JSON, libVac & STL/STD types. - * \unit Serializer - * \complexity The sum metrics of unit is high. It includes multiple template classes, which could be put into - * separate units. But the classes are closely related, and splitting the unit up into smaller pieces - * would not make it simpler. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of all available serializers. +/// \details Provides serializers for native JSON, libVac & STL/STD types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include "score/json/internal/writer/vajson/writer/serializers/stl.h" #include "score/json/internal/writer/vajson/writer/serializers/vac.h" -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl.h b/score/json/internal/writer/vajson/writer/serializers/stl.h index 4a3fc62186..aa3124071e 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl.h @@ -10,21 +10,15 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for native STD & STL types. - * - * \details Single include for all STD &STL types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for native STD & STL types. +/// \details Single include for all STD &STL types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include "score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h" #include "score/json/internal/writer/vajson/writer/serializers/stl/primitives.h" #include "score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h" -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h index 22f77d412e..45a43675f6 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h @@ -10,294 +10,146 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for associative STL containers. - * - * \details Provides serializers for (unordered) set, (unordered) map, and (unordered) multimap types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for associative STL containers. +/// \details Provides serializers for (unordered) set, (unordered) map, and (unordered) multimap types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ +#include "score/json/internal/writer/vajson/writer/types/array_type.h" +#include "score/json/internal/writer/vajson/writer/types/object_type.h" #include #include #include #include #include -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" -namespace amsr { -namespace json { +namespace score +{ +namespace json +{ +namespace vajson +{ -/*! - * \brief Serializes a set of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Value - * Type of value. - * \tparam Cmp - * Type of comparison function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] set - * Set to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a set of serializable elements +/// \tparam Next type of serializer. +/// \tparam Value Type of value. +/// \tparam Cmp Type of comparison function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] set Set to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::set const& set) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JArray(set); +auto operator<<(GenericValueSerializer&& serializer, const std::set& set) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JArray(set); } -/*! - * \brief Serializes an unordered set of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Value - * Type of value. - * \tparam Hash - * Type of hash function. - * \tparam Pred - * Type of predicate function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] set - * Unordered set to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an unordered set of serializable elements +/// \tparam Next type of serializer. +/// \tparam Value Type of value. +/// \tparam Hash Type of hash function. +/// \tparam Pred Type of predicate function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] set Unordered set to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, - std::unordered_set const& set) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JArray(set); + const std::unordered_set& set) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JArray(set); } -/*! - * \brief Serializes a map of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Key - * Type of key. Must be convertible to a JKey. - * \tparam Value - * Type of value. - * \tparam Cmp - * Type of comparison function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] map - * Map to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a map of serializable elements +/// \tparam Next type of serializer. +/// \tparam Key Type of key. Must be convertible to a JKey. +/// \tparam Value Type of value. +/// \tparam Cmp Type of comparison function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] map Map to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::map const& map) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JObject(map); +auto operator<<(GenericValueSerializer&& serializer, const std::map& map) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JObject(map); } -/*! - * \brief Serializes a map of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Key - * Type of key. Must be convertible to a JKey. - * \tparam Value - * Type of value. - * \tparam Cmp - * Type of comparison function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] map - * Map to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a map of serializable elements +/// \tparam Next type of serializer. +/// \tparam Key Type of key. Must be convertible to a JKey. +/// \tparam Value Type of value. +/// \tparam Cmp Type of comparison function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] map Map to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::map const& map) noexcept - -> typename GenericValueSerializer::Next { - return std::move(serializer) << JObject(map); +auto operator<<(GenericValueSerializer&& serializer, const std::map& map) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JObject(map); } -/*! - * \brief Serializes an unordered map of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Key - * Type of key. Must be convertible to a JKey. - * \tparam Value - * Type of value. - * \tparam Hash - * Type of hash function. - * \tparam Pred - * Type of predicate function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] map - * Unordered map to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an unordered map of serializable elements +/// \tparam Next type of serializer. +/// \tparam Key Type of key. Must be convertible to a JKey. +/// \tparam Value Type of value. +/// \tparam Hash Type of hash function. +/// \tparam Pred Type of predicate function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] map Unordered map to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, - std::unordered_map const& map) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JObject(map); + const std::unordered_map& map) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JObject(map); } -/*! - * \brief Serializes a multimap of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Key - * Type of key. Must be convertible to a JKey. - * \tparam Value - * Type of value. - * \tparam Cmp - * Type of comparison function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] map - * Multimap to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a multimap of serializable elements +/// \tparam Next type of serializer. +/// \tparam Key Type of key. Must be convertible to a JKey. +/// \tparam Value Type of value. +/// \tparam Cmp Type of comparison function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] map Multimap to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::multimap const& map) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JObject(map); +auto operator<<(GenericValueSerializer&& serializer, const std::multimap& map) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JObject(map); } -/*! - * \brief Serializes an unordered multimap of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Key - * Type of key. Must be convertible to a JKey. - * \tparam Value - * Type of value. - * \tparam Hash - * Type of hash function. - * \tparam Pred - * Type of predicate function. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] map - * Unordered multimap to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an unordered multimap of serializable elements +/// \tparam Next type of serializer. +/// \tparam Key Type of key. Must be convertible to a JKey. +/// \tparam Value Type of value. +/// \tparam Hash Type of hash function. +/// \tparam Pred Type of predicate function. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] map Unordered multimap to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, - std::unordered_multimap const& map) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JObject(map); + const std::unordered_multimap& map) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JObject(map); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h index 666b82638e..eefbd13644 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h @@ -10,19 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for primitive data types. - * - * \details Provides serializers for pointer, bool, number, and string types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for primitive data types. +/// \details Provides serializers for pointer, bool, number, and string types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -31,170 +25,84 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace amsr { -namespace json { -/*! - * \brief Forward declaration for the GenericValueSerializer - * \vpublic - * \tparam Return - * type after using operator<<(). - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief Forward declaration for the GenericValueSerializer +/// \tparam Return type after using operator<<(). template class GenericValueSerializer; -/*! - * \brief Serializes a null value directly from a nullptr literal - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a null value directly from a nullptr literal +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, std::nullptr_t) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JNull(); + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JNull(); } -/*! - * \brief Serializes a value directly from a pointer - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] ptr - * The pointer whose value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * - * \spec - * requires true; - * \endspec - * - * \internal - * - If the given pointer is a nullptr: - * - Serialize it as a null type. - * - Otherwise: - * - Serialize the pointer's value. - * \endinternal - */ +/// \brief Serializes a value directly from a pointer +/// \details +/// - If the given pointer is a nullptr: +/// - Serialize it as a null type. +/// - Otherwise: +/// - Serialize the pointer's value. +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] ptr The pointer whose value to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, T const* ptr) noexcept -> - typename GenericValueSerializer::Next { - // VCA_VAJSON_EXTERNAL_CALL - return (ptr == nullptr) ? (std::move(serializer) << JNull()) : (std::move(serializer) << *ptr); +auto operator<<(GenericValueSerializer&& serializer, const T* ptr) noexcept -> + typename GenericValueSerializer::Next +{ + return (ptr == nullptr) ? (std::move(serializer) << JNull()) : (std::move(serializer) << *ptr); } -/*! - * \brief Serializes a bool value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] b - * Bool value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a bool value directly +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] b Bool value to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, bool b) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JBool(b); + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JBool(b); } -/*! - * \brief Serializes a number value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam N - * Type of number. - * \param[in] serializer - * instance to write into. - * \param[in] number - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a number value directly +/// \tparam Next type of serializer. +/// \tparam N Type of number. +/// \param[in] serializer instance to write into. +/// \param[in] number to serialize. +/// \return The succeeding serializer. template ::value>> auto operator<<(GenericValueSerializer&& serializer, N number) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JNumber(number); + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JNumber(number); } -/*! - * \brief Serializes a string value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] string - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a string value directly +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] string to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::string const& string) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JString(string); +auto operator<<(GenericValueSerializer&& serializer, const std::string& string) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JString(string); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h index 4577fafc14..24da3056f5 100644 --- a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h @@ -10,129 +10,71 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for STD sequence containers. - * - * \details Provides serializers for std::array, std::vector, and std::deque types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for STD sequence containers. +/// \details Provides serializers for std::array, std::vector, and std::deque types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ +#include "score/json/internal/writer/vajson/writer/types/array_type.h" #include #include #include #include #include -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -namespace amsr { -namespace json { +namespace score +{ +namespace json +{ +namespace vajson +{ -/*! - * \brief Serializes an array of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Value - * Type of value. - * \tparam N - * Size of the array. - * \param[in] serializer - * instance to write into. - * \param[in] array - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an array of serializable elements +/// \tparam Next type of serializer. +/// \tparam Value Type of value. +/// \tparam N Size of the array. +/// \param[in] serializer instance to write into. +/// \param[in] array to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::array const& array) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JArray(array); +auto operator<<(GenericValueSerializer&& serializer, const std::array& array) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JArray(array); } -/*! - * \brief Serializes a vector of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Value - * Type of value. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] vector - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a vector of serializable elements +/// \tparam Next type of serializer. +/// \tparam Value Type of value. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] vector to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::vector const& vector) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JArray(vector); +auto operator<<(GenericValueSerializer&& serializer, const std::vector& vector) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JArray(vector); } -/*! - * \brief Serializes a deque of serializable elements - * \vpublic - * - * \tparam Next - * type of serializer. - * \tparam Value - * Type of value. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] deque - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a deque of serializable elements +/// \tparam Next type of serializer. +/// \tparam Value Type of value. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] deque to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::deque const& deque) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JArray(deque); +auto operator<<(GenericValueSerializer&& serializer, const std::deque& deque) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JArray(deque); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 8409fcce11..389b91014f 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -10,21 +10,15 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Serializer for generic JSON value types. - * - * \details Provides serializers for Null, Bool, Number, String, Array, and Object types. - * - *********************************************************************************************************************/ +/// \file +/// \brief Serializer for generic JSON value types. +/// \details Provides serializers for Null, Bool, Number, String, Array, and Object types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ -#include #include +#include #include #include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" @@ -37,422 +31,223 @@ #include "score/json/internal/writer/vajson/writer/types/bin_types.h" #include "score/json/internal/writer/vajson/writer/types/object_type.h" -namespace amsr { -namespace json { -/*! - * \brief A serializer for JSON value types - * \vpublic - * - * \tparam Return - * Type of the return value of a << operation. Must be one of the following types: - * - Unit: Serializer has no follow-up state (outermost element). - * - Self or GenericValueSerializer: Next element is another value (e.g. inside arrays). - * - KeySerializer: Next element is a key. - * - * \trace DSGN-JSON-Writer-Serialization - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief A serializer for JSON value types +/// \tparam Return Type of the return value of a << operation. Must be one of the following types: - Unit: Serializer +/// has no follow-up state (outermost element). - Self or GenericValueSerializer: Next element is another value +/// (e.g. inside arrays). - KeySerializer: Next element is a key. template -class GenericValueSerializer final { - public: - /*! - * \brief Type of the return value - * - * \details Set the type of the return value to be either its own type GenericValueSerializer (for arrays or a - * specified type) or the type specified by Return. - * \vpublic - */ - using Next = typename std::conditional_t::value, GenericValueSerializer, Return>; - - /*! - * \brief Constructs a GenericValueSerializer from an output stream - * \details Do not create an instance of GenericValueSerializer directly, use the vpublic aliases in - * amsr/json/writer/serializer.h - * \vpublic - * - * \param[in] os - * Output stream to write into. - * \param[in] state - * of the Serializer. - * \param[in] bom - * The BOM type to write. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - explicit GenericValueSerializer(WriterType os, SerializerState state = SerializerState::kEmpty, - EncodingType bom = EncodingType::kNone) noexcept - : os_(os), serializer_state_{state} { - this->WriteBom(bom); - } - - /*! - * \brief Default move constructor - * - * \vpublic - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - GenericValueSerializer(GenericValueSerializer&&) noexcept = default; - - /*! - * \brief Default move assignment - * \vpublic - * - * \return A reference to the moved into object. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - auto operator=(GenericValueSerializer&&) & noexcept -> GenericValueSerializer& = default; - - // Deleted copy constructor/assignment operator. - GenericValueSerializer(GenericValueSerializer const&) = delete; - auto operator=(GenericValueSerializer const&) -> GenericValueSerializer& = delete; - - /*! - * \brief Default DTOR - * \vpublic - */ - ~GenericValueSerializer() noexcept = default; - - /*! - * \brief Serializes a null value - * \vpublic - * - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JNullType) && noexcept -> Next { - return this->Serialize([this]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - constexpr auto null_str = "null"sv; - this->os_.get().write(null_str.data(), null_str.size()); - }); - } - - /*! - * \brief Serializes a boolean value - * \vpublic - * - * \param[in] b - * Boolean value to serialize. - * \return The succeeding serializer. - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JBoolType b) && noexcept -> Next { - const std::string_view value{b.value ? "true" : "false"}; - // NOLINTNEXTLINE(whitespace/line_length) - // VECTOR NL AutosarC++17_10-M8.5.1: MD_JSON_AutosarC++17_10-M8.5.1_use_of_possibly_unintialized_value_false_positive - return this->Serialize([this, value]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().write(value.data(), static_cast(value.size())); - }); - } - - /*! - * \brief Serializes a number value - * \vpublic - * - * \tparam T - * Type of number. - * \param[in] number - * value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - template - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JNumberType number) && noexcept -> Next { - return this->Serialize([this, number]() noexcept { - // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety - std::array buffer{}; - T value = static_cast(number.GetValue()); - - const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); +class GenericValueSerializer final +{ + public: + /// \brief Type of the return value + /// \details Set the type of the return value to be either its own type GenericValueSerializer (for arrays or a + /// specified type) or the type specified by Return. + using Next = typename std::conditional_t::value, GenericValueSerializer, Return>; + + /// \brief Constructs a GenericValueSerializer from an output stream + /// \details Do not create an instance of GenericValueSerializer directly, use the aliases in + /// score/json/internal/writer/vajson/writer/serializers/structures/serializer.h + /// \param[in] os Output stream to write into. + /// \param[in] state of the Serializer. + /// \param[in] bom The BOM type to write. + explicit GenericValueSerializer(WriterType os, + SerializerState state = SerializerState::kEmpty, + EncodingType bom = EncodingType::kNone) noexcept + : os_(os), serializer_state_{state} + { + this->WriteBom(bom); + } - AssertCondition(conversion_result.ec == std::errc{}, - "GenericValueSerializer: Could not convert number to textual representation."); + /// \brief Default move constructor + GenericValueSerializer(GenericValueSerializer&&) noexcept = default; + + /// \brief Default move assignment + /// \return A reference to the moved into object. + auto operator=(GenericValueSerializer&&) & noexcept -> GenericValueSerializer& = default; + + // Deleted copy constructor/assignment operator. + GenericValueSerializer(const GenericValueSerializer&) = delete; + auto operator=(const GenericValueSerializer&) -> GenericValueSerializer& = delete; + + /// \brief Default DTOR + ~GenericValueSerializer() noexcept = default; + + /// \brief Serializes a null value + /// \return The succeeding serializer. + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JNullType) && noexcept -> Next + { + return this->Serialize([this]() noexcept { + constexpr auto null_str = "null"sv; + this->os_.get().write(null_str.data(), null_str.size()); + }); + } - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().write(buffer.data(), static_cast(conversion_result.ptr - buffer.data())); - }); - } + /// \brief Serializes a boolean value + /// \param[in] b Boolean value to serialize. + /// \return The succeeding serializer. + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JBoolType b) && noexcept -> Next + { + const std::string_view value{b.value ? "true" : "false"}; + // NOLINTNEXTLINE(whitespace/line_length) + // coverity[autosar_cpp14_m8_5_1_violation] + return this->Serialize([this, value]() noexcept { + this->os_.get().write(value.data(), static_cast(value.size())); + }); + } - /*! - * \brief Serializes a string value - * \vpublic - * - * \param[in] string - * value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * - * \internal - * - Add quotes to the begin and end of the string. - * - Serialize the escaped string as a JSON string value. - * \endinternal - */ - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JStringType string) && noexcept -> Next { - return this->Serialize([this, string]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('"'); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get() << internal::EscapedJsonString(string); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('"'); - }); - } + /// \brief Serializes a number value + /// \tparam T Type of number. + /// \param[in] number value to serialize. + /// \return The succeeding serializer. + template + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JNumberType number) && noexcept -> Next + { + return this->Serialize([this, number]() noexcept { + // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety + std::array buffer{}; + T value = static_cast(number.GetValue()); + + const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); + + AssertCondition(conversion_result.ec == std::errc{}, + "GenericValueSerializer: Could not convert number to textual representation."); + + this->os_.get().write(buffer.data(), static_cast(conversion_result.ptr - buffer.data())); + }); + } - /*! - * \brief Serializes a binary string value - * \vpublic - * - * \param[in] string - * value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * - * \internal - * - Add a 's' to denote the following value as a string. - * - Serialize the length of the string value as four bytes big endian. - * - Write the string value. - * \endinternal - */ - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JBinStringType string) && noexcept -> Next { - return this->Serialize([this, string]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('s'); - // VCA_VAJSON_OUTPUTSTREAM - internal::SerializeLength(this->os_.get(), string.GetLength()); - // VCA_VAJSON_OUTPUTSTREAM - auto const& value = string.GetValue(); - this->os_.get().write(value.data(), static_cast(value.size())); - }); - } + /// \brief Serializes a string value + /// \details + /// - Add quotes to the begin and end of the string. + /// - Serialize the escaped string as a JSON string value. + /// \param[in] string value to serialize. + /// \return The succeeding serializer. + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JStringType string) && noexcept -> Next + { + return this->Serialize([this, string]() noexcept { + this->os_.get().put('"'); + this->os_.get() << internal::EscapedJsonString(string); + this->os_.get().put('"'); + }); + } - /*! - * \brief Serializes a series of serializable values - * \vpublic - * - * \tparam Fn - * Type of serializer function. - * \param[in] tuple - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre The function contained in the argument does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * \internal - * - Add an opening square bracket. - * - Serialize every value as a JSON value. - * - Add a closing square bracket. - * \endinternal - */ - template - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JArrayType tuple) && noexcept -> Next { - return this->Serialize([this, tuple]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('['); - static_cast(tuple.fn(ArrayStart(this->os_.get()))); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put(']'); - }); - } + /// \brief Serializes a binary string value + /// \details + /// - Add a 's' to denote the following value as a string. + /// - Serialize the length of the string value as four bytes big endian. + /// - Write the string value. + /// \param[in] string value to serialize. + /// \return The succeeding serializer. + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JBinStringType string) && noexcept -> Next + { + return this->Serialize([this, string]() noexcept { + this->os_.get().put('s'); + internal::SerializeLength(this->os_.get(), string.GetLength()); + const auto& value = string.GetValue(); + this->os_.get().write(value.data(), static_cast(value.size())); + }); + } - /*! - * \brief Serializes a binary value - * \vpublic - * - * \param[in] bin - * Value to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * - * \internal - * - Add a 'b' to denote the following value as binary. - * - Serialize the length of the binary value as four bytes big endian. - * - Write the binary value. - * \endinternal - */ - // VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const - auto operator<<(JBinType bin) && noexcept -> Next { - return this->Serialize([this, bin]() noexcept { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('b'); - // VCA_VAJSON_OUTPUTSTREAM - internal::SerializeLength(this->os_.get(), bin.GetLength()); - // VCA_VAJSON_OUTPUTSTREAM - auto const& value = bin.GetValue(); - this->os_.get().write(value.data(), static_cast(value.size())); - }); - } + /// \brief Serializes a series of serializable values + /// \details + /// - Add an opening square bracket. + /// - Serialize every value as a JSON value. + /// - Add a closing square bracket. + /// \tparam Fn Type of serializer function. + /// \param[in] tuple to serialize. + /// \return The succeeding serializer. + /// \pre The function contained in the argument does not throw any exceptions + template + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JArrayType tuple) && noexcept -> Next + { + return this->Serialize([this, tuple]() noexcept { + this->os_.get().put('['); + static_cast(tuple.fn(ArrayStart(this->os_.get()))); + this->os_.get().put(']'); + }); + } - /*! - * \brief Serializes an object - * \vpublic - * - * \tparam Fn - * Type of serializer function. - * \param[in] object - * to serialize. - * \return The succeeding serializer. - * \context ANY - * \pre The function contained in the argument does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - template - auto operator<<(JObjectType object) && noexcept -> Next; + /// \brief Serializes a binary value + /// \details + /// - Add a 'b' to denote the following value as binary. + /// - Serialize the length of the binary value as four bytes big endian. + /// - Write the binary value. + /// \param[in] bin Value to serialize. + /// \return The succeeding serializer. + // coverity[autosar_cpp14_m9_3_3_violation] + auto operator<<(JBinType bin) && noexcept -> Next + { + return this->Serialize([this, bin]() noexcept { + this->os_.get().put('b'); + internal::SerializeLength(this->os_.get(), bin.GetLength()); + const auto& value = bin.GetValue(); + this->os_.get().write(value.data(), static_cast(value.size())); + }); + } - private: - /*! - * \brief Serializes a value - * \tparam Fn - * Type of function. - * \param[in] fn - * Serializer call function. - * \return The succeeding serializer. - * - * \context ANY - * \pre The passed function does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * - * \internal - * - If another element was serialized before: - * - Add a comma. - * - Execute the given serializer function. - * \endinternal - */ - template - auto Serialize(Fn&& fn) const noexcept -> Next { - if (this->serializer_state_ == SerializerState::kNonEmpty) { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put(','); + /// \brief Serializes an object + /// \tparam Fn Type of serializer function. + /// \param[in] object to serialize. + /// \return The succeeding serializer. + /// \pre The function contained in the argument does not throw any exceptions + template + auto operator<<(JObjectType object) && noexcept -> Next; + + private: + /// \brief Serializes a value + /// \details + /// - If another element was serialized before: + /// - Add a comma. + /// - Execute the given serializer function. + /// \tparam Fn Type of function. + /// \param[in] fn Serializer call function. + /// \return The succeeding serializer. + /// \pre The passed function does not throw any exceptions + template + auto Serialize(Fn&& fn) const noexcept -> Next + { + if (this->serializer_state_ == SerializerState::kNonEmpty) + { + this->os_.get().put(','); + } + std::forward(fn)(); + return Next(this->os_.get(), SerializerState::kNonEmpty); } - std::forward(fn)(); - return Next(this->os_.get(), SerializerState::kNonEmpty); - } - /*! - * \brief Writes the requested BOM type - * \param[in] type - * The BOM type to write. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - * - * \internal - * - Write the requested BOM to the stream. - * \endinternal - */ - void WriteBom(EncodingType type) const noexcept { - if (type == EncodingType::kUtf8) { - constexpr std::string_view kUtf8Bom{"\xEF\xBB\xBF"sv}; - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().write(kUtf8Bom.data(), kUtf8Bom.size()); + /// \brief Writes the requested BOM type + /// \details + /// - Write the requested BOM to the stream. + /// \param[in] type The BOM type to write. + void WriteBom(EncodingType type) const noexcept + { + if (type == EncodingType::kUtf8) + { + constexpr std::string_view kUtf8Bom{"\xEF\xBB\xBF"sv}; + this->os_.get().write(kUtf8Bom.data(), kUtf8Bom.size()); + } } - } - /*! - * \brief Output stream to write into - */ - WriterType os_; + /// \brief Output stream to write into + WriterType os_; - /*! - * \brief Serializer state - */ - SerializerState serializer_state_; + /// \brief Serializer state + SerializerState serializer_state_; }; +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h index ddead882ac..d08b984f2e 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h @@ -10,58 +10,46 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Implementation of methods for generic value serializer type. - * - *********************************************************************************************************************/ +/// \file +/// \brief Implementation of methods for generic value serializer type. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" -namespace amsr { -namespace json { -/*! - * \brief Serializes another object into the output stream - * \vprivate component private - * - * \spec - * requires true; - * \endspec - * \internal - * - Assert that the current state allows adding an object. - * - Add an opening curly bracket. - * - Serialize the object as a JSON object. - * - Add a closing curly bracket. - * \endinternal - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief Serializes another object into the output stream +/// \details +/// - Assert that the current state allows adding an object. +/// - Add an opening curly bracket. +/// - Serialize the object as a JSON object. +/// - Add a closing curly bracket. template template -// VECTOR NCL AutosarC++17_10-M9.3.3: MD_JSON_AutosarC++17_10-M9.3.3_logical_const +// coverity[autosar_cpp14_m9_3_3_violation] auto GenericValueSerializer::operator<<(JObjectType object) && noexcept - -> GenericValueSerializer::Next { - return this->Serialize([this, &object]() noexcept { - /*! - * \brief Defines the return value type - */ - using ReturnType = decltype(object.fn(ObjectStart(this->os_.get()))); - static_assert(std::is_same::value, "Cannot close object in current state"); + -> GenericValueSerializer::Next +{ + return this->Serialize([this, &object]() noexcept { + /// \brief Defines the return value type + using ReturnType = decltype(object.fn(ObjectStart(this->os_.get()))); + static_assert(std::is_same::value, "Cannot close object in current state"); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('{'); - // VCA_VAJSON_OUTPUTSTREAM - static_cast(object.fn(ObjectStart(this->os_.get()))); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('}'); - }); + this->os_.get().put('{'); + static_cast(object.fn(ObjectStart(this->os_.get()))); + this->os_.get().put('}'); + }); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h index b58253c827..af32fea9ac 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h @@ -10,17 +10,12 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Serializer for JSON keys. - * - *********************************************************************************************************************/ +/// \file +/// \brief Serializer for JSON keys. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" @@ -29,198 +24,103 @@ #include "score/json/internal/writer/vajson/writer/types/basic_types.h" #include "score/json/internal/writer/vajson/writer/types/object_type.h" -namespace amsr { -namespace json { -/*! - * \brief A serializer for JSON keys - * - * \details This class only allows adding a key into the object and always returns a value serializer to only - * allow a value for the next concatenation operation. - * \vpublic - */ -class KeySerializer final { - public: - /*! - * \brief Serializer state after adding a key - * \vpublic - */ - using Next = ObjectSerializerValue; - - /*! - * \brief Constructs a KeySerializer from an output stream - * \details Do not create an instance of KeySerializer directly, use the vpublic aliases in - * amsr/json/writer/serializer.h - * \vpublic - * - * \param[in] os - * Output stream to write into. - * \param[in] state - * of the Serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - explicit KeySerializer(WriterType os, SerializerState state = SerializerState::kEmpty) noexcept - : os_(os), serializer_state_{state} {} - - /*! - * \brief Default move constructor - * - * \vpublic - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - KeySerializer(KeySerializer&&) noexcept = default; - - /*! - * \brief Default move assignment - * \vpublic - * - * \return A reference to the moved into object. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ - auto operator=(KeySerializer&&) & noexcept -> KeySerializer& = default; - - // Deleted copy constructor copy assignment operator. - KeySerializer(KeySerializer const&) = delete; - auto operator=(KeySerializer const&) -> KeySerializer& = delete; - - /*! - * \brief Default DTOR - * \vpublic - */ - ~KeySerializer() noexcept = default; - - /*! - * \brief Serializes a key - * \vpublic - * - * \param[in] key - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * - * \internal - * - Add a comma, if necessary. - * - Serialize the key. - * \endinternal - */ - auto operator<<(JKeyType key) const&& noexcept -> Next { - this->WriteComma(); - - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('"'); - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get() << internal::EscapedJsonString(key); - // VCA_VAJSON_OUTPUTSTREAM - constexpr auto colon_str = R"(":)"sv; - this->os_.get().write(colon_str.data(), colon_str.size()); - - return Next(this->os_.get()); - } - - /*! - * \brief Serializes a binary key - * \vpublic - * - * \param[in] key - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - * - * \internal - * - Add a comma, if necessary. - * - Serialize the length of the key as four bytes big endian. - * - Write the key. - * \endinternal - */ - auto operator<<(JBinKeyType key) const&& noexcept -> Next { - this->WriteComma(); - - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put('k'); - // VCA_VAJSON_OUTPUTSTREAM - internal::SerializeLength(this->os_.get(), key.GetLength()); - // VCA_VAJSON_OUTPUTSTREAM - const auto value = key.GetValue(); - this->os_.get().write(value.data(), value.size()); - - return Next(this->os_.get()); - } - - private: - /*! - * \brief Adds a comma to the stream, if necessary - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - * - * \internal - * - If another element was serialized before, add a comma. - * \endinternal - */ - void WriteComma() const noexcept { - if (this->serializer_state_ == SerializerState::kNonEmpty) { - // VCA_VAJSON_OUTPUTSTREAM - this->os_.get().put(','); +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief A serializer for JSON keys +/// \details This class only allows adding a key into the object and always returns a value serializer to only allow a +/// value for the next concatenation operation. +class KeySerializer final +{ + public: + /// \brief Serializer state after adding a key + using Next = ObjectSerializerValue; + + /// \brief Constructs a KeySerializer from an output stream + /// \details Do not create an instance of KeySerializer directly, use the aliases in + /// score/json/internal/writer/vajson/writer/serializers/structures/serializer.h + /// \param[in] os Output stream to write into. + /// \param[in] state of the Serializer. + explicit KeySerializer(WriterType os, SerializerState state = SerializerState::kEmpty) noexcept + : os_(os), serializer_state_{state} + { + } + + /// \brief Default move constructor + KeySerializer(KeySerializer&&) noexcept = default; + + /// \brief Default move assignment + /// \return A reference to the moved into object. + auto operator=(KeySerializer&&) & noexcept -> KeySerializer& = default; + + // Deleted copy constructor copy assignment operator. + KeySerializer(const KeySerializer&) = delete; + auto operator=(const KeySerializer&) -> KeySerializer& = delete; + + /// \brief Default DTOR + ~KeySerializer() noexcept = default; + + /// \brief Serializes a key + /// \details + /// - Add a comma, if necessary. + /// - Serialize the key. + /// \param[in] key to serialize. + /// \return The succeeding serializer. + auto operator<<(JKeyType key) const&& noexcept -> Next + { + this->WriteComma(); + + this->os_.get().put('"'); + this->os_.get() << internal::EscapedJsonString(key); + constexpr auto colon_str = R"(":)"sv; + this->os_.get().write(colon_str.data(), colon_str.size()); + + return Next(this->os_.get()); + } + + /// \brief Serializes a binary key + /// \details + /// - Add a comma, if necessary. + /// - Serialize the length of the key as four bytes big endian. + /// - Write the key. + /// \param[in] key to serialize. + /// \return The succeeding serializer. + auto operator<<(JBinKeyType key) const&& noexcept -> Next + { + this->WriteComma(); + + this->os_.get().put('k'); + internal::SerializeLength(this->os_.get(), key.GetLength()); + const auto value = key.GetValue(); + this->os_.get().write(value.data(), value.size()); + + return Next(this->os_.get()); + } + + private: + /// \brief Adds a comma to the stream, if necessary + /// \details + /// - If another element was serialized before, add a comma. + void WriteComma() const noexcept + { + if (this->serializer_state_ == SerializerState::kNonEmpty) + { + this->os_.get().put(','); + } } - } - /*! - * \brief Output stream to write into - */ - WriterType os_; + /// \brief Output stream to write into + WriterType os_; - /*! - * \brief Serializer state - */ - SerializerState serializer_state_; + /// \brief Serializer state + SerializerState serializer_state_; }; +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h index 85ddd81ca8..8da34b896f 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h @@ -10,143 +10,78 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Contains common types and forward declarations for JSON serializers. - * - *********************************************************************************************************************/ -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ +/// \file +/// \brief Contains common types and forward declarations for JSON serializers. +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -namespace amsr { -namespace json { -/*! - * \brief State of the object to be serialized - * - * \details Indicates if the object is empty or not. Commas should only appended if the object is not empty. - * \vpublic - */ -enum class SerializerState : bool { kEmpty, kNonEmpty }; +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief State of the object to be serialized +/// \details Indicates if the object is empty or not. Commas should only appended if the object is not empty. +enum class SerializerState : bool +{ + kEmpty, + kNonEmpty +}; -/*! - * \brief An empty type that signifies that the serializer has no follow-up state - * - * \vprivate component private - * - * \trace DSGN-JSON-Writer-Error-Prevention - */ -class Unit { - public: - /*! - * \brief Constructs a Unit type from an output stream - * - * \details This satisfies the 'Next' state interface for serializers. - * \vprivate Vector component internal API - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - explicit Unit(std::reference_wrapper, - SerializerState = SerializerState::kEmpty) noexcept {} +/// \brief An empty type that signifies that the serializer has no follow-up state +class Unit +{ + public: + /// \brief Constructs a Unit type from an output stream + /// \details This satisfies the 'Next' state interface for serializers. + explicit Unit(std::reference_wrapper, SerializerState = SerializerState::kEmpty) noexcept {} }; -/*! - * \brief A marker struct that only tells the GenericValueSerializer to return itself after using operator<<() - * - * \vprivate component private - * - * \trace DSGN-JSON-Writer-Error-Prevention - */ -class Self {}; +/// \brief A marker struct that only tells the GenericValueSerializer to return itself after using operator<<() +class Self +{ +}; -/*! - * \brief Forward declaration for the GenericValueSerializer - * - * \vprivate component private - */ +/// \brief Forward declaration for the GenericValueSerializer template class GenericValueSerializer; -/*! - * \brief A serializer type for single values - * - * \vprivate component private - */ +/// \brief A serializer type for single values using ValueSerializer = GenericValueSerializer; -/*! - * \brief A serializer type for JSON documents - * - * \details Intentionally a using to make it obvious that a JSON document must start with a single value. - * \vpublic - */ +/// \brief A serializer type for JSON documents +/// \details Intentionally a using to make it obvious that a JSON document must start with a single value. using DocumentSerializer = ValueSerializer; -/********************************************************************************************************************** - * Object Declarations * - *********************************************************************************************************************/ - -/*! - * \brief Forward declaration for the KeySerializer - */ +/// \brief Forward declaration for the KeySerializer class KeySerializer; -/*! - * \brief A serializer type for the start of JSON objects - * - * \details Typedef for the initial Object serializer state where only a key is allowed. - * \vpublic - */ +/// \brief A serializer type for the start of JSON objects +/// \details Typedef for the initial Object serializer state where only a key is allowed. using ObjectStart = KeySerializer; -/*! - * \brief A serializer type for JSON objects - * - * \details This class only allows adding a value into the object and the next concatenation will only allow a - * key. - * \vprivate component private - */ +/// \brief A serializer type for JSON objects +/// \details This class only allows adding a value into the object and the next concatenation will only allow a key. using ObjectSerializerValue = GenericValueSerializer; -/********************************************************************************************************************** - * Tuple Declarations * - *********************************************************************************************************************/ - -/*! - * \brief A serializer type for JSON arrays - * - * \details Serializes multiple, potentially inhomogeneous values. - * \vprivate component private - */ +/// \brief A serializer type for JSON arrays +/// \details Serializes multiple, potentially inhomogeneous values. using ArraySerializer = GenericValueSerializer<>; -/*! - * \brief A serializer type for the start of JSON arrays - * - * \details Typedef for the initial Array serializer state. - * \vpublic - */ +/// \brief A serializer type for the start of JSON arrays +/// \details Typedef for the initial Array serializer state. using ArrayStart = ArraySerializer; -/*! - * \brief Type of the output writer - * - * \vpublic - */ +/// \brief Type of the output writer using WriterType = std::reference_wrapper; +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 9084393003..8a00f7f410 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -10,17 +10,12 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Serializer for JSON string literals. - * - *********************************************************************************************************************/ +/// \file +/// \brief Serializer for JSON string literals. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -30,163 +25,106 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace amsr { -namespace json { -namespace internal { -/*! - * \brief An escaped JSON string type - * \vprivate component private - */ -class EscapedJsonString { - public: - /*! - * \brief Constructs an EscapedJsonString from a JSON key - * \vprivate Vector component internal API - * - * \param[in] key - * to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - explicit EscapedJsonString(JKeyType key) noexcept : EscapedJsonString(key.GetValue()) {} +namespace score +{ +namespace json +{ +namespace vajson +{ +namespace internal +{ +/// \brief An escaped JSON string type +class EscapedJsonString +{ + public: + /// \brief Constructs an EscapedJsonString from a JSON key + /// \param[in] key to serialize. + explicit EscapedJsonString(JKeyType key) noexcept : EscapedJsonString(key.GetValue()) {} - /*! - * \brief Constructs an EscapedJsonString from a JSON string - * \vprivate Vector component internal API - * - * \param[in] string - * to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - explicit EscapedJsonString(JStringType string) noexcept : EscapedJsonString(string.GetValue()) {} + /// \brief Constructs an EscapedJsonString from a JSON string + /// \param[in] string to serialize. + explicit EscapedJsonString(JStringType string) noexcept : EscapedJsonString(string.GetValue()) {} - /*! - * \brief Returns the contained string - * \vprivate Vector component internal API - * - * \return The contained string. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::string_view { return this->value_; } + /// \brief Returns the contained string + /// \return The contained string. + auto GetValue() const noexcept -> std::string_view + { + return this->value_; + } - private: - /*! - * \brief Constructs an EscapedJsonString from a StringView - * \param[in] value - * The value to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - */ - explicit EscapedJsonString(std::string_view value) noexcept : value_{value} {} + private: + /// \brief Constructs an EscapedJsonString from a StringView + /// \param[in] value The value to serialize. + explicit EscapedJsonString(std::string_view value) noexcept : value_{value} {} - /*! - * \brief Value to write as a JSON string literal - */ - std::string_view value_; + /// \brief Value to write as a JSON string literal + std::string_view value_; }; // NOLINTNEXTLINE(whitespace/line_length) -// VECTOR NCL AutosarC++17_10-M5.0.16, Metric-HIS.VG: MD_JSON_AutosarC++17_10-M5.0.16_pointer_arithmetic, MD_JSON_Metric-HIS.VG_json_value -/*! - * \brief Serializes an escaped string literal type - * \vprivate component private - * - * \param[in] os - * Output stream to write into. - * \param[in] string - * to escape and serialize. - * \return A reference to the output stream. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - * - * \internal - * - If the string contains a character that needs to be escaped in JSON: - * - Serialize the escaped character. - * - Otherwise: - * - Serialize the character directly. - * \endinternal - */ -auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> std::ostream& { - for (char const ch : string.GetValue()) { - switch (std::char_traits::to_int_type(ch)) { - case std::char_traits::to_int_type('"'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\\"", 2); - break; - } - case std::char_traits::to_int_type('\\'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\\\", 2); - break; - } - case std::char_traits::to_int_type('\b'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\b", 2); - break; - } - case std::char_traits::to_int_type('\f'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\f", 2); - break; - } - case std::char_traits::to_int_type('\n'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\n", 2); - break; - } - case std::char_traits::to_int_type('\r'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\r", 2); - break; - } - case std::char_traits::to_int_type('\t'): { - // VCA_VAJSON_OUTPUTSTREAM - os.write("\\t", 2); - break; - } - default: - // VCA_VAJSON_OUTPUTSTREAM - os.put(ch); - break; +// coverity[autosar_cpp14_m5_0_16_violation] +/// \brief Serializes an escaped string literal type +/// \details +/// - If the string contains a character that needs to be escaped in JSON: +/// - Serialize the escaped character. +/// - Otherwise: +/// - Serialize the character directly. +/// \param[in] os Output stream to write into. +/// \param[in] string to escape and serialize. +/// \return A reference to the output stream. +auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> std::ostream& +{ + for (const char ch : string.GetValue()) + { + switch (std::char_traits::to_int_type(ch)) + { + case std::char_traits::to_int_type('"'): + { + os.write("\\\"", 2); + break; + } + case std::char_traits::to_int_type('\\'): + { + os.write("\\\\", 2); + break; + } + case std::char_traits::to_int_type('\b'): + { + os.write("\\b", 2); + break; + } + case std::char_traits::to_int_type('\f'): + { + os.write("\\f", 2); + break; + } + case std::char_traits::to_int_type('\n'): + { + os.write("\\n", 2); + break; + } + case std::char_traits::to_int_type('\r'): + { + os.write("\\r", 2); + break; + } + case std::char_traits::to_int_type('\t'): + { + os.write("\\t", 2); + break; + } + default: + os.put(ch); + break; + } } - } - return os; + return os; } } // namespace internal +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H diff --git a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h index 1cffc95445..eb96fd0381 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h @@ -10,62 +10,47 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Serializer for length tags. - * - *********************************************************************************************************************/ +/// \file +/// \brief Serializer for length tags. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -namespace amsr { -namespace json { -namespace internal { -/*! - * \brief Serializes a length value as big endian - * \vprivate component private - * - * \param[in] os - * Stream to write to. - * \param[in] length - * to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - * - * \internal - * - Copy each byte of the value to byte buffer, big-endian byte order, - * - Write byte buffer to the stream. - * \endinternal - */ -inline void SerializeLength(WriterType os, std::uint32_t const length) noexcept { - constexpr std::size_t kPrefixSize{sizeof(std::uint32_t)}; - - std::array arr{}; - arr[0] = static_cast((length >> 24) & 0xffu); - arr[1] = static_cast((length >> 16) & 0xffu); - arr[2] = static_cast((length >> 8) & 0xffu); - arr[3] = static_cast(length & 0xffu); - - // VCA_VAJSON_OUTPUTSTREAM - os.get().write(reinterpret_cast(arr.data()), kPrefixSize); +namespace score +{ +namespace json +{ +namespace vajson +{ +namespace internal +{ +/// \brief Serializes a length value as big endian +/// \details +/// - Copy each byte of the value to byte buffer, big-endian byte order, +/// - Write byte buffer to the stream. +/// \param[in] os Stream to write to. +/// \param[in] length to serialize. +inline void SerializeLength(WriterType os, const std::uint32_t length) noexcept +{ + constexpr std::size_t kPrefixSize{sizeof(std::uint32_t)}; + + std::array arr{}; + arr[0] = static_cast((length >> 24) & 0xffu); + arr[1] = static_cast((length >> 16) & 0xffu); + arr[2] = static_cast((length >> 8) & 0xffu); + arr[3] = static_cast(length & 0xffu); + + os.get().write(reinterpret_cast(arr.data()), kPrefixSize); } } // namespace internal +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac.h b/score/json/internal/writer/vajson/writer/serializers/vac.h index c8a998571e..bb52e16f74 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac.h @@ -10,21 +10,15 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for native libVac types. - * - * \details Single include for all libVac types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for native libVac types. +/// \details Single include for all libVac types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include "score/json/internal/writer/vajson/writer/serializers/vac/primitives.h" #include "score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h" #include "score/json/internal/writer/vajson/writer/serializers/vac/variant.h" -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h index d6d03f3446..011a5595fd 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h @@ -10,19 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for libVac primitive data types. - * - * \details Provides serializers for std::string, std::string_view, and std::uint8_t types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for libVac primitive data types. +/// \details Provides serializers for std::string, std::string_view, and std::uint8_t types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -31,101 +25,54 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace amsr { -namespace json { -/*! - * \brief Forward declaration for the GenericValueSerializer - * - * \vprivate component private - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief Forward declaration for the GenericValueSerializer template class GenericValueSerializer; -/*! - * \brief Serializes a string value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] string - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a string value directly +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] string to serialize. +/// \return The succeeding serializer. template -auto operator<<(GenericValueSerializer&& serializer, std::string const& string) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JString(string); +auto operator<<(GenericValueSerializer&& serializer, const std::string& string) noexcept -> + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JString(string); } -/*! - * \brief Serializes a string value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] string - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a string value directly +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] string to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, ::std::string_view string) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << JString(string); + typename GenericValueSerializer::Next +{ + return std::move(serializer) << JString(string); } -/*! - * \brief Serializes a std::uint8_t value directly - * \vpublic - * - * \tparam Next - * type of serializer. - * \param[in] serializer - * instance to write into. - * \param[in] byte - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a std::uint8_t value directly +/// \tparam Next type of serializer. +/// \param[in] serializer instance to write into. +/// \param[in] byte to serialize. +/// \return The succeeding serializer. template auto operator<<(GenericValueSerializer&& serializer, ::std::uint8_t byte) noexcept -> - typename GenericValueSerializer::Next { - return std::move(serializer) << static_cast(byte); + typename GenericValueSerializer::Next +{ + return std::move(serializer) << static_cast(byte); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h index 1f48c8b99b..d072d65104 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h @@ -10,20 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for libVac sequence containers. - * - * \details Provides serializers for score::cpp::span, score::cpp::static_vector - * types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for libVac sequence containers. +/// \details Provides serializers for score::cpp::span, score::cpp::static_vector types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include "score/json/internal/writer/vajson/writer/types/array_type.h" @@ -33,104 +26,55 @@ #include "score/static_vector.h" -namespace amsr { -namespace json { +namespace score +{ +namespace json +{ +namespace vajson +{ -/*! - * \brief Serializes an array view of serializable elements - * \vpublic - * - * \tparam Serializer - * Type of serializer. - * \tparam Value - * Type of value. - * \param[in] serializer - * instance to write into. - * \param[in] view - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an array view of serializable elements +/// \tparam Serializer Type of serializer. +/// \tparam Value Type of value. +/// \param[in] serializer instance to write into. +/// \param[in] view to serialize. +/// \return The succeeding serializer. template -auto operator<<(Serializer&& serializer, ::score::cpp::span const& view) noexcept -> typename Serializer::Next { - return std::forward(serializer) << JArray(view); +auto operator<<(Serializer&& serializer, const ::score::cpp::span& view) noexcept -> typename Serializer::Next +{ + return std::forward(serializer) << JArray(view); } -/*! - * \brief Serializes a vector of serializable elements - * \vpublic - * - * \tparam Serializer - * Type of serializer. - * \tparam Value - * Type of value. - * \tparam Alloc - * Vector allocator. - * \param[in,out] serializer - * instance to write into. - * \param[in] vector - * Data to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a vector of serializable elements +/// \tparam Serializer Type of serializer. +/// \tparam Value Type of value. +/// \tparam Alloc Vector allocator. +/// \param[in,out] serializer instance to write into. +/// \param[in] vector Data to serialize. +/// \return The succeeding serializer. template -auto operator<<(Serializer&& serializer, ::score::cpp::pmr::vector const& vector) noexcept -> - typename Serializer::Next { - return std::forward(serializer) << JArray(vector); +auto operator<<(Serializer&& serializer, const ::score::cpp::pmr::vector& vector) noexcept -> + typename Serializer::Next +{ + return std::forward(serializer) << JArray(vector); } -/*! - * \brief Serializes a static vector of serializable elements - * \vpublic - * - * \tparam Serializer - * Type of serializer. - * \tparam Value - * Type of value. - * \tparam Alloc - * Type of allocator. - * \param[in] serializer - * instance to write into. - * \param[in] vector - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a static vector of serializable elements +/// \tparam Serializer Type of serializer. +/// \tparam Value Type of value. +/// \tparam Alloc Type of allocator. +/// \param[in] serializer instance to write into. +/// \param[in] vector to serialize. +/// \return The succeeding serializer. template -auto operator<<(Serializer&& serializer, ::score::cpp::static_vector const& vector) noexcept -> - typename Serializer::Next { - return std::forward(serializer) << JArray(vector); +auto operator<<(Serializer&& serializer, const ::score::cpp::static_vector& vector) noexcept -> + typename Serializer::Next +{ + return std::forward(serializer) << JArray(vector); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/variant.h b/score/json/internal/writer/vajson/writer/serializers/vac/variant.h index 7583aea9e0..89643eb238 100644 --- a/score/json/internal/writer/vajson/writer/serializers/vac/variant.h +++ b/score/json/internal/writer/vajson/writer/serializers/vac/variant.h @@ -10,115 +10,67 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Provides a serializer for the score::cpp::variant type. - * - *********************************************************************************************************************/ +/// \file +/// \brief Provides a serializer for the score::cpp::variant type. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include "score/variant.hpp" -namespace amsr { -namespace json { -/*! - * \brief A generic variant visitor - * \details Serializes the children according to their implemented serializers. - * \vprivate component private - * - * \tparam Serializer - * Type of serializer. Must be noexcept move constructible and noexcept move assignable. - * - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +/// \brief A generic variant visitor +/// \details Serializes the children according to their implemented serializers. +/// \tparam Serializer Type of serializer. Must be noexcept move constructible and noexcept move assignable. template -class VariantVisitor { - static_assert(std::is_nothrow_move_constructible::value, "Serializer must be noexcept movable"); - static_assert(std::is_nothrow_move_assignable::value, "Serializer must be noexcept movable"); +class VariantVisitor +{ + static_assert(std::is_nothrow_move_constructible::value, "Serializer must be noexcept movable"); + static_assert(std::is_nothrow_move_assignable::value, "Serializer must be noexcept movable"); - public: - /*! - * \brief Constructs the visitor - * \details Visitor must only be used once. - * \vprivate Vector component internal API - * - * \param[in] serializer - * to use. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - explicit VariantVisitor(Serializer&& serializer) noexcept : serializer_(std::move(serializer)) {} + public: + /// \brief Constructs the visitor + /// \details Visitor must only be used once. + /// \param[in] serializer to use. + explicit VariantVisitor(Serializer&& serializer) noexcept : serializer_(std::move(serializer)) {} - /*! - * \brief Call operator - * \vprivate Vector component internal API - * - * \tparam T - * Type of the variant. - * \param[in] value - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - template - auto operator()(T const& value) noexcept -> typename Serializer::Next { - return std::move(this->serializer_) << value; - } + /// \brief Call operator + /// \tparam T Type of the variant. + /// \param[in] value to serialize. + /// \return The succeeding serializer. + template + auto operator()(const T& value) noexcept -> typename Serializer::Next + { + return std::move(this->serializer_) << value; + } - private: - /*! - * \brief Serializer of the variant - */ - Serializer serializer_; + private: + /// \brief Serializer of the variant + Serializer serializer_; }; -/*! - * \brief Serializes a variant of serializable elements - * \vpublic - * - * \tparam Serializer - * Type of serializer. - * \tparam Types - * of values in the variant. - * \param[in] s - * Serializer instance to write into. - * \param[in] variant - * to serialize. - * \return The succeeding serializer. - * - * \context ANY - * \pre All alternatives contained in the variant are noexcept movable - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \trace DSGN-JSON-Writer-Serializable-Data-Structures - */ +/// \brief Serializes a variant of serializable elements +/// \tparam Serializer Type of serializer. +/// \tparam Types of values in the variant. +/// \param[in] s Serializer instance to write into. +/// \param[in] variant to serialize. +/// \return The succeeding serializer. +/// \pre All alternatives contained in the variant are noexcept movable template -auto operator<<(Serializer s, score::cpp::variant& variant) noexcept -> typename Serializer::Next { - VariantVisitor ser{std::move(s)}; - // VCA_VAJSON_EXTERNAL_CALL - return score::cpp::visit(std::move(ser), variant); +auto operator<<(Serializer s, score::cpp::variant& variant) noexcept -> typename Serializer::Next +{ + VariantVisitor ser{std::move(s)}; + return score::cpp::visit(std::move(ser), variant); } +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_SERIALIZERS_VAC_VARIANT_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H diff --git a/score/json/internal/writer/vajson/writer/types/array_type.h b/score/json/internal/writer/vajson/writer/types/array_type.h index 64573cf0ac..1149e0f837 100644 --- a/score/json/internal/writer/vajson/writer/types/array_type.h +++ b/score/json/internal/writer/vajson/writer/types/array_type.h @@ -10,19 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for range-based containers. - * - * \details Provides serializers for arrays and tuples. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for range-based containers. +/// \details Provides serializers for arrays and tuples. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_ARRAY_TYPE_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_ARRAY_TYPE_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -31,147 +25,92 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace amsr { -namespace json { -inline namespace types { -/*! - * \brief A serializer type for a JSON array from a homogeneous C++ range - * \vprivate Vector component internal API - * \tparam Range - * Type of range. - * \tparam Fn - * The function type for this serializer. - */ +namespace score +{ +namespace json +{ +namespace vajson +{ +inline namespace types +{ +/// \brief A serializer type for a JSON array from a homogeneous C++ range +/// \tparam Range Type of range. +/// \tparam Fn The function type for this serializer. template -class RangeSerializer final { - public: - /*! - * \brief Constructs a RangeSerializer - * \vprivate Vector component internal API - * \tparam Fn1 - * Type of function. - * \param[in] range - * to serialize. - * \param[in] fn - * Function used to serialize. Must not throw exceptions. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - template - RangeSerializer(Range const& range, Fn1&& fn) noexcept : container_{range}, function_{std::forward(fn)} {} +class RangeSerializer final +{ + public: + /// \brief Constructs a RangeSerializer + /// \tparam Fn1 Type of function. + /// \param[in] range to serialize. + /// \param[in] fn Function used to serialize. Must not throw exceptions. + template + RangeSerializer(const Range& range, Fn1&& fn) noexcept : container_{range}, function_{std::forward(fn)} + { + } - /*! - * \brief Call operator - * \vprivate Vector component internal API - * - * \tparam AS - * Type of array serializer. - * \param[in] as - * Array serializer to write into. - * - * \context ANY - * \pre The function contained in the class does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \internal - * - Serialize every element of the array as a JSON value. - * \endinternal - */ - template - void operator()(AS as) const noexcept { - // VCA_VAJSON_THIS_DEREF - for (auto const& value : this->container_.get()) { - as = std::move(as) << this->function_(value); - } // VCA_VAJSON_EXTERNAL_CALL - } + /// \brief Call operator + /// \details + /// - Serialize every element of the array as a JSON value. + /// \tparam AS Type of array serializer. + /// \param[in] as Array serializer to write into. + /// \pre The function contained in the class does not throw any exceptions + template + void operator()(AS as) const noexcept + { + for (const auto& value : this->container_.get()) + { + as = std::move(as) << this->function_(value); + } + } - private: - /*! - * \brief Container instance to be serialized - */ - std::reference_wrapper container_; + private: + /// \brief Container instance to be serialized + std::reference_wrapper container_; - /*! - * \brief Function to serialize single items with - */ - Fn function_; + /// \brief Function to serialize single items with + Fn function_; }; -/*! - * \brief Serialize an ad-hoc defined Tuple as heterogeneous array - * \vprivate Vector component internal API - * \tparam Fn - * The function type that defines the serialization. - */ +/// \brief Serialize an ad-hoc defined Tuple as heterogeneous array +/// \tparam Fn The function type that defines the serialization. template -struct JArrayType final { - /*! - * \brief Wrapped function value - */ - Fn fn; +struct JArrayType final +{ + /// \brief Wrapped function value + Fn fn; }; -/*! - * \brief Serializes an ad-hoc defined Tuple as a heterogeneous array - * \details The function can be used to define a tuple by adding values. - * \vpublic - * \tparam Fn - * Type of serializer function. Must take an ArrayStart&& and return the follow-up serializer. - * \param[in] fn - * Function used to serialize the tuple. - * \return A serializable Tuple type. - * - * \context ANY - * \pre The passed function does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an ad-hoc defined Tuple as a heterogeneous array +/// \details The function can be used to define a tuple by adding values. +/// \tparam Fn Type of serializer function. Must take an ArrayStart&& and return the follow-up serializer. +/// \param[in] fn Function used to serialize the tuple. +/// \return A serializable Tuple type. +/// \pre The passed function does not throw any exceptions template ::value>> -auto JArray(Fn&& fn) noexcept -> JArrayType { // VECTOR SL AutosarC++17_10-A13.3.1: MD_JSON_rvalue_ref - return {std::forward(fn)}; +auto JArray(Fn&& fn) noexcept -> JArrayType +{ // coverity[autosar_cpp14_a13_3_1_violation] + return {std::forward(fn)}; } -/*! - * \brief Serializes a homogeneous C++ range as a JSON array - * \vpublic - * \tparam Range - * Type of range. - * \tparam Fn - * Type of value serializer function. Must take the range's value type and return a JSON type. - * \param[in] range - * instance to be serialized. - * \param[in] fn - * Function used to serialize single elements. - * \return A serializable JSON array. - * - * \context ANY - * \pre The passed range & function do not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a homogeneous C++ range as a JSON array +/// \tparam Range Type of range. +/// \tparam Fn Type of value serializer function. Must take the range's value type and return a JSON type. +/// \param[in] range instance to be serialized. +/// \param[in] fn Function used to serialize single elements. +/// \return A serializable JSON array. +/// \pre The passed range & function do not throw any exceptions template > -auto JArray(Range const& range, Fn&& fn = IdSerializer{}) noexcept -> JArrayType> { - return {RangeSerializer{range, std::forward(fn)}}; +auto JArray(const Range& range, Fn&& fn = IdSerializer{}) noexcept -> JArrayType> +{ + return {RangeSerializer{range, std::forward(fn)}}; } // clang-format off } // inline namespace types // clang-format off +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_ARRAY_TYPE_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_ARRAY_TYPE_H diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h index 7570da5a9c..df3bba9a8a 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -10,19 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for basic JSON types. - * - * \details Provides serializers for Null, Bool, Key, Number, and String types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for basic JSON types. +/// \details Provides serializers for Null, Bool, Key, Number, and String types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BASIC_TYPES_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BASIC_TYPES_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -30,501 +24,247 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -namespace amsr { -namespace json { -inline namespace types { -/*! - * \brief A Null type - * - * \vprivate Vector component internal API - */ -struct JNullType final {}; - -/*! - * \brief Serializes a Null value - * \vpublic - * \return The serializable null type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr inline auto JNull() noexcept -> JNullType { return JNullType{}; } - -/*! - * \brief A Bool type - * - * \vprivate Vector component internal API - */ -struct JBoolType final { - /*! - * \brief Wrapped bool value - */ - bool value; +namespace score +{ +namespace json +{ +namespace vajson +{ +inline namespace types +{ +/// \brief A Null type +struct JNullType final +{ }; -/*! - * \brief Serializes a Bool value - * \vpublic - * \param[in] b - * Bool value to serialize. - * \return The serializable bool type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr inline auto JBool(bool b) noexcept -> JBoolType { return {b}; } - -/*! - * \brief A Key type - * - * \vprivate Vector component internal API - */ -class JKeyType final { - public: - /*! - * \brief Constructs a Key type - * \param[in] s - * Key to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - */ - explicit constexpr JKeyType(std::string_view s) noexcept : value_(s) {} - - /*! - * \brief Returns the contained value - * \return The value. - * - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::string_view { return this->value_; } - - private: - /*! - * \brief Wrapped string value - */ - std::string_view value_; +/// \brief Serializes a Null value +/// \return The serializable null type. +constexpr inline auto JNull() noexcept -> JNullType +{ + return JNullType{}; +} + +/// \brief A Bool type +struct JBoolType final +{ + /// \brief Wrapped bool value + bool value; }; -/*! - * \brief Serializes a Key value - * \vpublic - * \param[in] s - * Key to serialize. - * \return The serializable key type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr auto JKey(std::string_view s) noexcept -> JKeyType { return JKeyType{s}; } - -/*! - * \brief Serializes a Key value - * \vpublic - * \param[in] s - * Key to serialize. - * \return The serializable key type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JKey(std::string const& s) noexcept -> JKeyType { return JKey(std::string_view(s)); } - -inline namespace literals { -/*! - * \brief Serializes a Key value - * \vpublic - * \param[in] s - * String literal to serialize. - * \param[in] size - * Size of the pointer. - * \return The serializable key type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \synchronous - - * \spec - * requires true; - * \endspec - */ -// VECTOR NCL AutosarC++17_10-A13.1.3: MD_JSON_A13-1-3_false_positive -constexpr auto operator""_key(char const* s, std::size_t size) noexcept -> JKeyType { - return JKey(score::safecpp::zstring_view{s, size}); +/// \brief Serializes a Bool value +/// \param[in] b Bool value to serialize. +/// \return The serializable bool type. +constexpr inline auto JBool(bool b) noexcept -> JBoolType +{ + return {b}; +} + +/// \brief A Key type +class JKeyType final +{ + public: + /// \brief Constructs a Key type + /// \param[in] s Key to serialize. + explicit constexpr JKeyType(std::string_view s) noexcept : value_(s) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> std::string_view + { + return this->value_; + } + + private: + /// \brief Wrapped string value + std::string_view value_; +}; + +/// \brief Serializes a Key value +/// \param[in] s Key to serialize. +/// \return The serializable key type. +constexpr auto JKey(std::string_view s) noexcept -> JKeyType +{ + return JKeyType{s}; +} + +/// \brief Serializes a Key value +/// \param[in] s Key to serialize. +/// \return The serializable key type. +inline auto JKey(const std::string& s) noexcept -> JKeyType +{ + return JKey(std::string_view(s)); +} + +inline namespace literals +{ +/// \brief Serializes a Key value +/// \param[in] s String literal to serialize. +/// \param[in] size Size of the pointer. +/// \return The serializable key type. +// coverity[autosar_cpp14_a13_1_3_violation] +constexpr auto operator""_key(const char* s, std::size_t size) noexcept -> JKeyType +{ + return JKey(score::safecpp::zstring_view{s, size}); } // clang-format off } // namespace literals // clang-format on -/*! - * \brief A Number type - * \vprivate Vector component internal API - * \tparam N - * Type of number. - */ +/// \brief A Number type +/// \tparam N Type of number. template ::value>::type> -class JNumberType final { - public: - /*! - * \brief Constructs a Number type - * \vprivate Vector component internal API - * - * \param[in] num - * Number to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JNumberType(N num) noexcept : value_(num) {} - - /*! - * \brief Returns the contained value - * \return The value. - * - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> N { return this->value_; } - - private: - /*! - * \brief Wrapped number value - */ - N value_; +class JNumberType final +{ + public: + /// \brief Constructs a Number type + /// \param[in] num Number to serialize. + constexpr explicit JNumberType(N num) noexcept : value_(num) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> N + { + return this->value_; + } + + private: + /// \brief Wrapped number value + N value_; }; -/*! - * \brief A char Number type - * - * \vprivate Vector component internal API - */ +/// \brief A char Number type template <> -class JNumberType final { - public: - /*! - * \brief Constructs a Number type - * \vprivate Vector component internal API - * - * \param[in] num - * Number to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JNumberType(char num) noexcept : value_(std::char_traits::to_int_type(num)) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::int32_t { return this->value_; } - - private: - /*! - * \brief Wrapped number value - */ - std::int32_t value_; +class JNumberType final +{ + public: + /// \brief Constructs a Number type + /// \param[in] num Number to serialize. + constexpr explicit JNumberType(char num) noexcept : value_(std::char_traits::to_int_type(num)) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> std::int32_t + { + return this->value_; + } + + private: + /// \brief Wrapped number value + std::int32_t value_; }; -/*! - * \brief A std::uint8_t Number type - * \vprivate Vector component internal API - */ +/// \brief A std::uint8_t Number type template <> -class JNumberType final { - public: - /*! - * \brief Constructs a Number type - * \vprivate Vector component internal API - * - * \param[in] num - * Number to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JNumberType(std::uint8_t num) noexcept : value_(static_cast(num)) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::uint32_t { return this->value_; } - - private: - /*! - * \brief Wrapped number value - */ - std::uint32_t value_; +class JNumberType final +{ + public: + /// \brief Constructs a Number type + /// \param[in] num Number to serialize. + constexpr explicit JNumberType(std::uint8_t num) noexcept : value_(static_cast(num)) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> std::uint32_t + { + return this->value_; + } + + private: + /// \brief Wrapped number value + std::uint32_t value_; }; -/*! - * \brief A std::int8_t Number type - * - * \vprivate Vector component internal API - */ +/// \brief A std::int8_t Number type template <> -class JNumberType final { - public: - /*! - * \brief Constructs a Number type - * \vprivate Vector component internal API - * - * \param[in] num - * Number to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JNumberType(std::int8_t num) noexcept : value_(static_cast(num)) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::int32_t { return this->value_; } - - private: - /*! - * \brief Wrapped number value - */ - std::int32_t value_; +class JNumberType final +{ + public: + /// \brief Constructs a Number type + /// \param[in] num Number to serialize. + constexpr explicit JNumberType(std::int8_t num) noexcept : value_(static_cast(num)) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> std::int32_t + { + return this->value_; + } + + private: + /// \brief Wrapped number value + std::int32_t value_; }; -/*! - * \brief Serializes a Number value - * \vpublic - * \tparam N - * Type of number. - * \param[in] n - * The number to serialize. - * \return The serializable number type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a Number value +/// \tparam N Type of number. +/// \param[in] n The number to serialize. +/// \return The serializable number type. template ::value>::type> -constexpr auto JNumber(N n) noexcept -> JNumberType { - return JNumberType{n}; +constexpr auto JNumber(N n) noexcept -> JNumberType +{ + return JNumberType{n}; } -/*! - * \brief A String type - * - * \vprivate Vector component internal API - */ -class JStringType final { - public: - /*! - * \brief Constructs a String type - * \vprivate Vector component internal API - * - * \param[in] s - * String to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JStringType(std::string_view s) noexcept : value_(s) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> std::string_view { return this->value_; } - - private: - /*! - * \brief Wrapped string value - */ - std::string_view value_; +/// \brief A String type +class JStringType final +{ + public: + /// \brief Constructs a String type + /// \param[in] s String to serialize. + constexpr explicit JStringType(std::string_view s) noexcept : value_(s) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> std::string_view + { + return this->value_; + } + + private: + /// \brief Wrapped string value + std::string_view value_; }; -/*! - * \brief Serializes a String value - * \vpublic - * - * \param[in] s - * String to serialize. - * \return The serializable string type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr auto JString(std::string_view s) noexcept -> JStringType { return JStringType(s); } - -/*! - * \brief Serializes a String value - * \vpublic - * - * \param[in] s - * String to serialize. - * \return The serializable string type. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JString(std::string const& s) noexcept -> JStringType { return JString(std::string_view{s}); } - -/*! - * \brief A function object used to serialize predefined serializers - * \vprivate Vector component internal API - * \tparam Container - * Type of container to serialize. - */ +/// \brief Serializes a String value +/// \param[in] s String to serialize. +/// \return The serializable string type. +constexpr auto JString(std::string_view s) noexcept -> JStringType +{ + return JStringType(s); +} + +/// \brief Serializes a String value +/// \param[in] s String to serialize. +/// \return The serializable string type. +inline auto JString(const std::string& s) noexcept -> JStringType +{ + return JString(std::string_view{s}); +} + +/// \brief A function object used to serialize predefined serializers +/// \tparam Container Type of container to serialize. template -class IdSerializer { - public: - /*! - * \brief Value Type of container - * \vprivate Vector component internal API - */ - using value_type = typename Container::value_type; - - /*! - * \brief Returns the unchanged value - * \vprivate Vector component internal API - * - * \param[in] v - * Value to return. - * \return The unchanged value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - template - auto operator()(Value&& v) const noexcept -> Value { - return std::forward(v); - } +class IdSerializer +{ + public: + /// \brief Value Type of container + using value_type = typename Container::value_type; + + /// \brief Returns the unchanged value + /// \param[in] v Value to return. + /// \return The unchanged value. + template + auto operator()(Value&& v) const noexcept -> Value + { + return std::forward(v); + } }; // clang-format off } // namespace types // // clang-format on +} // namespace vajson } // namespace json -} // namespace amsr -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BASIC_TYPES_H_ +} // namespace score +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BASIC_TYPES_H diff --git a/score/json/internal/writer/vajson/writer/types/bin_types.h b/score/json/internal/writer/vajson/writer/types/bin_types.h index e735da2788..09223839d5 100644 --- a/score/json/internal/writer/vajson/writer/types/bin_types.h +++ b/score/json/internal/writer/vajson/writer/types/bin_types.h @@ -10,326 +10,167 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Implementation of methods for Bin type. - * - * \details Provides serializers for arrays and tuples. - * - *********************************************************************************************************************/ +/// \file +/// \brief Implementation of methods for Bin type. +/// \details Provides serializers for arrays and tuples. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -namespace amsr { -namespace json { -namespace internal { -/*! - * \brief Safely convert a length - * \param[in] length - * to convert. - * \return The converted length. - * - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ -inline auto ConvertLength(std::size_t const length) noexcept -> std::uint32_t { - AssertCondition(length <= std::numeric_limits::max(), "Length exceeds std::uint32_t range."); - - // VECTOR NCL AutosarC++17_10-A4.7.1: MD_JSON_AutosarC++17_10-A4.7.1_truncating_cast - // VECTOR NL AutosarC++17_10-M0.3.1: MD_JSON_AutosarC++17_10-M0.3.1_OOB - return static_cast(length); +namespace score +{ +namespace json +{ +namespace vajson +{ +namespace internal +{ +/// \brief Safely convert a length +/// \param[in] length to convert. +/// \return The converted length. +inline auto ConvertLength(const std::size_t length) noexcept -> std::uint32_t +{ + AssertCondition(length <= std::numeric_limits::max(), "Length exceeds std::uint32_t range."); + + // coverity[autosar_cpp14_a4_7_1_violation] + // coverity[autosar_cpp14_m0_3_1_violation] + return static_cast(length); } } // namespace internal -inline namespace types { -/*! - * \brief A binary type - * - * \vprivate Vector component internal API - */ -class JBinType final { - public: - /*! - * \brief Constructs a binary type - * \vprivate Vector component internal API - * \param[in] b - * Bytes to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - */ - explicit JBinType(score::cpp::span b) noexcept : value_(b) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> score::cpp::span { return this->value_; } - - /*! - * \brief Returns the length - * \vprivate Vector component internal API - * - * \return The length. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } - - private: - /*! - * \brief Wrapped binary value - */ - score::cpp::span value_; +inline namespace types +{ +/// \brief A binary type +class JBinType final +{ + public: + /// \brief Constructs a binary type + /// \param[in] b Bytes to serialize. + explicit JBinType(score::cpp::span b) noexcept : value_(b) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> score::cpp::span + { + return this->value_; + } + + /// \brief Returns the length + /// \return The length. + auto GetLength() const noexcept -> std::uint32_t + { + return internal::ConvertLength(this->value_.size()); + } + + private: + /// \brief Wrapped binary value + score::cpp::span value_; }; -/*! - * \brief Serializes a binary value - * \vpublic - * - * \param[in] b - * Bytes to serialize. - * \return The serializable binary value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JBin(score::cpp::span b) noexcept -> JBinType { return JBinType{b}; } - -/*! - * \brief A binary string type - * - * \vprivate Vector component internal API - */ -class JBinStringType final { - public: - /*! - * \brief Constructs a binary string - * \vprivate Vector component internal API - * - * \param[in] s - * String to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JBinStringType(StringView s) noexcept : value_(s) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> StringView { return this->value_; } - - /*! - * \brief Returns the length - * \vprivate Vector component internal API - * - * \return The length. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } +/// \brief Serializes a binary value +/// \param[in] b Bytes to serialize. +/// \return The serializable binary value. +inline auto JBin(score::cpp::span b) noexcept -> JBinType +{ + return JBinType{b}; +} - private: - /*! - * \brief Wrapped string value - */ - StringView value_; +/// \brief A binary string type +class JBinStringType final +{ + public: + /// \brief Constructs a binary string + /// \param[in] s String to serialize. + constexpr explicit JBinStringType(StringView s) noexcept : value_(s) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> StringView + { + return this->value_; + } + + /// \brief Returns the length + /// \return The length. + auto GetLength() const noexcept -> std::uint32_t + { + return internal::ConvertLength(this->value_.size()); + } + + private: + /// \brief Wrapped string value + StringView value_; }; -/*! - * \brief Serializes a string as binary - * \vpublic - * - * \param[in] s - * The string to serialize. - * \return The serializable string value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr auto JBinString(StringView s) noexcept -> JBinStringType { return JBinStringType{s}; } - -/*! - * \brief Serializes a string as binary - * \vpublic - * - * \param[in] s - * The string to serialize. - * \return The serializable string value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JBinString(std::string const& s) noexcept -> JBinStringType { return JBinString(std::string_view{s}); } - -/*! - * \brief A binary key type - * - * \vprivate Vector component internal API - */ -class JBinKeyType final { - public: - /*! - * \brief Constructs a binary key - * \vprivate Vector component internal API - * - * \param[in] s - * Key to serialize. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \spec - * requires true; - * \endspec - */ - constexpr explicit JBinKeyType(StringView s) noexcept : value_(s) {} - - /*! - * \brief Returns the contained value - * \vprivate Vector component internal API - * - * \return The value. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetValue() const noexcept -> StringView { return this->value_; } +/// \brief Serializes a string as binary +/// \param[in] s The string to serialize. +/// \return The serializable string value. +constexpr auto JBinString(StringView s) noexcept -> JBinStringType +{ + return JBinStringType{s}; +} - /*! - * \brief Returns the length - * \vprivate Vector component internal API - * - * \return The length. - * \context ANY - * \pre - - * \threadsafe TRUE, for different this pointer - * \spec - * requires true; - * \endspec - */ - auto GetLength() const noexcept -> std::uint32_t { return internal::ConvertLength(this->value_.size()); } +/// \brief Serializes a string as binary +/// \param[in] s The string to serialize. +/// \return The serializable string value. +inline auto JBinString(const std::string& s) noexcept -> JBinStringType +{ + return JBinString(std::string_view{s}); +} - private: - /*! - * \brief Wrapped key value - */ - StringView value_; +/// \brief A binary key type +class JBinKeyType final +{ + public: + /// \brief Constructs a binary key + /// \param[in] s Key to serialize. + constexpr explicit JBinKeyType(StringView s) noexcept : value_(s) {} + + /// \brief Returns the contained value + /// \return The value. + auto GetValue() const noexcept -> StringView + { + return this->value_; + } + + /// \brief Returns the length + /// \return The length. + auto GetLength() const noexcept -> std::uint32_t + { + return internal::ConvertLength(this->value_.size()); + } + + private: + /// \brief Wrapped key value + StringView value_; }; -/*! - * \brief Serializes a key as binary - * \vpublic - * - * \param[in] s - * The key to serialize. - * \return The serializable key value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -constexpr auto JBinKey(StringView s) noexcept -> JBinKeyType { return JBinKeyType{s}; } +/// \brief Serializes a key as binary +/// \param[in] s The key to serialize. +/// \return The serializable key value. +constexpr auto JBinKey(StringView s) noexcept -> JBinKeyType +{ + return JBinKeyType{s}; +} -/*! - * \brief Serializes a key as binary - * \vpublic - * - * \param[in] s - * The key to serialize. - * \return The serializable key value. - * - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ -inline auto JBinKey(std::string const& s) noexcept -> JBinKeyType { return JBinKey(std::string_view{s}); } +/// \brief Serializes a key as binary +/// \param[in] s The key to serialize. +/// \return The serializable key value. +inline auto JBinKey(const std::string& s) noexcept -> JBinKeyType +{ + return JBinKey(std::string_view{s}); +} // clang-format off } // inline namespace types // clang-format off +} // namespace vajson } // namespace json -} // namespace amsr -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_BIN_TYPES_H_ +} // namespace score +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H diff --git a/score/json/internal/writer/vajson/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h index 967acdd6eb..ca973ec2ee 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -10,19 +10,13 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief A collection of serializers for objects. - * - * \details Provides serializers for homogeneous C++ pair-ranges and Object types. - * - *********************************************************************************************************************/ +/// \file +/// \brief A collection of serializers for objects. +/// \details Provides serializers for homogeneous C++ pair-ranges and Object types. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include #include @@ -31,187 +25,114 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace amsr { -namespace json { -inline namespace types { -/*! - * \brief A serializer type for predefined keys - * - * \vprivate Vector component internal API - */ -class DefaultKeySerializer { - public: - /*! - * \brief Call operator - * \vprivate Vector component internal API - * - * \tparam T - * Type of value. - * \param[in] value - * to serialize as a key. Must be convertible to a StringView type. - * \return The serializable key type. - * \context ANY - * \pre - - * \threadsafe FALSE - * \reentrant FALSE - * - * \spec - * requires true; - * \endspec - */ - template - auto operator()(T const& value) const noexcept -> JKeyType { - static_assert(std::is_constructible::value, "Keys must be convertible to a StringView"); - return JKeyType{std::string_view{value}}; - } +namespace score +{ +namespace json +{ +namespace vajson +{ +inline namespace types +{ +/// \brief A serializer type for predefined keys +class DefaultKeySerializer +{ + public: + /// \brief Call operator + /// \tparam T Type of value. + /// \param[in] value to serialize as a key. Must be convertible to a StringView type. + /// \return The serializable key type. + template + auto operator()(const T& value) const noexcept -> JKeyType + { + static_assert(std::is_constructible::value, "Keys must be convertible to a StringView"); + return JKeyType{std::string_view{value}}; + } }; -/*! - * \brief A serializer type for a JSON array from a homogeneous C++ pair-range - * \vprivate Vector component internal API - * \tparam Range - * Type of range to serialize. - * \tparam KeyFn - * Type of key function. - * \tparam ValueFn - * Type of value function. - */ +/// \brief A serializer type for a JSON array from a homogeneous C++ pair-range +/// \tparam Range Type of range to serialize. +/// \tparam KeyFn Type of key function. +/// \tparam ValueFn Type of value function. template -class PairRangeSerializer final { - public: - /*! - * \brief Constructs a PairRangeSerializer - * \vprivate Vector component internal API - * \tparam KeyFn1 - * Type of key function. - * \tparam ValueFn1 - * Type of value function. - * \param[in] range - * to serialize. - * \param[in] key_fn - * Function used to serialize single keys. - * \param[in] value_fn - * Function used to serialize single values. - * - * \context ANY - * \pre The passed functions & range do not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - */ - template - PairRangeSerializer(Range const& range, KeyFn1&& key_fn, ValueFn1&& value_fn) noexcept - : map_{range}, key_function_{std::forward(key_fn)}, value_function_{std::forward(value_fn)} {} +class PairRangeSerializer final +{ + public: + /// \brief Constructs a PairRangeSerializer + /// \tparam KeyFn1 Type of key function. + /// \tparam ValueFn1 Type of value function. + /// \param[in] range to serialize. + /// \param[in] key_fn Function used to serialize single keys. + /// \param[in] value_fn Function used to serialize single values. + /// \pre The passed functions & range do not throw any exceptions + template + PairRangeSerializer(const Range& range, KeyFn1&& key_fn, ValueFn1&& value_fn) noexcept + : map_{range}, key_function_{std::forward(key_fn)}, value_function_{std::forward(value_fn)} + { + } - /*! - * \brief Call operator - * \vprivate Vector component internal API - * \tparam KS - * Type of key serializer. - * \param[in] os - * Object serializer to write into. - * \return The serializer state after serializing the range. - * - * \context ANY - * \pre The functions contained in the class do not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - */ - template - auto operator()(KS os) const noexcept -> KS; + /// \brief Call operator + /// \tparam KS Type of key serializer. + /// \param[in] os Object serializer to write into. + /// \return The serializer state after serializing the range. + /// \pre The functions contained in the class do not throw any exceptions + template + auto operator()(KS os) const noexcept -> KS; - private: - /*! - * \brief Range instance to be serialized - */ - std::reference_wrapper map_; + private: + /// \brief Range instance to be serialized + std::reference_wrapper map_; - /*! - * \brief Function used to serialize single keys - */ - KeyFn key_function_; + /// \brief Function used to serialize single keys + KeyFn key_function_; - /*! - * \brief Function used to serialize single values - */ - ValueFn value_function_; + /// \brief Function used to serialize single values + ValueFn value_function_; }; -/*! - * \brief An Object type - * \vprivate Vector component internal API - * \tparam Fn - * Type of serializer function. - */ +/// \brief An Object type +/// \tparam Fn Type of serializer function. template -// VCA_VAJSON_MOLE_1298 -struct JObjectType final { - /*! - * \brief Function used to serialize the object - */ - Fn fn; +struct JObjectType final +{ + /// \brief Function used to serialize the object + Fn fn; }; -/*! - * \brief Serializes an object value - * \vpublic - * \tparam Fn - * Type of serializer function. Must take an ObjectStart&& and return the follow-up serializer. - * \param[in] fn - * Function used to serialize the object. - * \return The serializable object type. - * - * \context ANY - * \pre The passed function does not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes an object value +/// \tparam Fn Type of serializer function. Must take an ObjectStart&& and return the follow-up serializer. +/// \param[in] fn Function used to serialize the object. +/// \return The serializable object type. +/// \pre The passed function does not throw any exceptions template ::value>> -auto JObject(Fn&& fn) noexcept -> JObjectType { // VECTOR SL AutosarC++17_10-A13.3.1: MD_JSON_rvalue_ref - return {std::forward(fn)}; +auto JObject(Fn&& fn) noexcept -> JObjectType +{ // coverity[autosar_cpp14_a13_3_1_violation] + return {std::forward(fn)}; } -/*! - * \brief Serializes a homogeneous C++ pair-range (e.g. a map) as a JSON object - * \vpublic - * \tparam Range - * Type of range. - * \tparam KeyFn - * Type of key serializer function. Must take the range's key type and return a JSON type. - * \tparam ValueFn - * Type of value serializer function. Must take the range's value type and return a JSON type. - * \param[in] range - * instance to be serialized. - * \param[in] key_fn - * Function used to serialize single keys. - * \param[in] value_fn - * Function used to serialize single values. - * \return The serializable JSON object. - * - * \context ANY - * \pre The passed range & functions do not throw any exceptions - * \threadsafe FALSE - * \reentrant FALSE - * \synchronous - - * \spec - * requires true; - * \endspec - */ +/// \brief Serializes a homogeneous C++ pair-range (e.g. a map) as a JSON object +/// \tparam Range Type of range. +/// \tparam KeyFn Type of key serializer function. Must take the range's key type and return a JSON type. +/// \tparam ValueFn Type of value serializer function. Must take the range's value type and return a JSON type. +/// \param[in] range instance to be serialized. +/// \param[in] key_fn Function used to serialize single keys. +/// \param[in] value_fn Function used to serialize single values. +/// \return The serializable JSON object. +/// \pre The passed range & functions do not throw any exceptions template > -auto JObject(Range const& range, KeyFn&& key_fn = DefaultKeySerializer{}, +auto JObject(const Range& range, + KeyFn&& key_fn = DefaultKeySerializer{}, ValueFn&& value_fn = IdSerializer{}) noexcept - -> JObjectType> { - return { - PairRangeSerializer{range, std::forward(key_fn), std::forward(value_fn)}}; + -> JObjectType> +{ + return {PairRangeSerializer{ + range, std::forward(key_fn), std::forward(value_fn)}}; } // clang-format off } // inline namespace types // clang-format on +} // namespace vajson } // namespace json -} // namespace amsr +} // namespace score -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_H_ +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H diff --git a/score/json/internal/writer/vajson/writer/types/object_type_impl.h b/score/json/internal/writer/vajson/writer/types/object_type_impl.h index a287103dae..23ba6e423e 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type_impl.h +++ b/score/json/internal/writer/vajson/writer/types/object_type_impl.h @@ -10,52 +10,44 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -/*! \file - * \brief Implementation of methods for object type. - * - *********************************************************************************************************************/ +/// \file +/// \brief Implementation of methods for object type. -#ifndef LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ -#define LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ +#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H +#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H -/********************************************************************************************************************** - * INCLUDES - *********************************************************************************************************************/ #include #include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/object_type.h" -namespace amsr { -namespace json { +namespace score +{ +namespace json +{ +namespace vajson +{ -inline namespace types { -/*! - * \brief Serialize every key-value pair as a JSON key followed by a JSON value - * \vprivate Vector component internal API - * - * \spec - * requires true; - * \endspec - */ +inline namespace types +{ +/// \brief Serialize every key-value pair as a JSON key followed by a JSON value template template -auto PairRangeSerializer::operator()(KS os) const noexcept -> KS { - // VCA_VAJSON_THIS_DEREF - // VECTOR NCL MisraC++2023-11.6.1: MD_JSON_MISRAC++2023-11.6.1_range_based_initialization - for (auto const& pair : this->map_.get()) { - // VCA_VAJSON_THIS_DEREF - ObjectSerializerValue osv{std::move(os) << this->key_function_(pair.first)}; - // VCA_VAJSON_THIS_DEREF - os = std::move(osv) << this->value_function_(pair.second); - } - return os; +auto PairRangeSerializer::operator()(KS os) const noexcept -> KS +{ + for (const auto& pair : this->map_.get()) + { + ObjectSerializerValue osv{std::move(os) << this->key_function_(pair.first)}; + os = std::move(osv) << this->value_function_(pair.second); + } + return os; } // clang-format off } // namespace types // clang-format on +} // namespace vajson } // namespace json -} // namespace amsr -#endif // LIB_VAJSON_INCLUDE_AMSR_JSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H_ +} // namespace score +#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H From c6d1dee3a99b8f6262e3292983cdca1d931b763a Mon Sep 17 00:00:00 2001 From: visjui Date: Wed, 26 Aug 2026 16:33:11 +0200 Subject: [PATCH 09/21] Make functions single return --- .../writer/vajson/vajson_serialize.cpp | 20 ++++--- .../internal/writer/vajson/vajson_serialize.h | 60 ++++++++++++------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp index e3b786a2bb..c16dbc66e7 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -22,24 +22,28 @@ score::Result SerializeToStream(std::ostream& out_stream, const T& json_da { auto serializer = score::json::vajson::DocumentSerializer{std::ref(out_stream)}; static_cast(std::move(serializer) << json_data); + + // A default constructed Result already carries the success state. + score::Result result{}; if (out_stream.fail()) { - return score::Result{ + result = score::Result{ score::unexpect, score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")}; } - return {}; + + return result; } template score::Result SerializeToBuffer(const T& json_data) { std::ostringstream out_stream{}; - auto result = SerializeToStream(out_stream, json_data); - if (!result.has_value()) - { - return score::Unexpected{result.error()}; - } - return out_stream.str(); + + // and_then keeps a single exit point: the buffer is only extracted once serialization succeeded, + // and any error is forwarded unchanged. + return SerializeToStream(out_stream, json_data).and_then([&out_stream](auto&&...) -> score::Result { + return out_stream.str(); + }); } } // namespace namespace score diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h index 0ed14e9887..000e36ee82 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -50,24 +51,33 @@ auto SerializeNumber(score::json::vajson::GenericValueSerializer&& seriali const score::json::Number& value) noexcept -> typename score::json::vajson::GenericValueSerializer::Next { + // The serializer is consumed by whichever alternative matches, so the outcome is parked in an + // optional to keep a single exit point. Number::As() re-parses the value on every call, hence the + // chain stays an else-if: the alternatives must be probed lazily, in order. + std::optional::Next> serialized{}; + if (const auto unsigned_value = value.As(); unsigned_value.has_value()) { - return std::move(serializer) << score::json::vajson::JNumber(*unsigned_value); + serialized.emplace(std::move(serializer) << score::json::vajson::JNumber(*unsigned_value)); + } + else if (const auto signed_value = value.As(); signed_value.has_value()) + { + serialized.emplace(std::move(serializer) << score::json::vajson::JNumber(*signed_value)); } - if (const auto signed_value = value.As(); signed_value.has_value()) + else if (const auto float_value = value.As(); float_value.has_value()) { - return std::move(serializer) << score::json::vajson::JNumber(*signed_value); + serialized.emplace(std::move(serializer) << score::json::vajson::JNumber(*float_value)); } - if (const auto float_value = value.As(); float_value.has_value()) + else if (const auto double_value = value.As(); double_value.has_value()) { - return std::move(serializer) << score::json::vajson::JNumber(*float_value); + serialized.emplace(std::move(serializer) << score::json::vajson::JNumber(*double_value)); } - if (const auto double_value = value.As(); double_value.has_value()) + else { - return std::move(serializer) << score::json::vajson::JNumber(*double_value); + serialized.emplace(std::move(serializer) << score::json::vajson::JNull()); } - SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(false, "Unable to serialize score::json::Number with vaJSON adapter."); - return std::move(serializer) << score::json::vajson::JNull(); + + return *std::move(serialized); } template auto SerializeList(score::json::vajson::GenericValueSerializer&& serializer, @@ -104,31 +114,37 @@ auto SerializeValue(score::json::vajson::GenericValueSerializer&& serializ const score::json::Any& value) noexcept -> typename score::json::vajson::GenericValueSerializer::Next { + // See SerializeNumber() for why the outcome is parked in an optional rather than returned directly. + std::optional::Next> serialized{}; + if (const auto object = value.As(); object.has_value()) { - return SerializeObject(std::move(serializer), object->get()); + serialized.emplace(SerializeObject(std::move(serializer), object->get())); } - if (const auto list = value.As(); list.has_value()) + else if (const auto list = value.As(); list.has_value()) { - return SerializeList(std::move(serializer), list->get()); + serialized.emplace(SerializeList(std::move(serializer), list->get())); } - if (const auto string_value = value.As(); string_value.has_value()) + else if (const auto string_value = value.As(); string_value.has_value()) { - return std::move(serializer) << score::json::vajson::JString(string_value->get()); + serialized.emplace(std::move(serializer) << score::json::vajson::JString(string_value->get())); } - if (const auto null_value = value.As(); null_value.has_value()) + else if (const auto null_value = value.As(); null_value.has_value()) { score::cpp::ignore = null_value; - return std::move(serializer) << score::json::vajson::JNull(); + serialized.emplace(std::move(serializer) << score::json::vajson::JNull()); + } + else if (const auto number = value.As(); number.has_value()) + { + serialized.emplace(SerializeNumber(std::move(serializer), number->get())); } - if (const auto number = value.As(); number.has_value()) + else { - return SerializeNumber(std::move(serializer), number->get()); + const auto boolean = value.As(); + serialized.emplace(std::move(serializer) << score::json::vajson::JBool(*boolean)); } - const auto boolean = value.As(); - SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(boolean.has_value(), - "Unable to determine score::json::Any type for vaJSON serialization."); - return std::move(serializer) << score::json::vajson::JBool(*boolean); + + return *std::move(serialized); } } // namespace vajson } // namespace writer From 3a5bfcc8e1a1dc883b47b85439b9e2455b6b525e Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 14:37:19 +0200 Subject: [PATCH 10/21] RFC compliance for JSON serializer --- .../writer/vajson/vajson_serialize_test.cpp | 108 ++++++++++++++++++ .../structures/generic_value_serializer.h | 52 +++++++-- .../serializers/util/escaped_json_string.h | 44 ++++++- 3 files changed, 194 insertions(+), 10 deletions(-) diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index de705cd849..31b5bfca86 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -13,6 +13,13 @@ #include "score/json/internal/writer/vajson/vajson_serialize.h" #include + +#include +#include +#include +#include +#include +#include namespace score { namespace json @@ -45,6 +52,107 @@ TEST(VajsonSerializeTest, SerializesObjectKeysUsingStringComparisonAdaptor) ASSERT_TRUE(result.has_value()); EXPECT_EQ(*result, std::string{"{\"alpha\":\"a\",\"beta\":2}"}); } +// RFC 8259, section 7 does not allow characters in the range U+0000 to U+001F to appear unescaped in a string. +TEST(VajsonSerializeTest, EscapesControlCharactersWithoutShortEscapeSequence) +{ + Object object{}; + object["value"] = Any{std::string{"\x01\x0b\x1f"}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"value\":\"\\u0001\\u000b\\u001f\"}"}); +} +TEST(VajsonSerializeTest, EscapesNullCharacterInsideString) +{ + Object object{}; + object["value"] = Any{std::string{std::string_view{"a\0b", 3U}}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"value\":\"a\\u0000b\"}"}); +} +TEST(VajsonSerializeTest, EscapesControlCharactersInObjectKeys) +{ + Object object{}; + object[std::string{"a\x1e" "b"}] = Any{std::string{"v"}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"a\\u001eb\":\"v\"}"}); +} +TEST(VajsonSerializeTest, PrefersShortEscapeSequencesOverUnicodeEscapes) +{ + Object object{}; + object["value"] = Any{std::string{"\b\f\n\r\t"}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"value\":\"\\b\\f\\n\\r\\t\"}"}); +} +TEST(VajsonSerializeTest, EscapesEveryControlCharacterAndNothingElse) +{ + std::string value{}; + for (std::uint32_t character{0U}; character <= 0x7FU; ++character) + { + value.push_back(static_cast(character)); + } + Object object{}; + object["value"] = Any{value}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + + // No unescaped control character may survive in the output. + for (const char serialized : *result) + { + EXPECT_GE(std::char_traits::to_int_type(serialized), 0x20) + << "unescaped control character in serialized output"; + } + // The printable characters are written as they are, with only the quote and the backslash escaped. + EXPECT_NE(result->find("!\\\"#$%&'()*+,-./0123456789:;<=>?"), std::string::npos); + EXPECT_NE(result->find("[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f"), std::string::npos); +} +TEST(VajsonSerializeTest, PassesMultiByteUtf8CharactersThrough) +{ + Object object{}; + object["value"] = Any{std::string{"\u00e4\u20ac"}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"value\":\"\u00e4\u20ac\"}"}); +} +TEST(VajsonSerializeTest, SerializesFiniteDouble) +{ + Object object{}; + object["number"] = Any{double{1.5}}; + const auto result = VajsonToBuffer(object); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"{\"number\":1.5}"}); +} +// RFC 8259, section 6 has no representation for infinity or NaN, hence they cannot be serialized. +TEST(VajsonSerializeTest, RejectsInfiniteDouble) +{ + Object object{}; + object["number"] = Any{std::numeric_limits::infinity()}; + EXPECT_FALSE(VajsonToBuffer(object).has_value()); +} +TEST(VajsonSerializeTest, RejectsNegativeInfiniteFloat) +{ + Object object{}; + object["number"] = Any{-std::numeric_limits::infinity()}; + EXPECT_FALSE(VajsonToBuffer(object).has_value()); +} +TEST(VajsonSerializeTest, RejectsNotANumber) +{ + List list{}; + list.emplace_back(Any{std::numeric_limits::quiet_NaN()}); + EXPECT_FALSE(VajsonToBuffer(list).has_value()); +} +TEST(VajsonSerializeTest, RejectsNotANumberOnStream) +{ + Object object{}; + object["number"] = Any{std::numeric_limits::quiet_NaN()}; + std::ostringstream out_stream{}; + VajsonSerialize serializer{out_stream}; + const auto result = serializer << object; + EXPECT_FALSE(result.has_value()); + // The non-representable number itself must not have been written. + EXPECT_EQ(out_stream.str().find("nan"), std::string::npos); +} TEST(VajsonSerializeTest, SerializesTopLevelList) { List list{}; diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 389b91014f..6af52e385a 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -19,7 +19,10 @@ #include #include +#include +#include #include +#include #include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" @@ -104,6 +107,9 @@ class GenericValueSerializer final } /// \brief Serializes a number value + /// \details The JSON grammar of RFC 8259, section 6 only covers finite numbers, so infinity and NaN have no + /// representation. Such a value is not written at all, instead the output stream is put into the failed + /// state, which the enclosing serializer reports as an error to its caller. /// \tparam T Type of number. /// \param[in] number value to serialize. /// \return The succeeding serializer. @@ -112,16 +118,25 @@ class GenericValueSerializer final auto operator<<(JNumberType number) && noexcept -> Next { return this->Serialize([this, number]() noexcept { - // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety - std::array buffer{}; - T value = static_cast(number.GetValue()); + const T value = static_cast(number.GetValue()); - const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); + if (!IsFinite(value)) + { + this->os_.get().setstate(std::ios_base::failbit); + } + else + { + // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety + std::array buffer{}; - AssertCondition(conversion_result.ec == std::errc{}, - "GenericValueSerializer: Could not convert number to textual representation."); + const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); - this->os_.get().write(buffer.data(), static_cast(conversion_result.ptr - buffer.data())); + AssertCondition(conversion_result.ec == std::errc{}, + "GenericValueSerializer: Could not convert number to textual representation."); + + this->os_.get().write(buffer.data(), + static_cast(conversion_result.ptr - buffer.data())); + } }); } @@ -206,6 +221,29 @@ class GenericValueSerializer final auto operator<<(JObjectType object) && noexcept -> Next; private: + /// \brief Checks whether a number is representable as a JSON number + /// \details RFC 8259, section 6 only allows finite numbers. Only floating point values can be non-finite, + /// integral values are always representable. + /// \tparam T Type of number. + /// \param[in] value Number to check. + /// \return True if the value is finite, false otherwise. + template + static auto IsFinite(const T value) noexcept -> bool + { + bool is_finite{true}; + // Coverity doesn't know constexpr if statements + // coverity[autosar_cpp14_a7_1_8_violation] + if constexpr (std::is_floating_point::value) + { + is_finite = std::isfinite(value); + } + else + { + static_cast(value); + } + return is_finite; + } + /// \brief Serializes a value /// \details /// - If another element was serialized before: diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 8a00f7f410..5e4b9ff60b 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -16,6 +16,8 @@ #ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H #define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H +#include +#include #include #include #include @@ -61,12 +63,40 @@ class EscapedJsonString std::string_view value_; }; +/// \brief First character that RFC 8259, section 7 allows to appear unescaped in a JSON string +constexpr std::char_traits::int_type kFirstUnescapedCharacter{0x20}; + +/// \brief Writes a control character as a six character \uXXXX escape sequence +/// \details RFC 8259, section 7 requires characters in the range U+0000 to U+001F to be escaped. Those without a +/// two character escape sequence must be written in the \uXXXX notation. +/// \param[in] os Output stream to write into. +/// \param[in] value Value of the control character to escape, must be below kFirstUnescapedCharacter. +inline void WriteUnicodeEscape(std::ostream& os, const std::char_traits::int_type value) noexcept +{ + constexpr std::string_view kHexDigits{"0123456789abcdef"}; + constexpr std::char_traits::int_type kNibbleMask{0x0F}; + constexpr std::char_traits::int_type kNibbleWidth{4}; + constexpr std::size_t kUnicodeEscapeLength{6U}; + + // Only characters below U+0020 reach this function, hence the two upper hexadecimal digits are always zero. + const std::array escape{'\\', + 'u', + '0', + '0', + kHexDigits[static_cast((value >> kNibbleWidth) & kNibbleMask)], + kHexDigits[static_cast(value & kNibbleMask)]}; + os.write(escape.data(), static_cast(escape.size())); +} + // NOLINTNEXTLINE(whitespace/line_length) // coverity[autosar_cpp14_m5_0_16_violation] /// \brief Serializes an escaped string literal type /// \details -/// - If the string contains a character that needs to be escaped in JSON: +/// - If the string contains a character that has a two character escape sequence in JSON: /// - Serialize the escaped character. +/// - Otherwise, if the character is a control character (U+0000 to U+001F), which RFC 8259, section 7 does not +/// allow to appear unescaped: +/// - Serialize the character as a \uXXXX escape sequence. /// - Otherwise: /// - Serialize the character directly. /// \param[in] os Output stream to write into. @@ -76,7 +106,8 @@ auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> s { for (const char ch : string.GetValue()) { - switch (std::char_traits::to_int_type(ch)) + const auto value = std::char_traits::to_int_type(ch); + switch (value) { case std::char_traits::to_int_type('"'): { @@ -114,7 +145,14 @@ auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> s break; } default: - os.put(ch); + if (value < kFirstUnescapedCharacter) + { + WriteUnicodeEscape(os, value); + } + else + { + os.put(ch); + } break; } } From 60d52d77de2323c645aca13e153c4c4d53e63348 Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 15:14:00 +0200 Subject: [PATCH 11/21] Remove dead code --- .../writer/vajson/writer/serializers.h | 23 --- .../writer/vajson/writer/serializers/stl.h | 24 --- .../serializers/stl/associative_containers.h | 155 ------------------ .../writer/serializers/stl/primitives.h | 108 ------------ .../serializers/stl/sequence_containers.h | 80 --------- .../writer/vajson/writer/serializers/vac.h | 24 --- .../writer/serializers/vac/primitives.h | 78 --------- .../serializers/vac/sequence_containers.h | 80 --------- .../vajson/writer/serializers/vac/variant.h | 76 --------- .../writer/vajson/writer/types/object_type.h | 82 +-------- .../vajson/writer/types/object_type_impl.h | 53 ------ 11 files changed, 1 insertion(+), 782 deletions(-) delete mode 100644 score/json/internal/writer/vajson/writer/serializers.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/stl.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/stl/primitives.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/vac.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/vac/primitives.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h delete mode 100644 score/json/internal/writer/vajson/writer/serializers/vac/variant.h delete mode 100644 score/json/internal/writer/vajson/writer/types/object_type_impl.h diff --git a/score/json/internal/writer/vajson/writer/serializers.h b/score/json/internal/writer/vajson/writer/serializers.h deleted file mode 100644 index aa8b6b8676..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers.h +++ /dev/null @@ -1,23 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of all available serializers. -/// \details Provides serializers for native JSON, libVac & STL/STD types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H - -#include "score/json/internal/writer/vajson/writer/serializers/stl.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac.h" - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl.h b/score/json/internal/writer/vajson/writer/serializers/stl.h deleted file mode 100644 index aa3124071e..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/stl.h +++ /dev/null @@ -1,24 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for native STD & STL types. -/// \details Single include for all STD &STL types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H - -#include "score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h" -#include "score/json/internal/writer/vajson/writer/serializers/stl/primitives.h" -#include "score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h" - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h deleted file mode 100644 index 45a43675f6..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/stl/associative_containers.h +++ /dev/null @@ -1,155 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for associative STL containers. -/// \details Provides serializers for (unordered) set, (unordered) map, and (unordered) multimap types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H - -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" -#include -#include -#include -#include -#include - -namespace score -{ -namespace json -{ -namespace vajson -{ - -/// \brief Serializes a set of serializable elements -/// \tparam Next type of serializer. -/// \tparam Value Type of value. -/// \tparam Cmp Type of comparison function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] set Set to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::set& set) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JArray(set); -} - -/// \brief Serializes an unordered set of serializable elements -/// \tparam Next type of serializer. -/// \tparam Value Type of value. -/// \tparam Hash Type of hash function. -/// \tparam Pred Type of predicate function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] set Unordered set to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, - const std::unordered_set& set) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JArray(set); -} - -/// \brief Serializes a map of serializable elements -/// \tparam Next type of serializer. -/// \tparam Key Type of key. Must be convertible to a JKey. -/// \tparam Value Type of value. -/// \tparam Cmp Type of comparison function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] map Map to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::map& map) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JObject(map); -} - -/// \brief Serializes a map of serializable elements -/// \tparam Next type of serializer. -/// \tparam Key Type of key. Must be convertible to a JKey. -/// \tparam Value Type of value. -/// \tparam Cmp Type of comparison function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] map Map to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::map& map) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JObject(map); -} - -/// \brief Serializes an unordered map of serializable elements -/// \tparam Next type of serializer. -/// \tparam Key Type of key. Must be convertible to a JKey. -/// \tparam Value Type of value. -/// \tparam Hash Type of hash function. -/// \tparam Pred Type of predicate function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] map Unordered map to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, - const std::unordered_map& map) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JObject(map); -} - -/// \brief Serializes a multimap of serializable elements -/// \tparam Next type of serializer. -/// \tparam Key Type of key. Must be convertible to a JKey. -/// \tparam Value Type of value. -/// \tparam Cmp Type of comparison function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] map Multimap to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::multimap& map) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JObject(map); -} - -/// \brief Serializes an unordered multimap of serializable elements -/// \tparam Next type of serializer. -/// \tparam Key Type of key. Must be convertible to a JKey. -/// \tparam Value Type of value. -/// \tparam Hash Type of hash function. -/// \tparam Pred Type of predicate function. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] map Unordered multimap to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, - const std::unordered_multimap& map) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JObject(map); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_ASSOCIATIVE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h b/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h deleted file mode 100644 index eefbd13644..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/stl/primitives.h +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for primitive data types. -/// \details Provides serializers for pointer, bool, number, and string types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H - -#include -#include -#include -#include - -#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ -/// \brief Forward declaration for the GenericValueSerializer -/// \tparam Return type after using operator<<(). -template -class GenericValueSerializer; - -/// \brief Serializes a null value directly from a nullptr literal -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, std::nullptr_t) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JNull(); -} - -/// \brief Serializes a value directly from a pointer -/// \details -/// - If the given pointer is a nullptr: -/// - Serialize it as a null type. -/// - Otherwise: -/// - Serialize the pointer's value. -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] ptr The pointer whose value to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const T* ptr) noexcept -> - typename GenericValueSerializer::Next -{ - return (ptr == nullptr) ? (std::move(serializer) << JNull()) : (std::move(serializer) << *ptr); -} - -/// \brief Serializes a bool value directly -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] b Bool value to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, bool b) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JBool(b); -} - -/// \brief Serializes a number value directly -/// \tparam Next type of serializer. -/// \tparam N Type of number. -/// \param[in] serializer instance to write into. -/// \param[in] number to serialize. -/// \return The succeeding serializer. -template ::value>> -auto operator<<(GenericValueSerializer&& serializer, N number) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JNumber(number); -} - -/// \brief Serializes a string value directly -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] string to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::string& string) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JString(string); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_PRIMITIVES_H diff --git a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h deleted file mode 100644 index 24da3056f5..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/stl/sequence_containers.h +++ /dev/null @@ -1,80 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for STD sequence containers. -/// \details Provides serializers for std::array, std::vector, and std::deque types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H - -#include "score/json/internal/writer/vajson/writer/types/array_type.h" -#include -#include -#include -#include -#include - -namespace score -{ -namespace json -{ -namespace vajson -{ - -/// \brief Serializes an array of serializable elements -/// \tparam Next type of serializer. -/// \tparam Value Type of value. -/// \tparam N Size of the array. -/// \param[in] serializer instance to write into. -/// \param[in] array to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::array& array) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JArray(array); -} - -/// \brief Serializes a vector of serializable elements -/// \tparam Next type of serializer. -/// \tparam Value Type of value. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] vector to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::vector& vector) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JArray(vector); -} - -/// \brief Serializes a deque of serializable elements -/// \tparam Next type of serializer. -/// \tparam Value Type of value. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] deque to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::deque& deque) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JArray(deque); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STL_SEQUENCE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac.h b/score/json/internal/writer/vajson/writer/serializers/vac.h deleted file mode 100644 index bb52e16f74..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/vac.h +++ /dev/null @@ -1,24 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for native libVac types. -/// \details Single include for all libVac types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H - -#include "score/json/internal/writer/vajson/writer/serializers/vac/primitives.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h" -#include "score/json/internal/writer/vajson/writer/serializers/vac/variant.h" - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h b/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h deleted file mode 100644 index 011a5595fd..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/vac/primitives.h +++ /dev/null @@ -1,78 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for libVac primitive data types. -/// \details Provides serializers for std::string, std::string_view, and std::uint8_t types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H - -#include -#include -#include -#include - -#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ -/// \brief Forward declaration for the GenericValueSerializer -template -class GenericValueSerializer; - -/// \brief Serializes a string value directly -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] string to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, const std::string& string) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JString(string); -} - -/// \brief Serializes a string value directly -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] string to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, ::std::string_view string) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << JString(string); -} - -/// \brief Serializes a std::uint8_t value directly -/// \tparam Next type of serializer. -/// \param[in] serializer instance to write into. -/// \param[in] byte to serialize. -/// \return The succeeding serializer. -template -auto operator<<(GenericValueSerializer&& serializer, ::std::uint8_t byte) noexcept -> - typename GenericValueSerializer::Next -{ - return std::move(serializer) << static_cast(byte); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_PRIMITIVES_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h b/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h deleted file mode 100644 index d072d65104..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/vac/sequence_containers.h +++ /dev/null @@ -1,80 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief A collection of serializers for libVac sequence containers. -/// \details Provides serializers for score::cpp::span, score::cpp::static_vector types. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H - -#include - -#include "score/json/internal/writer/vajson/writer/types/array_type.h" - -#include "score/span.hpp" -#include "score/vector.hpp" - -#include "score/static_vector.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ - -/// \brief Serializes an array view of serializable elements -/// \tparam Serializer Type of serializer. -/// \tparam Value Type of value. -/// \param[in] serializer instance to write into. -/// \param[in] view to serialize. -/// \return The succeeding serializer. -template -auto operator<<(Serializer&& serializer, const ::score::cpp::span& view) noexcept -> typename Serializer::Next -{ - return std::forward(serializer) << JArray(view); -} - -/// \brief Serializes a vector of serializable elements -/// \tparam Serializer Type of serializer. -/// \tparam Value Type of value. -/// \tparam Alloc Vector allocator. -/// \param[in,out] serializer instance to write into. -/// \param[in] vector Data to serialize. -/// \return The succeeding serializer. -template -auto operator<<(Serializer&& serializer, const ::score::cpp::pmr::vector& vector) noexcept -> - typename Serializer::Next -{ - return std::forward(serializer) << JArray(vector); -} - -/// \brief Serializes a static vector of serializable elements -/// \tparam Serializer Type of serializer. -/// \tparam Value Type of value. -/// \tparam Alloc Type of allocator. -/// \param[in] serializer instance to write into. -/// \param[in] vector to serialize. -/// \return The succeeding serializer. -template -auto operator<<(Serializer&& serializer, const ::score::cpp::static_vector& vector) noexcept -> - typename Serializer::Next -{ - return std::forward(serializer) << JArray(vector); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_SEQUENCE_CONTAINERS_H diff --git a/score/json/internal/writer/vajson/writer/serializers/vac/variant.h b/score/json/internal/writer/vajson/writer/serializers/vac/variant.h deleted file mode 100644 index 89643eb238..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/vac/variant.h +++ /dev/null @@ -1,76 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief Provides a serializer for the score::cpp::variant type. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H - -#include - -#include "score/variant.hpp" -namespace score -{ -namespace json -{ -namespace vajson -{ -/// \brief A generic variant visitor -/// \details Serializes the children according to their implemented serializers. -/// \tparam Serializer Type of serializer. Must be noexcept move constructible and noexcept move assignable. -template -class VariantVisitor -{ - static_assert(std::is_nothrow_move_constructible::value, "Serializer must be noexcept movable"); - static_assert(std::is_nothrow_move_assignable::value, "Serializer must be noexcept movable"); - - public: - /// \brief Constructs the visitor - /// \details Visitor must only be used once. - /// \param[in] serializer to use. - explicit VariantVisitor(Serializer&& serializer) noexcept : serializer_(std::move(serializer)) {} - - /// \brief Call operator - /// \tparam T Type of the variant. - /// \param[in] value to serialize. - /// \return The succeeding serializer. - template - auto operator()(const T& value) noexcept -> typename Serializer::Next - { - return std::move(this->serializer_) << value; - } - - private: - /// \brief Serializer of the variant - Serializer serializer_; -}; - -/// \brief Serializes a variant of serializable elements -/// \tparam Serializer Type of serializer. -/// \tparam Types of values in the variant. -/// \param[in] s Serializer instance to write into. -/// \param[in] variant to serialize. -/// \return The succeeding serializer. -/// \pre All alternatives contained in the variant are noexcept movable -template -auto operator<<(Serializer s, score::cpp::variant& variant) noexcept -> typename Serializer::Next -{ - VariantVisitor ser{std::move(s)}; - return score::cpp::visit(std::move(ser), variant); -} - -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_VAC_VARIANT_H diff --git a/score/json/internal/writer/vajson/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h index ca973ec2ee..39df24c445 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -12,19 +12,14 @@ ********************************************************************************/ /// \file /// \brief A collection of serializers for objects. -/// \details Provides serializers for homogeneous C++ pair-ranges and Object types. +/// \details Provides serializers for Object types. #ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H #define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H -#include #include #include -#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/basic_types.h" - namespace score { namespace json @@ -33,62 +28,6 @@ namespace vajson { inline namespace types { -/// \brief A serializer type for predefined keys -class DefaultKeySerializer -{ - public: - /// \brief Call operator - /// \tparam T Type of value. - /// \param[in] value to serialize as a key. Must be convertible to a StringView type. - /// \return The serializable key type. - template - auto operator()(const T& value) const noexcept -> JKeyType - { - static_assert(std::is_constructible::value, "Keys must be convertible to a StringView"); - return JKeyType{std::string_view{value}}; - } -}; - -/// \brief A serializer type for a JSON array from a homogeneous C++ pair-range -/// \tparam Range Type of range to serialize. -/// \tparam KeyFn Type of key function. -/// \tparam ValueFn Type of value function. -template -class PairRangeSerializer final -{ - public: - /// \brief Constructs a PairRangeSerializer - /// \tparam KeyFn1 Type of key function. - /// \tparam ValueFn1 Type of value function. - /// \param[in] range to serialize. - /// \param[in] key_fn Function used to serialize single keys. - /// \param[in] value_fn Function used to serialize single values. - /// \pre The passed functions & range do not throw any exceptions - template - PairRangeSerializer(const Range& range, KeyFn1&& key_fn, ValueFn1&& value_fn) noexcept - : map_{range}, key_function_{std::forward(key_fn)}, value_function_{std::forward(value_fn)} - { - } - - /// \brief Call operator - /// \tparam KS Type of key serializer. - /// \param[in] os Object serializer to write into. - /// \return The serializer state after serializing the range. - /// \pre The functions contained in the class do not throw any exceptions - template - auto operator()(KS os) const noexcept -> KS; - - private: - /// \brief Range instance to be serialized - std::reference_wrapper map_; - - /// \brief Function used to serialize single keys - KeyFn key_function_; - - /// \brief Function used to serialize single values - ValueFn value_function_; -}; - /// \brief An Object type /// \tparam Fn Type of serializer function. template @@ -109,25 +48,6 @@ auto JObject(Fn&& fn) noexcept -> JObjectType return {std::forward(fn)}; } -/// \brief Serializes a homogeneous C++ pair-range (e.g. a map) as a JSON object -/// \tparam Range Type of range. -/// \tparam KeyFn Type of key serializer function. Must take the range's key type and return a JSON type. -/// \tparam ValueFn Type of value serializer function. Must take the range's value type and return a JSON type. -/// \param[in] range instance to be serialized. -/// \param[in] key_fn Function used to serialize single keys. -/// \param[in] value_fn Function used to serialize single values. -/// \return The serializable JSON object. -/// \pre The passed range & functions do not throw any exceptions -template > -auto JObject(const Range& range, - KeyFn&& key_fn = DefaultKeySerializer{}, - ValueFn&& value_fn = IdSerializer{}) noexcept - -> JObjectType> -{ - return {PairRangeSerializer{ - range, std::forward(key_fn), std::forward(value_fn)}}; -} - // clang-format off } // inline namespace types // clang-format on diff --git a/score/json/internal/writer/vajson/writer/types/object_type_impl.h b/score/json/internal/writer/vajson/writer/types/object_type_impl.h deleted file mode 100644 index 23ba6e423e..0000000000 --- a/score/json/internal/writer/vajson/writer/types/object_type_impl.h +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief Implementation of methods for object type. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H - -#include - -#include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ - -inline namespace types -{ -/// \brief Serialize every key-value pair as a JSON key followed by a JSON value -template -template -auto PairRangeSerializer::operator()(KS os) const noexcept -> KS -{ - for (const auto& pair : this->map_.get()) - { - ObjectSerializerValue osv{std::move(os) << this->key_function_(pair.first)}; - os = std::move(osv) << this->value_function_(pair.second); - } - return os; -} - -// clang-format off -} // namespace types -// clang-format on -} // namespace vajson -} // namespace json -} // namespace score -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_IMPL_H From 42976cc35bd7d773b184358b252cec5d497d9ac5 Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 15:27:16 +0200 Subject: [PATCH 12/21] Handle bool differently --- score/json/internal/writer/vajson/vajson_serialize.cpp | 4 +--- score/json/internal/writer/vajson/vajson_serialize.h | 10 ++++++++-- .../internal/writer/vajson/vajson_serialize_test.cpp | 9 +++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp index c16dbc66e7..55a510349f 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -27,9 +27,7 @@ score::Result SerializeToStream(std::ostream& out_stream, const T& json_da score::Result result{}; if (out_stream.fail()) { - result = score::Result{ - score::unexpect, - score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")}; + result = score::MakeUnexpected(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream"); } return result; diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h index 000e36ee82..36c8b5320a 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -138,11 +138,17 @@ auto SerializeValue(score::json::vajson::GenericValueSerializer&& serializ { serialized.emplace(SerializeNumber(std::move(serializer), number->get())); } - else + else if (const auto boolean = value.As(); boolean.has_value()) { - const auto boolean = value.As(); serialized.emplace(std::move(serializer) << score::json::vajson::JBool(*boolean)); } + else + { + // Any holds a bool, a Number, a std::string, a Null, an Object or a List, so every alternative has been + // probed by now and this branch cannot be reached. Writing null keeps the output valid JSON in case an + // alternative is added to Any without being handled here. + serialized.emplace(std::move(serializer) << score::json::vajson::JNull()); /* LCOV_EXCL_LINE */ + } return *std::move(serialized); } diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index 31b5bfca86..916e2c098d 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -153,6 +153,15 @@ TEST(VajsonSerializeTest, RejectsNotANumberOnStream) // The non-representable number itself must not have been written. EXPECT_EQ(out_stream.str().find("nan"), std::string::npos); } +TEST(VajsonSerializeTest, SerializesBothBooleanValues) +{ + List list{}; + list.emplace_back(Any{true}); + list.emplace_back(Any{false}); + const auto result = VajsonToBuffer(list); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(*result, std::string{"[true,false]"}); +} TEST(VajsonSerializeTest, SerializesTopLevelList) { List list{}; From ba1c8ff8a6e80e7ef639b4c6b5cca312e0c8f616 Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 16:12:59 +0200 Subject: [PATCH 13/21] Remove more dead code --- .../structures/generic_value_serializer.h | 38 ---- .../serializers/structures/key_serializer.h | 22 --- .../serializers/util/length_serializer.h | 56 ------ .../writer/vajson/writer/types/bin_types.h | 176 ------------------ 4 files changed, 292 deletions(-) delete mode 100644 score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h delete mode 100644 score/json/internal/writer/vajson/writer/types/bin_types.h diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 6af52e385a..041eb48a46 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -28,10 +28,8 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" #include "score/json/internal/writer/vajson/writer/types/array_type.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -#include "score/json/internal/writer/vajson/writer/types/bin_types.h" #include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace score @@ -156,24 +154,6 @@ class GenericValueSerializer final }); } - /// \brief Serializes a binary string value - /// \details - /// - Add a 's' to denote the following value as a string. - /// - Serialize the length of the string value as four bytes big endian. - /// - Write the string value. - /// \param[in] string value to serialize. - /// \return The succeeding serializer. - // coverity[autosar_cpp14_m9_3_3_violation] - auto operator<<(JBinStringType string) && noexcept -> Next - { - return this->Serialize([this, string]() noexcept { - this->os_.get().put('s'); - internal::SerializeLength(this->os_.get(), string.GetLength()); - const auto& value = string.GetValue(); - this->os_.get().write(value.data(), static_cast(value.size())); - }); - } - /// \brief Serializes a series of serializable values /// \details /// - Add an opening square bracket. @@ -194,24 +174,6 @@ class GenericValueSerializer final }); } - /// \brief Serializes a binary value - /// \details - /// - Add a 'b' to denote the following value as binary. - /// - Serialize the length of the binary value as four bytes big endian. - /// - Write the binary value. - /// \param[in] bin Value to serialize. - /// \return The succeeding serializer. - // coverity[autosar_cpp14_m9_3_3_violation] - auto operator<<(JBinType bin) && noexcept -> Next - { - return this->Serialize([this, bin]() noexcept { - this->os_.get().put('b'); - internal::SerializeLength(this->os_.get(), bin.GetLength()); - const auto& value = bin.GetValue(); - this->os_.get().write(value.data(), static_cast(value.size())); - }); - } - /// \brief Serializes an object /// \tparam Fn Type of serializer function. /// \param[in] object to serialize. diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h index af32fea9ac..0dc5b9f02c 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h @@ -19,10 +19,7 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" -#include "score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h" -#include "score/json/internal/writer/vajson/writer/types/array_type.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -#include "score/json/internal/writer/vajson/writer/types/object_type.h" namespace score { @@ -81,25 +78,6 @@ class KeySerializer final return Next(this->os_.get()); } - /// \brief Serializes a binary key - /// \details - /// - Add a comma, if necessary. - /// - Serialize the length of the key as four bytes big endian. - /// - Write the key. - /// \param[in] key to serialize. - /// \return The succeeding serializer. - auto operator<<(JBinKeyType key) const&& noexcept -> Next - { - this->WriteComma(); - - this->os_.get().put('k'); - internal::SerializeLength(this->os_.get(), key.GetLength()); - const auto value = key.GetValue(); - this->os_.get().write(value.data(), value.size()); - - return Next(this->os_.get()); - } - private: /// \brief Adds a comma to the stream, if necessary /// \details diff --git a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h b/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h deleted file mode 100644 index eb96fd0381..0000000000 --- a/score/json/internal/writer/vajson/writer/serializers/util/length_serializer.h +++ /dev/null @@ -1,56 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief Serializer for length tags. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H - -#include -#include - -#include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ -namespace internal -{ -/// \brief Serializes a length value as big endian -/// \details -/// - Copy each byte of the value to byte buffer, big-endian byte order, -/// - Write byte buffer to the stream. -/// \param[in] os Stream to write to. -/// \param[in] length to serialize. -inline void SerializeLength(WriterType os, const std::uint32_t length) noexcept -{ - constexpr std::size_t kPrefixSize{sizeof(std::uint32_t)}; - - std::array arr{}; - arr[0] = static_cast((length >> 24) & 0xffu); - arr[1] = static_cast((length >> 16) & 0xffu); - arr[2] = static_cast((length >> 8) & 0xffu); - arr[3] = static_cast(length & 0xffu); - - os.get().write(reinterpret_cast(arr.data()), kPrefixSize); -} - -} // namespace internal -} // namespace vajson -} // namespace json -} // namespace score - -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_LENGTH_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/types/bin_types.h b/score/json/internal/writer/vajson/writer/types/bin_types.h deleted file mode 100644 index 09223839d5..0000000000 --- a/score/json/internal/writer/vajson/writer/types/bin_types.h +++ /dev/null @@ -1,176 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2026 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -/// \file -/// \brief Implementation of methods for Bin type. -/// \details Provides serializers for arrays and tuples. - -#ifndef SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H -#define SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H - -#include -#include - -#include "score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h" -#include "score/json/internal/parser/vajson/vajson_impl/util/types.h" - -namespace score -{ -namespace json -{ -namespace vajson -{ -namespace internal -{ -/// \brief Safely convert a length -/// \param[in] length to convert. -/// \return The converted length. -inline auto ConvertLength(const std::size_t length) noexcept -> std::uint32_t -{ - AssertCondition(length <= std::numeric_limits::max(), "Length exceeds std::uint32_t range."); - - // coverity[autosar_cpp14_a4_7_1_violation] - // coverity[autosar_cpp14_m0_3_1_violation] - return static_cast(length); -} - -} // namespace internal - -inline namespace types -{ -/// \brief A binary type -class JBinType final -{ - public: - /// \brief Constructs a binary type - /// \param[in] b Bytes to serialize. - explicit JBinType(score::cpp::span b) noexcept : value_(b) {} - - /// \brief Returns the contained value - /// \return The value. - auto GetValue() const noexcept -> score::cpp::span - { - return this->value_; - } - - /// \brief Returns the length - /// \return The length. - auto GetLength() const noexcept -> std::uint32_t - { - return internal::ConvertLength(this->value_.size()); - } - - private: - /// \brief Wrapped binary value - score::cpp::span value_; -}; - -/// \brief Serializes a binary value -/// \param[in] b Bytes to serialize. -/// \return The serializable binary value. -inline auto JBin(score::cpp::span b) noexcept -> JBinType -{ - return JBinType{b}; -} - -/// \brief A binary string type -class JBinStringType final -{ - public: - /// \brief Constructs a binary string - /// \param[in] s String to serialize. - constexpr explicit JBinStringType(StringView s) noexcept : value_(s) {} - - /// \brief Returns the contained value - /// \return The value. - auto GetValue() const noexcept -> StringView - { - return this->value_; - } - - /// \brief Returns the length - /// \return The length. - auto GetLength() const noexcept -> std::uint32_t - { - return internal::ConvertLength(this->value_.size()); - } - - private: - /// \brief Wrapped string value - StringView value_; -}; - -/// \brief Serializes a string as binary -/// \param[in] s The string to serialize. -/// \return The serializable string value. -constexpr auto JBinString(StringView s) noexcept -> JBinStringType -{ - return JBinStringType{s}; -} - -/// \brief Serializes a string as binary -/// \param[in] s The string to serialize. -/// \return The serializable string value. -inline auto JBinString(const std::string& s) noexcept -> JBinStringType -{ - return JBinString(std::string_view{s}); -} - -/// \brief A binary key type -class JBinKeyType final -{ - public: - /// \brief Constructs a binary key - /// \param[in] s Key to serialize. - constexpr explicit JBinKeyType(StringView s) noexcept : value_(s) {} - - /// \brief Returns the contained value - /// \return The value. - auto GetValue() const noexcept -> StringView - { - return this->value_; - } - - /// \brief Returns the length - /// \return The length. - auto GetLength() const noexcept -> std::uint32_t - { - return internal::ConvertLength(this->value_.size()); - } - - private: - /// \brief Wrapped key value - StringView value_; -}; - -/// \brief Serializes a key as binary -/// \param[in] s The key to serialize. -/// \return The serializable key value. -constexpr auto JBinKey(StringView s) noexcept -> JBinKeyType -{ - return JBinKeyType{s}; -} - -/// \brief Serializes a key as binary -/// \param[in] s The key to serialize. -/// \return The serializable key value. -inline auto JBinKey(const std::string& s) noexcept -> JBinKeyType -{ - return JBinKey(std::string_view{s}); -} -// clang-format off -} // inline namespace types -// clang-format off -} // namespace vajson -} // namespace json -} // namespace score -#endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BIN_TYPES_H From 35394f52c8102641b12e9fd88642541506b96dba Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 16:13:07 +0200 Subject: [PATCH 14/21] Coverage scope fix --- score/json/internal/writer/vajson/BUILD | 6 +++++- tools/coverage/BUILD | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/score/json/internal/writer/vajson/BUILD b/score/json/internal/writer/vajson/BUILD index 1d5ecdbb6b..3107530b96 100644 --- a/score/json/internal/writer/vajson/BUILD +++ b/score/json/internal/writer/vajson/BUILD @@ -36,7 +36,11 @@ cc_library( srcs = ["vajson_backend.cpp"], features = COMPILER_WARNING_FEATURES, tags = ["FFI"], - visibility = ["@score_baselibs//score/json/internal/writer:__pkg__"], + visibility = [ + # Coverage scope root (//tools/coverage, eclipse-score/baselibs#512). + "//tools/coverage:__pkg__", + "@score_baselibs//score/json/internal/writer:__pkg__", + ], deps = [ ":vajson_serialize", "@score_baselibs//score/json/internal/writer:writer_backend", diff --git a/tools/coverage/BUILD b/tools/coverage/BUILD index 5fdf5ffd28..2470fd44e7 100644 --- a/tools/coverage/BUILD +++ b/tools/coverage/BUILD @@ -53,6 +53,10 @@ score_coverage_scope( "//score/json:interface", "//score/json:json_serializer", "//score/json/internal/parser/vajson:vajson_parser", + # Second writer backend: only reachable under the non-default + # //score/json:writer_library=vajson select; has a coverage grant. + # Pulls in its :vajson_serialize dependency. + "//score/json/internal/writer/vajson:vajson_backend", "//score/language/rust/memres", "//score/language/rust/stop_token", "//score/language/safecpp/aborts_upon_exception:abortsuponexception", From 42bcca80cf553868997a1b8568839dc1f7d09e11 Mon Sep 17 00:00:00 2001 From: visjui Date: Thu, 17 Sep 2026 16:18:41 +0200 Subject: [PATCH 15/21] Update README.md --- score/json/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/score/json/README.md b/score/json/README.md index 22e4af39d6..c7cf6134e3 100644 --- a/score/json/README.md +++ b/score/json/README.md @@ -447,7 +447,7 @@ Independently of the parser, the serialization backend is selected by the `write `json_serialize` (the default, a custom implementation) and `vajson` (the vector json library). The parser flag `base_library` has no influence on serialization. -bazel test --config=spp_host_clang //score/json/... --//platform/aas/lib/json:writer_library="vajson" +bazel test --config=spp_host_clang //score/json/... --//score/json:writer_library="vajson" Mind that the two backends differ in the representation they emit: `json_serialize` pretty-prints with a four space indentation, whereas `vajson` emits compact JSON without any insignificant whitespace between tokens. Both From d8be608e106ccbd8b7287172fc790083d4523c76 Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 08:33:11 +0200 Subject: [PATCH 16/21] Format --- .../internal/writer/vajson/vajson_serialize.cpp | 3 ++- .../writer/vajson/vajson_serialize_test.cpp | 4 +++- .../writer/serializers/util/escaped_json_string.h | 13 +++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp index 55a510349f..d70aabfe79 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -27,7 +27,8 @@ score::Result SerializeToStream(std::ostream& out_stream, const T& json_da score::Result result{}; if (out_stream.fail()) { - result = score::MakeUnexpected(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream"); + result = + score::MakeUnexpected(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream"); } return result; diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index 916e2c098d..43f25f80a7 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -72,7 +72,9 @@ TEST(VajsonSerializeTest, EscapesNullCharacterInsideString) TEST(VajsonSerializeTest, EscapesControlCharactersInObjectKeys) { Object object{}; - object[std::string{"a\x1e" "b"}] = Any{std::string{"v"}}; + object[std::string{ + "a\x1e" + "b"}] = Any{std::string{"v"}}; const auto result = VajsonToBuffer(object); ASSERT_TRUE(result.has_value()); EXPECT_EQ(*result, std::string{"{\"a\\u001eb\":\"v\"}"}); diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 5e4b9ff60b..776a0daaea 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -79,12 +79,13 @@ inline void WriteUnicodeEscape(std::ostream& os, const std::char_traits::i constexpr std::size_t kUnicodeEscapeLength{6U}; // Only characters below U+0020 reach this function, hence the two upper hexadecimal digits are always zero. - const std::array escape{'\\', - 'u', - '0', - '0', - kHexDigits[static_cast((value >> kNibbleWidth) & kNibbleMask)], - kHexDigits[static_cast(value & kNibbleMask)]}; + const std::array escape{ + '\\', + 'u', + '0', + '0', + kHexDigits[static_cast((value >> kNibbleWidth) & kNibbleMask)], + kHexDigits[static_cast(value & kNibbleMask)]}; os.write(escape.data(), static_cast(escape.size())); } From 9596c1a7289ba2b66be251a1b0e99846379638ed Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 09:24:10 +0200 Subject: [PATCH 17/21] Rewrite IsFinite --- .../structures/generic_value_serializer.h | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 041eb48a46..c3889f68b3 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -32,11 +32,7 @@ #include "score/json/internal/writer/vajson/writer/types/basic_types.h" #include "score/json/internal/writer/vajson/writer/types/object_type.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { /// \brief A serializer for JSON value types /// \tparam Return Type of the return value of a << operation. Must be one of the following types: - Unit: Serializer @@ -49,7 +45,7 @@ class GenericValueSerializer final /// \brief Type of the return value /// \details Set the type of the return value to be either its own type GenericValueSerializer (for arrays or a /// specified type) or the type specified by Return. - using Next = typename std::conditional_t::value, GenericValueSerializer, Return>; + using Next = typename std::conditional_t, GenericValueSerializer, Return>; /// \brief Constructs a GenericValueSerializer from an output stream /// \details Do not create an instance of GenericValueSerializer directly, use the aliases in @@ -190,20 +186,9 @@ class GenericValueSerializer final /// \param[in] value Number to check. /// \return True if the value is finite, false otherwise. template - static auto IsFinite(const T value) noexcept -> bool + static auto IsFinite(T const value) noexcept -> bool { - bool is_finite{true}; - // Coverity doesn't know constexpr if statements - // coverity[autosar_cpp14_a7_1_8_violation] - if constexpr (std::is_floating_point::value) - { - is_finite = std::isfinite(value); - } - else - { - static_cast(value); - } - return is_finite; + return !std::is_floating_point_v || std::isfinite(value); } /// \brief Serializes a value @@ -246,8 +231,6 @@ class GenericValueSerializer final SerializerState serializer_state_; }; -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_H From f747a97458b369c3a4271ac7e48cc40ad73be1ff Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 11:06:35 +0200 Subject: [PATCH 18/21] Collapse nested namespaces --- .../internal/writer/vajson/vajson_backend.cpp | 13 ++------ .../writer/vajson/vajson_serialize.cpp | 8 ++--- .../internal/writer/vajson/vajson_serialize.h | 32 +++++++------------ .../writer/vajson/vajson_serialize_test.cpp | 8 ++--- .../structures/generic_value_serializer.h | 2 +- .../generic_value_serializer_impl.h | 10 ++---- .../serializers/structures/key_serializer.h | 10 ++---- .../serializers/structures/serializer.h | 10 ++---- .../serializers/util/escaped_json_string.h | 13 ++------ .../writer/vajson/writer/types/array_type.h | 12 +++---- .../writer/vajson/writer/types/basic_types.h | 12 +++---- .../writer/vajson/writer/types/object_type.h | 10 ++---- 12 files changed, 38 insertions(+), 102 deletions(-) diff --git a/score/json/internal/writer/vajson/vajson_backend.cpp b/score/json/internal/writer/vajson/vajson_backend.cpp index 9ee08ed255..9fb50bedf3 100644 --- a/score/json/internal/writer/vajson/vajson_backend.cpp +++ b/score/json/internal/writer/vajson/vajson_backend.cpp @@ -27,13 +27,7 @@ score::Result SerializeToStreamInternal(std::ostream& out_stream, const T& } // namespace -namespace score -{ -namespace json -{ -namespace internal -{ -namespace writer +namespace score::json::internal::writer { score::Result SerializeToStream(std::ostream& out_stream, const score::json::Object& json_data) @@ -66,7 +60,4 @@ score::Result SerializeToBuffer(const score::json::Any& json_data) return score::json::VajsonToBuffer(json_data); } -} // namespace writer -} // namespace internal -} // namespace json -} // namespace score +} // namespace score::json::internal::writer diff --git a/score/json/internal/writer/vajson/vajson_serialize.cpp b/score/json/internal/writer/vajson/vajson_serialize.cpp index d70aabfe79..9bfc846445 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize.cpp @@ -45,9 +45,8 @@ score::Result SerializeToBuffer(const T& json_data) }); } } // namespace -namespace score -{ -namespace json + +namespace score::json { VajsonSerialize::VajsonSerialize(std::ostream& out_stream) noexcept : out_stream_{out_stream} {} score::Result VajsonSerialize::operator<<(const score::json::Object& json_data) @@ -74,5 +73,4 @@ score::Result VajsonToBuffer(const score::json::Any& json_data) { return SerializeToBuffer(json_data); } -} // namespace json -} // namespace score +} // namespace score::json diff --git a/score/json/internal/writer/vajson/vajson_serialize.h b/score/json/internal/writer/vajson/vajson_serialize.h index 36c8b5320a..b0a4d6865a 100644 --- a/score/json/internal/writer/vajson/vajson_serialize.h +++ b/score/json/internal/writer/vajson/vajson_serialize.h @@ -24,15 +24,11 @@ #include #include #include -namespace score -{ -namespace json -{ -namespace internal -{ -namespace writer + +namespace score::json { -namespace vajson + +namespace internal::writer::vajson { class ObjectKeySerializer final { @@ -152,9 +148,8 @@ auto SerializeValue(score::json::vajson::GenericValueSerializer&& serializ return *std::move(serialized); } -} // namespace vajson -} // namespace writer -} // namespace internal +} // namespace internal::writer::vajson + class VajsonSerialize final { public: @@ -174,13 +169,9 @@ class VajsonSerialize final score::Result VajsonToBuffer(const score::json::Object& json_data); score::Result VajsonToBuffer(const score::json::List& json_data); score::Result VajsonToBuffer(const score::json::Any& json_data); -} // namespace json -} // namespace score -namespace score -{ -namespace json -{ -namespace vajson +} // namespace score::json + +namespace score::json::vajson { template auto operator<<(GenericValueSerializer&& serializer, const score::json::Object& value) noexcept -> @@ -200,7 +191,6 @@ auto operator<<(GenericValueSerializer&& serializer, const score::json::An { return score::json::internal::writer::vajson::SerializeValue(std::move(serializer), value); } -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson + #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_VAJSON_SERIALIZE_H diff --git a/score/json/internal/writer/vajson/vajson_serialize_test.cpp b/score/json/internal/writer/vajson/vajson_serialize_test.cpp index 43f25f80a7..6f17efd9ce 100644 --- a/score/json/internal/writer/vajson/vajson_serialize_test.cpp +++ b/score/json/internal/writer/vajson/vajson_serialize_test.cpp @@ -20,9 +20,8 @@ #include #include #include -namespace score -{ -namespace json + +namespace score::json { namespace { @@ -174,5 +173,4 @@ TEST(VajsonSerializeTest, SerializesTopLevelList) EXPECT_EQ(*result, std::string{"[5,\"value\"]"}); } } // namespace -} // namespace json -} // namespace score +} // namespace score::json diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index c3889f68b3..3e89c04769 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -186,7 +186,7 @@ class GenericValueSerializer final /// \param[in] value Number to check. /// \return True if the value is finite, false otherwise. template - static auto IsFinite(T const value) noexcept -> bool + static auto IsFinite(const T value) noexcept -> bool { return !std::is_floating_point_v || std::isfinite(value); } diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h index d08b984f2e..7c3b14cc50 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer_impl.h @@ -19,11 +19,7 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h" #include "score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { /// \brief Serializes another object into the output stream /// \details @@ -48,8 +44,6 @@ auto GenericValueSerializer::operator<<(JObjectType object) && noexc }); } -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_GENERIC_VALUE_SERIALIZER_IMPL_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h index 0dc5b9f02c..25242342f4 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/key_serializer.h @@ -21,11 +21,7 @@ #include "score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { /// \brief A serializer for JSON keys /// \details This class only allows adding a key into the object and always returns a value serializer to only allow a @@ -97,8 +93,6 @@ class KeySerializer final SerializerState serializer_state_; }; -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_KEY_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h index 8da34b896f..45b3da2594 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/serializer.h @@ -20,11 +20,7 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { /// \brief State of the object to be serialized /// \details Indicates if the object is empty or not. Commas should only appended if the object is not empty. @@ -80,8 +76,6 @@ using ArrayStart = ArraySerializer; /// \brief Type of the output writer using WriterType = std::reference_wrapper; -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_STRUCTURES_SERIALIZER_H diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index 776a0daaea..e18341314c 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -27,13 +27,7 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace score -{ -namespace json -{ -namespace vajson -{ -namespace internal +namespace score::json::vajson::internal { /// \brief An escaped JSON string type class EscapedJsonString @@ -161,9 +155,6 @@ auto inline operator<<(std::ostream& os, EscapedJsonString string) noexcept -> s return os; } -} // namespace internal -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson::internal #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_SERIALIZERS_UTIL_ESCAPED_JSON_STRING_H diff --git a/score/json/internal/writer/vajson/writer/types/array_type.h b/score/json/internal/writer/vajson/writer/types/array_type.h index 1149e0f837..c29b727b3f 100644 --- a/score/json/internal/writer/vajson/writer/types/array_type.h +++ b/score/json/internal/writer/vajson/writer/types/array_type.h @@ -25,11 +25,7 @@ #include "score/json/internal/writer/vajson/writer/serializers/structures/serializer.h" #include "score/json/internal/writer/vajson/writer/types/basic_types.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { inline namespace types { @@ -109,8 +105,8 @@ auto JArray(const Range& range, Fn&& fn = IdSerializer{}) noexcept -> JAr // clang-format off } // inline namespace types // clang-format off -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson + + #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_ARRAY_TYPE_H diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h index df3bba9a8a..a4e8ba0f65 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -24,11 +24,7 @@ #include "score/json/internal/parser/vajson/vajson_impl/util/types.h" -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { inline namespace types { @@ -264,7 +260,7 @@ class IdSerializer // clang-format off } // namespace types // // clang-format on -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson + + #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_BASIC_TYPES_H diff --git a/score/json/internal/writer/vajson/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h index 39df24c445..e2c6b3688b 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -20,11 +20,7 @@ #include #include -namespace score -{ -namespace json -{ -namespace vajson +namespace score::json::vajson { inline namespace types { @@ -51,8 +47,6 @@ auto JObject(Fn&& fn) noexcept -> JObjectType // clang-format off } // inline namespace types // clang-format on -} // namespace vajson -} // namespace json -} // namespace score +} // namespace score::json::vajson #endif // SCORE_LIB_JSON_INTERNAL_WRITER_VAJSON_WRITER_TYPES_OBJECT_TYPE_H From 312785e0a6e65428b511796ae787437c6ee01da2 Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 11:23:18 +0200 Subject: [PATCH 19/21] Some clang-tidy findings --- .../serializers/util/escaped_json_string.h | 2 +- .../writer/vajson/writer/types/array_type.h | 2 +- .../writer/vajson/writer/types/basic_types.h | 16 ++++++++-------- .../writer/vajson/writer/types/object_type.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h index e18341314c..5680ba4b02 100644 --- a/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h +++ b/score/json/internal/writer/vajson/writer/serializers/util/escaped_json_string.h @@ -43,7 +43,7 @@ class EscapedJsonString /// \brief Returns the contained string /// \return The contained string. - auto GetValue() const noexcept -> std::string_view + [[nodiscard]] auto GetValue() const noexcept -> std::string_view { return this->value_; } diff --git a/score/json/internal/writer/vajson/writer/types/array_type.h b/score/json/internal/writer/vajson/writer/types/array_type.h index c29b727b3f..9df11a0e00 100644 --- a/score/json/internal/writer/vajson/writer/types/array_type.h +++ b/score/json/internal/writer/vajson/writer/types/array_type.h @@ -83,7 +83,7 @@ struct JArrayType final /// \param[in] fn Function used to serialize the tuple. /// \return A serializable Tuple type. /// \pre The passed function does not throw any exceptions -template ::value>> +template >> auto JArray(Fn&& fn) noexcept -> JArrayType { // coverity[autosar_cpp14_a13_3_1_violation] return {std::forward(fn)}; diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h index a4e8ba0f65..a89d74a9f2 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -65,7 +65,7 @@ class JKeyType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> std::string_view + [[nodiscard]] auto GetValue() const noexcept -> std::string_view { return this->value_; } @@ -109,7 +109,7 @@ constexpr auto operator""_key(const char* s, std::size_t size) noexcept -> JKeyT /// \brief A Number type /// \tparam N Type of number. -template ::value>::type> +template >> class JNumberType final { public: @@ -119,7 +119,7 @@ class JNumberType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> N + [[nodiscard]] auto GetValue() const noexcept -> N { return this->value_; } @@ -140,7 +140,7 @@ class JNumberType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> std::int32_t + [[nodiscard]] auto GetValue() const noexcept -> std::int32_t { return this->value_; } @@ -161,7 +161,7 @@ class JNumberType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> std::uint32_t + [[nodiscard]] auto GetValue() const noexcept -> std::uint32_t { return this->value_; } @@ -182,7 +182,7 @@ class JNumberType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> std::int32_t + [[nodiscard]] auto GetValue() const noexcept -> std::int32_t { return this->value_; } @@ -196,7 +196,7 @@ class JNumberType final /// \tparam N Type of number. /// \param[in] n The number to serialize. /// \return The serializable number type. -template ::value>::type> +template >> constexpr auto JNumber(N n) noexcept -> JNumberType { return JNumberType{n}; @@ -212,7 +212,7 @@ class JStringType final /// \brief Returns the contained value /// \return The value. - auto GetValue() const noexcept -> std::string_view + [[nodiscard]] auto GetValue() const noexcept -> std::string_view { return this->value_; } diff --git a/score/json/internal/writer/vajson/writer/types/object_type.h b/score/json/internal/writer/vajson/writer/types/object_type.h index e2c6b3688b..1d37711aae 100644 --- a/score/json/internal/writer/vajson/writer/types/object_type.h +++ b/score/json/internal/writer/vajson/writer/types/object_type.h @@ -38,7 +38,7 @@ struct JObjectType final /// \param[in] fn Function used to serialize the object. /// \return The serializable object type. /// \pre The passed function does not throw any exceptions -template ::value>> +template >> auto JObject(Fn&& fn) noexcept -> JObjectType { // coverity[autosar_cpp14_a13_3_1_violation] return {std::forward(fn)}; From 1ec271fcad564de7cb05bd65fc564762b7921ed0 Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 11:28:41 +0200 Subject: [PATCH 20/21] Revert unrelated changes --- score/json/detailed_design/class_diagram.puml | 35 ++++++------------- .../vajson/vajson_impl/reader/json_data.h | 2 +- .../vajson_impl/util/json_error_domain.h | 1 + .../parser/vajson/vajson_impl/util/number.h | 4 +-- tools/coverage/BUILD | 3 -- 5 files changed, 14 insertions(+), 31 deletions(-) diff --git a/score/json/detailed_design/class_diagram.puml b/score/json/detailed_design/class_diagram.puml index ed72f22e7b..01b5c65022 100644 --- a/score/json/detailed_design/class_diagram.puml +++ b/score/json/detailed_design/class_diagram.puml @@ -76,7 +76,6 @@ class score::json::JsonWriter { score::json::JsonWriter --> score::json::Error : use score::json::JsonWriter --> score::json::JsonSerialize : use -score::json::JsonWriter --> score::json::VajsonSerialize : use interface score::json::IJsonParser { @@ -110,35 +109,21 @@ score::json::JsonSerialize --> score::json::Error : use score::json::JsonSerialize --> score::Result : use -class score::json::VajsonSerialize #LightSteelBlue { - +VajsonSerialize(out_stream: std::ostream) - +operator<<(json_data: score::json::Object): score::Result - +operator<<(json_data: score::json::List): score::Result - +operator<<(json_data: score::json::Any): score::Result -} - -score::json::VajsonSerialize --> score::json::Any : use -score::json::VajsonSerialize --> score::json::Object : use -score::json::VajsonSerialize --> score::json::List : use -score::json::VajsonSerialize --> score::json::Error : use -score::json::VajsonSerialize --> score::Result : use - - -enum score::json::vajson::ParserState #LightSteelBlue +enum amsr::json::ParserState #LightSteelBlue -enum score::json::vajson::JsonErrc #LightSteelBlue +enum amsr::json::JsonErrc #LightSteelBlue -class score::json::vajson::ParserResult <> #LightSteelBlue +class amsr::json::ParserResult <> #LightSteelBlue -class score::json::vajson::v2::Parser #LightSteelBlue +class amsr::json::v2::Parser #LightSteelBlue -class score::json::vajson::JsonData #LightSteelBlue +class amsr::json::JsonData #LightSteelBlue -score::json::vajson::v2::Parser <|-- score::json::VajsonParser +amsr::json::v2::Parser <|-- score::json::VajsonParser class score::json::VajsonParser #LightSteelBlue { @@ -149,10 +134,10 @@ class score::json::VajsonParser #LightSteelBlue { score::json::VajsonParser *-- score::json::Any score::json::VajsonParser --> score::json::Error : use score::json::VajsonParser --> score::Result : use -score::json::VajsonParser --> score::json::vajson::ParserResult : use -score::json::VajsonParser --> score::json::vajson::ParserState : use -score::json::VajsonParser --> score::json::vajson::JsonErrc : use -score::json::VajsonParser --> score::json::vajson::JsonData : use +score::json::VajsonParser --> amsr::json::ParserResult : use +score::json::VajsonParser --> amsr::json::ParserState : use +score::json::VajsonParser --> amsr::json::JsonErrc : use +score::json::VajsonParser --> amsr::json::JsonData : use class score::json::NlohmannParser #LightSteelBlue { diff --git a/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h b/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h index bf9a1aa5ce..13b7f4c34a 100644 --- a/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h +++ b/score/json/internal/parser/vajson/vajson_impl/reader/json_data.h @@ -65,7 +65,7 @@ class JsonData final public: /// \brief Class JsonOps must have access to the InputStream but no other classes - friend class internal::JsonOps; + friend class internal::JsonOps; // VECTOR SL AutosarC++17_10-A11.3.1: MD_JSON_AutosarC++17_10-A11.3.1_json_ops /// \brief Initializes a JSON data object using a constructed reader /// \param[in] input_stream diff --git a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h index 5447c378d0..dd1222798d 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/json_error_domain.h @@ -281,6 +281,7 @@ inline auto MakeResult(bool value, score::result::Error error) noexcept -> score template inline auto MakeResult(Optional value, score::result::Error error) noexcept -> Result { + // VCA_VAJSON_EXTERNAL_CALL return value.has_value() ? Result{value.value()} : score::MakeUnexpected(error); } diff --git a/score/json/internal/parser/vajson/vajson_impl/util/number.h b/score/json/internal/parser/vajson/vajson_impl/util/number.h index 18b143203d..4d30a03cdc 100644 --- a/score/json/internal/parser/vajson/vajson_impl/util/number.h +++ b/score/json/internal/parser/vajson/vajson_impl/util/number.h @@ -58,11 +58,11 @@ using IsInt = std::conjunction>, std::is_int /// \brief Typedef for longlong /// \details Return type of std::strtoll and transformed to fixed-size type later. -using SignedLL = long long int; +using SignedLL = long long int; // VECTOR SL AutosarC++17_10-A3.9.1: MD_JSON_base_type /// \brief Typedef for unsigned longlong /// \details Return type of std::strtoull and transformed to fixed-size type later. -using UnsignedLL = unsigned long long int; +using UnsignedLL = unsigned long long int; // VECTOR SL AutosarC++17_10-A3.9.1: MD_JSON_base_type namespace util { diff --git a/tools/coverage/BUILD b/tools/coverage/BUILD index 2470fd44e7..d604bc602a 100644 --- a/tools/coverage/BUILD +++ b/tools/coverage/BUILD @@ -53,9 +53,6 @@ score_coverage_scope( "//score/json:interface", "//score/json:json_serializer", "//score/json/internal/parser/vajson:vajson_parser", - # Second writer backend: only reachable under the non-default - # //score/json:writer_library=vajson select; has a coverage grant. - # Pulls in its :vajson_serialize dependency. "//score/json/internal/writer/vajson:vajson_backend", "//score/language/rust/memres", "//score/language/rust/stop_token", From e2e15df423974436f1144dc5ebf963c6158b0dac Mon Sep 17 00:00:00 2001 From: visjui Date: Fri, 18 Sep 2026 14:19:40 +0200 Subject: [PATCH 21/21] Remove more dead code --- .../structures/generic_value_serializer.h | 2 +- .../writer/vajson/writer/types/basic_types.h | 70 ++----------------- 2 files changed, 6 insertions(+), 66 deletions(-) diff --git a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h index 3e89c04769..b98a2932a4 100644 --- a/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h +++ b/score/json/internal/writer/vajson/writer/serializers/structures/generic_value_serializer.h @@ -112,7 +112,7 @@ class GenericValueSerializer final auto operator<<(JNumberType number) && noexcept -> Next { return this->Serialize([this, number]() noexcept { - const T value = static_cast(number.GetValue()); + const T value = number.GetValue(); if (!IsFinite(value)) { diff --git a/score/json/internal/writer/vajson/writer/types/basic_types.h b/score/json/internal/writer/vajson/writer/types/basic_types.h index a89d74a9f2..718ea17cca 100644 --- a/score/json/internal/writer/vajson/writer/types/basic_types.h +++ b/score/json/internal/writer/vajson/writer/types/basic_types.h @@ -108,8 +108,10 @@ constexpr auto operator""_key(const char* s, std::size_t size) noexcept -> JKeyT // clang-format on /// \brief A Number type +/// \details bool is excluded: it is a JSON boolean, not a JSON number, and has no std::to_chars overload. +/// Use JBool instead. /// \tparam N Type of number. -template >> +template && !std::is_same_v>> class JNumberType final { public: @@ -129,74 +131,12 @@ class JNumberType final N value_; }; -/// \brief A char Number type -template <> -class JNumberType final -{ - public: - /// \brief Constructs a Number type - /// \param[in] num Number to serialize. - constexpr explicit JNumberType(char num) noexcept : value_(std::char_traits::to_int_type(num)) {} - - /// \brief Returns the contained value - /// \return The value. - [[nodiscard]] auto GetValue() const noexcept -> std::int32_t - { - return this->value_; - } - - private: - /// \brief Wrapped number value - std::int32_t value_; -}; - -/// \brief A std::uint8_t Number type -template <> -class JNumberType final -{ - public: - /// \brief Constructs a Number type - /// \param[in] num Number to serialize. - constexpr explicit JNumberType(std::uint8_t num) noexcept : value_(static_cast(num)) {} - - /// \brief Returns the contained value - /// \return The value. - [[nodiscard]] auto GetValue() const noexcept -> std::uint32_t - { - return this->value_; - } - - private: - /// \brief Wrapped number value - std::uint32_t value_; -}; - -/// \brief A std::int8_t Number type -template <> -class JNumberType final -{ - public: - /// \brief Constructs a Number type - /// \param[in] num Number to serialize. - constexpr explicit JNumberType(std::int8_t num) noexcept : value_(static_cast(num)) {} - - /// \brief Returns the contained value - /// \return The value. - [[nodiscard]] auto GetValue() const noexcept -> std::int32_t - { - return this->value_; - } - - private: - /// \brief Wrapped number value - std::int32_t value_; -}; - /// \brief Serializes a Number value +/// \details bool is excluded: it is a JSON boolean, not a JSON number. Use JBool instead. /// \tparam N Type of number. /// \param[in] n The number to serialize. /// \return The serializable number type. -template >> +template && !std::is_same_v>> constexpr auto JNumber(N n) noexcept -> JNumberType { return JNumberType{n};