Skip to content

v.0.2.11

Compare
Choose a tag to compare
@eao197 eao197 released this 16 Oct 06:28
· 69 commits to master since this release
58bdc34

The v.0.2.11 solves an issue of using custom Reader_Writer with the content of containers. For example:

// A custom Reader_Writer for (de)serializing std::uint32_t values.
struct my_uint_formatter {
  void read(std::uint32_t & v, ...) const {...}
  void write(const std::uint32_t & v, ...) const {...}
};

struct my_data {
  std::uint32_t single_value_;
  std::vector<std::uint32_t> several_values_;
  std::optional< std::vector<std::uint32_t> > optional_values_;

  template<typename Io> void json_io(Io & io) {
    io & json_dto::mandatory(
           // Simple usage of formatter for single uint32.
           my_uint_formatter{},
           "single", single_value_)
       & json_dto::mandatory(
           // Custom formatter should be applied for every item.
           json_dto::apply_to_content_t<my_uint_formatter>{},
           "several", several_values_)
       & json_dto::mandatory(
           // The first occurrence of apply_to_content is for std::optional.
           json_dto::apply_to_content_t<
             // The second occurrence of apply_to_content is for std::vector.
             json_dto::apply_to_content_t<
               my_uint_formatter
             >
           >{}
       ;
  }
};

Another new feature is the two new proxy types mutable_map_key_t and const_map_key_t that are used by json-dto for (de)serialization of keys in map-like structures (std::map, std::multimap, std::unordered_map and so on). It makes possible to overload read_json_value/write_json_value functions for a custom (de)serialization of non-string keys. For example:

// New overloads should be placed into json_dto namespace.
namespace json_dto {

void read_json_value(
   mutable_map_key_t<int> key,
   const rapidjson::Value & from)
{
  ... // Reading an int key from a string representation.
}

void write_json_value(
  const_map_key_t<int> key,
  rapidjson::Value & to,
  rapidjson::MemoryPoolAllocator<> & allocator)
{
  ... // Storing an int key as a string.
}

} // namespace json_dto

// Now we can use ints as keys in map-like structures.
struct my_data {
  std::map<int, int> ints_to_ints_;
  std::multimap<int, float> ints_to_floats_;
  ...
  template<typename Io> void json_io(Io & io) {
    io & json_dto::mandatory("ints_to_ints", ints_to_ints_)
       & json_dto::mandatory("ints_to_floats", ints_to_floats_)
       ...
       ;
  }
};

There are two new examples that show new features in the action: one, two.