Add vajson as second serializer backend - #520
JuriSchroeder wants to merge 19 commits into
Conversation
This reverts commit ca5628f.
|
Documentation preview for this pull request is available at: |
… into vajson-serializer
|
@mihajlo-k, can you please review this PR? |
mihajlo-k
left a comment
There was a problem hiding this comment.
I left a couple of comments. please note that this is a partial review, and I might post more comments.
| std::array<char, 64> buffer{}; | ||
| T value = static_cast<T>(number.GetValue()); | ||
|
|
||
| const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); |
There was a problem hiding this comment.
According to RFC8259: https://www.rfc-editor.org/info/rfc8259/#section-6, infinities, NaNs or similar are not permitted. However, we don't guard against those values here. If we were to pass e.g. std::numeric_limits<double>::infinity() to the std::to_chars it will write (probably) a string "inf" into the buffer.
maybe guard it with std::isfinite?
There was a problem hiding this comment.
I added a check for that.
| { | ||
| for (const char ch : string.GetValue()) | ||
| { | ||
| switch (std::char_traits<char>::to_int_type(ch)) |
There was a problem hiding this comment.
According to RFC8259: https://www.rfc-editor.org/info/rfc8259/#section-7 we should also escape control characters (U+0000 through U+001F).
| /// \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 <typename Next, typename Key, typename Value, typename Cmp, typename Alloc> | ||
| auto operator<<(GenericValueSerializer<Next>&& serializer, const std::map<Key, Value, Cmp, Alloc>& map) noexcept -> | ||
| typename GenericValueSerializer<Next>::Next | ||
| { | ||
| return std::move(serializer) << JObject(map); | ||
| } |
There was a problem hiding this comment.
duplicate. please remove one
There was a problem hiding this comment.
so, actually this should not compile. and now when I look at it, it seems it is indeed not compiled anywhere in the code. it's only included in score/json/internal/writer/vajson/writer/serializers.h but serializers.h itself is not included anywhere. @JuriSchroeder please check if this chunk of code is needed at all, and remove if not.
There was a problem hiding this comment.
True. This was dead code because the adapter I built from S-Core to vaJSon, never actually uses these types. There was some more dead code I removed as well.
mihajlo-k
left a comment
There was a problem hiding this comment.
Generally a very good addition with a lot of improvements to existing code. However, there might be a couple of unnecessary additions, and a few potential improvements.
Additionally, please remove any commits that are out of this scope from the PR. e.g. Remove amsr namespace; Remove VCA annotations
There was a problem hiding this comment.
Are these types used in VajsonSerialize? are they even related to JSON serialization?
There was a problem hiding this comment.
Removed. (This was related to an extension, but since binary parsing is also removed it should also be removed from the serializer.)
There was a problem hiding this comment.
Don't forget to add tests metadata to the tests (RecordProperty(...)). see some existing tests for this
| return this->Serialize([this, number]() noexcept { | ||
| // Buffer size: max 24 chars for double, ~20 for int64, extra space for safety | ||
| std::array<char, 64> buffer{}; | ||
| T value = static_cast<T>(number.GetValue()); |
There was a problem hiding this comment.
what is the point of JNumberType? to me it seemed that it is "widening" the underlying type, but in this line it's narrowed back again.
| else | ||
| { | ||
| const auto boolean = value.As<bool>(); | ||
| serialized.emplace(std::move(serializer) << score::json::vajson::JBool(*boolean)); |
There was a problem hiding this comment.
I know we currently exhaust all other possibilities before we go to bool, but still I think that we shouldn't simply dereference a Result type before checking it's validity.
| result = score::Result<void>{ | ||
| score::unexpect, | ||
| score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")}; |
There was a problem hiding this comment.
this can be simplified
| result = score::Result<void>{ | |
| score::unexpect, | |
| score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")}; | |
| result = MakeUnexpected(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream"); |
| `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" |
There was a problem hiding this comment.
| 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" |
There was a problem hiding this comment.
ClangTidy found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
Adds vajson as a second json serialization backend. It's currently selectable by a CMake switch, but could replace the json_serializer implementation in the future. (See also https://github.com/orgs/eclipse-score/discussions/2390#discussioncomment-18160655)