Skip to content

Add vajson as second serializer backend - #520

Open
JuriSchroeder wants to merge 19 commits into
eclipse-score:mainfrom
JuriSchroeder:vajson-serializer
Open

JuriSchroeder wants to merge 19 commits into
eclipse-score:mainfrom
JuriSchroeder:vajson-serializer

Conversation

@JuriSchroeder

Copy link
Copy Markdown
Contributor

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)

@github-project-automation github-project-automation Bot moved this to In Progress in BAS - Baselibs FT Aug 26, 2026
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 26, 2026 12:12 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 26, 2026 12:12 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 26, 2026 12:12 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 26, 2026 12:12 — with GitHub Actions Active
@github-actions github-actions Bot added comp-json Related to score/json component c++ C++ code bazel Bazel and Starlark build files labels Aug 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview for this pull request is available at:
pr-520: https://eclipse-score.github.io/baselibs/pr-520/

@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 27, 2026 06:36 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 27, 2026 06:36 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 27, 2026 06:36 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder deployed to workflow-approval August 27, 2026 06:36 — with GitHub Actions Active
@JuriSchroeder
JuriSchroeder marked this pull request as ready for review August 27, 2026 08:34
@4og
4og requested a review from mihajlo-k September 1, 2026 12:26
@4og

4og commented Sep 1, 2026

Copy link
Copy Markdown
Member

@mihajlo-k, can you please review this PR?

@mihajlo-k mihajlo-k left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check for that.

{
for (const char ch : string.GetValue())
{
switch (std::char_traits<char>::to_int_type(ch))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to RFC8259: https://www.rfc-editor.org/info/rfc8259/#section-7 we should also escape control characters (U+0000 through U+001F).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +83 to +97
/// \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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate. please remove one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 mihajlo-k left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these types used in VajsonSerialize? are they even related to JSON serialization?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. (This was related to an extension, but since binary parsing is also removed it should also be removed from the serializer.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +30 to +32
result = score::Result<void>{
score::unexpect,
score::json::MakeError(score::json::Error::kUnknownError, "vaJSON serializer failed to write to stream")};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified

Suggested change
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");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread score/json/README.md Outdated
`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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClangTidy found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel Bazel and Starlark build files c++ C++ code comp-json Related to score/json component

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

4 participants