Json Dto Versions Save

A small header-only library for converting data between json representation and c++ structs

v.0.3.3

4 months ago

This release contains a solution for #19 in the form of new functions inside a new json_dto::inside_array namespace.

This release also contains a fix for #23.

v.0.3.2

4 months ago

This is a bug-fix release. It fixes #20.

v.0.3.1

2 years ago

Support for std::int8_t and std::uint8_t added.

v.0.3.0

2 years ago

Function templates set_attr_null_value are replaced by default_on_null.

Implementations of manopt_policy traits now have to implement method on_null that is being called when null value is found during deserialization. This change affects custom implementations of manopt_policy traits.

New binder mandatory_with_null_as_default added.

More details can be found in the corresponding section of README file.

v.0.2.15

2 years ago

This is just a maintenance release.

Compilation error with G++11 and C++20 fixed.

v.0.2.14

2 years ago

Adds an overload of to_stream that accepts pretty_writer_params_t (fixes #16).

v.0.2.13

2 years ago

New overload for json_dto::to_json that accepts parameters for RapidJSON's PrettyWritter:

my_data my_obj;
...
// Make default serialization, without pretty-writer:
std::string my_obj_image = json_dto::to_json(my_obj);

// Make serialization with pretty-writer:
std::string my_obj_pretty_image = json_dto::to_json(my_obj,
		json_dto::pretty_writer_params_t{}
				.indent_char(' ')
				.indent_char_count(3u)
				.format_options(rapidjson::kFormatSingleLineArray));

v.0.2.12

3 years ago

This version allows passing const- and rvalue references to functions mandatory, optional, optional_no_default, optional_null.

The template class json_dto::binder_t was refactored and now it uses several customization points (binder_data_holder_t, binder_read_from_implementation_t, binder_write_to_implementation_t). Those customization points can be used for solving tasks like described in #11. See more in the corresponding README section.

There are several new examples that show the new functionality: one, two, three.

v.0.2.11

3 years ago

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.

v.0.2.10

3 years ago

A new way of custom IO has been introduced via user-supplied Reader_Writer types and new overloads of mandatory, optional, and optional_no_default functions. See the corresponding README section.

New overload for write_json_value function:

void write_json_value(
	const rapidjson::Value::StringRefType & s,
	rapidjson::Value & object,
	rapidjson::MemoryPoolAllocator<> & allocator );