Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat formats: automatic serialization/deserialization based on Boost.PFR #472

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

linuxnyasha
Copy link
Contributor

Example

#include <userver/formats/json.hpp>
#include <userver/formats/universal/universal.hpp>
#include <userver/formats/universal/common_containers.hpp>
#include <iostream>


struct SomeStruct {
  std::optional<int> field;
  std::string field2;
};

template <>
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct> =
  SerializationConfig<SomeStruct>()
  .With<"field">({.Maximum = 1221, .Minimum = 12, .Default = 124})
  .With<"field2">({.Pattern = kPattern<"^[0-9]+$">});

int main() {
  userver::formats::json::ValueBuilder builder(SomeStruct{{}, "121"});
  auto json = builder.ExtractValue();
  std::cout << userver::formats::json::ToString(json) << std::endl; // {"field":124,"field2":"121"}
  std::cout << json.As<SomeStruct>().field << std::endl; // 124
};

universal/include/userver/formats/universal/universal.hpp Outdated Show resolved Hide resolved
universal/include/userver/formats/parse/try_parse.hpp Outdated Show resolved Hide resolved
universal/include/userver/formats/parse/try_parse.hpp Outdated Show resolved Hide resolved
universal/include/userver/formats/parse/try_parse.hpp Outdated Show resolved Hide resolved
template <>
struct FieldConfig<int> {
std::optional<Max<int>> Maximum;
std::optional<Min<int>> Minimum;
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes it's desired to have a JSON field with a C++-reserved name, e.g. "default". In this case we should choose an alternative field name, e.g. default_value, and override the JSON name for the field. For this we need std::optional<std::string_view> Name for every single-field FieldConfig. Not applicable to maps with their Additional

@linuxnyasha linuxnyasha force-pushed the feature_reflection_serialize_2 branch from 5703876 to c3323b9 Compare March 11, 2024 19:33
@Anton3
Copy link
Member

Anton3 commented Mar 12, 2024

In a separate .hpp, let's add an easy way to plug utils::TrivialBiMap into serialization of optionals

It should allow to write something like:

template <>
inline constexpr auto formats::enums::kEnumMapping<SomeEnum> = [](auto selector) {
  return selector
    .Case(SomeEnum::kFoo, "foo")
    .Case(SomeEnum::kBar, "bar");
};

and get:

  • Parse
  • Serialize
  • formats::enums::ToString (although it won't be ADL-found, but still useful)
  • formats::enums::ParseFromString? (same ^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants