Coder Social home page Coder Social logo

stiffstream / json_dto Goto Github PK

View Code? Open in Web Editor NEW
141.0 12.0 17.0 373 KB

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

License: BSD 3-Clause "New" or "Revised" License

CMake 2.73% Ruby 4.28% Batchfile 0.03% C++ 92.96%
cpp cpp14 cplusplus json dto rapidjson linux windows

json_dto's Issues

NaN values

Hi How would one go about dealing with JSON that has optional fields which is a double which can also have "Nan" values

Basically everything works as expected utill i get a JSON value with a Nan. Any help much appreciated!

Working:
{"id":234, "test": 4.0}

Working :
{"id":235}

Not Working:
{"id":234, "test": "Nan"}

Customizing behaviour on incorrect type in JSON

I'd like to be able to customize what happens when deserializing a value that is defined in the JSON, but that has the wrong type. As an example, the following JSON:

{"x":"abc"}

and deserializing like this:

struct X {
    int x;

    template <typename io_type>
    void json_io(io_type& io)
    {
        json_dto::optional("x", x, 0);
    }
};

This leads to an exception because we are expecting a number, but are getting a string instead. It'd be nice if we could set a policy that we use the default value in that case.

nullable_t fails for containers with null values

#include <json_dto/pub.hpp>
#include <string>
#include <vector>

struct nullable {
    std::vector<json_dto::nullable_t<std::string>> strings;

    template <typename io_type>
    void json_io(io_type& io) {
        io& json_dto::optional_no_default("strings", strings);
    }
};

int main() {
    std::string serialized1{R"({"strings":[null]})"};
    json_dto::from_json<nullable>(serialized1);
}

Unless I'm mistaken, this should work. The values inside the vector are nullable, so it should be allowed to deserialize a JSON null. It fails with an exception, though.

CMake error, if version less than 3.8

If I want to create the json_dto library with a CMake version lesser than 3.8 I get the following error:

CMake Error at json_dto/CMakeLists.txt:13 (set_target_properties):
INTERFACE_LIBRARY targets may only have whitelisted properties. The
property "CXX_STANDARD" is not allowed.

CMake Error at json_dto/CMakeLists.txt:13 (set_target_properties):
INTERFACE_LIBRARY targets may only have whitelisted properties. The
property "CXX_STANDARD_REQUIRED" is not allowed.

CMake Error at json_dto/CMakeLists.txt:13 (set_target_properties):
INTERFACE_LIBRARY targets may only have whitelisted properties. The
property "CXX_EXTENSIONS" is not allowed.

arduino ESP8266

Hello there

Would this work for an esp8266 /NodeMCU ?

I would like to try this to serialize an array of struct and back again.
I'd be happy to experiment and feedback.

Thank you
Den

Serializing integral types - built-in aliases for `std::size_t`

I'm having an issue with serialization of std::size_t with libc++ (the standard library coming with clang).

It seems that on most platforms, this is caught by the std::uint64_t variant, but when compiling with libc++ (the default on macOS) things go awry, because std::size_t is aliased to unsigned long, but std::uint64_t is not.

I could easily add an overload for unsigned long of course:

RW_JSON_VALUES( unsigned long, IsUint64, GetUint64, SetUint64 )

This fixes the compilation issues when using libc++, but of course it breaks elsewhere, because it's considered a redefinition of the same function (because the type aliases to std::uint64_t, which already has an overload).

I can't think of a nice solution to this, I hope you'll have more luck!

Tons of errors on windows

1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(620,53): warning C4003: not enough arguments for function-like macro invocation 'max'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(620,53): error C2589: '(': illegal token on right side of '::'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(620): error C2062: type 'unknown-type' unexpected
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(620,59): error C2059: syntax error: ')'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(622,2): error C2181: illegal else without matching if
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(646,52): warning C4003: not enough arguments for function-like macro invocation 'max'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(647,49): warning C4003: not enough arguments for function-like macro invocation 'min'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(646,52): error C2589: '(': illegal token on right side of '::'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(646): error C2062: type 'unknown-type' unexpected
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(647,49): error C2589: '(': illegal token on right side of '::'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(647,1): error C2059: syntax error: ')'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(647,55): error C2059: syntax error: ')'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(649,2): error C2181: illegal else without matching if
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(682,93): warning C4003: not enough arguments for function-like macro invocation 'max'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(682,93): error C2589: '(': illegal token on right side of '::'
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(682): error C2062: type 'unknown-type' unexpected
1>D:\dev\vcpkg\installed\x64-windows\include\json_dto\pub.hpp(682,93): error C2059: syntax error: ')'

The only thing I'm doing is this:

UserLogin login_info(username, password);
std::string json_data = json_dto::to_json<UserLogin>(login_info);

Array issue under MSVS 15 compilation

Hello guys,
I'm facing issue with array logic. Even this example:
`struct data_t
{
std::string m_a;
int m_b;

template<typename I>
void json_io(I & io)
{
	io & json_dto::mandatory("a", m_a)
		& json_dto::mandatory("b", m_b);
}

};
int main() {
std::vector< data_t > e1{ { "a", 0 },{ "b", 1 },{ "c", 3 } };

//const std::string json_data = json_dto::to_json(e1);

std::string txt = R"JSON([{"a":"a","b":0},{"a":"b","b":1},{"a":"c","b":3}])JSON";
auto e2 = json_dto::from_json< std::vector<data_t> >(txt);

std::cout << "Sorted data: " << txt << std::endl;

}`

Fail with Error C2039 'json_io': is not a member of 'std::vector<data_t,std::allocator<_Ty>>' ...json_dto\pub.hpp 880

Can someone advise me what can be the issue?

Missing predefined IO for raw JSON values

Somethimes there is a need to extract raw value from object tree, e.g. if value structure is irrelevant or variable. This can be accomplished by storing a field of type rapidjson::Value, which can hold any JSON value.

However, this does not compile:

#include <json_dto/pub.hpp>

struct Test {
    rapidjson::Value test;

    template <typename IO>
    void json_io(IO &io) {
        io & json_dto::mandatory("test", test);
    }
};

static void test() {
    Test t;
    json_dto::to_json(t);
}

failing with

error: ‘class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘json_io’

It would be useful to have such predefined reader/writer.

magic json serialization

I recently discovered Boost.PFR, which allows a form of reflection on simple aggregates. Reading the field names is, unfortunately, only supported in c++20 (so we couldn't add direct support for it inside json_dto), but I think it's useful enough that it warrants an example, and maybe a mention in the main README. Take the following code:

#include <boost/pfr.hpp>
#include <iostream>
#include <json_dto/pub.hpp>

struct bla {
    int x;
    float y;
    std::string z;
};

template <typename wrapped>
class aggregate {
   public:
    aggregate(wrapped& data) : data{data} {}

    template <typename io_type>
    void json_io(io_type& io) {
        json_io(io,
                std::make_index_sequence<boost::pfr::tuple_size_v<wrapped>>());
    }

   private:
    template <typename io_type, std::size_t... i>
    void json_io(io_type& io, std::index_sequence<i...>) {
        ((io &
          json_dto::mandatory(
              rapidjson::StringRef(boost::pfr::get_name<i, wrapped>().data(),
                                   boost::pfr::get_name<i, wrapped>().size()),
              boost::pfr::get<i>(data))),
         ...);
    }

    wrapped& data;
};

int main() {
    bla x{1, 2.5f, "greetings"};

    auto result = json_dto::to_json(aggregate{x});
    std::cout << result << std::endl;

    bla y{};
    aggregate a{y};
    std::string input{"{\"x\":1,\"y\":2.5,\"z\":\"greetings\"}"};
    json_dto::from_json(input, a);

    assert(x.x == y.x);
    assert(x.y == y.y);
    assert(x.z == y.z);
}

This example highlights (de)serializing structs without manually binding the fields! Simply add a field and it's automatically (de)serialized.

variant type

Hi.

I have a dynamic type field in json, I simply declare it with std::any type, but it cannot be compiled.
Is this possible or not?

Feature request: read-only fields

I'm thinking it could be useful to have fields that are explicitly set to read-only, which will serialize to json normally, but simply ignore the field when reading from json.

Right now, we can of course make a field optional_no_default, which ensures we don't get an error when reading from json while not having the field, but this is not as explicit as marking it as read-only. Also, it might result in unwanted behaviour if the json does contain the optional field. Additionally, a read-only field could accept a const-reference when seriailizing which would of course never work with an optional field (since it might need to write to the field).

I could whip up a PR if this is something we might want in json_dto.

std::map with int keys fails at runtime

First, thanks for this great library.

The implementation of map_like_associative_container does work only with string-keys, so a std::map with an int key fails at runtime, because
object.AddMember( key, value, allocator );
asserts that key is a string.

e.g.:

struct test {
	std::map<int, int> map1;
	std::map<std::string, int> map2;

	template<typename Json_Io>
	void json_io(Json_Io & io)
	{
		io & json_dto::mandatory("map1", map1) 
		& json_dto::mandatory("map2", map2) ;
	}
};
test r2;
r2.map1[99] = 100;
r2.map1[199] = 9100;
r2.map2["99"] = 1100;
r2.map2["199"] = 19100;

std::string json = json_dto::to_json(r2);

As I needed a fast solution, I made a quick hack using std::to_string

https://github.com/DaDuFan/json_dto/commit/0158c4a359afb09cc7bb3ece312f04731d055e64

This hack is not good enough to be published, therefore I made no Pull request, but maybe you can use it as an inspiration for a better solution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.