Giter Site home page Giter Site logo

Comments (6)

nlohmann avatar nlohmann commented on May 25, 2024

This is documented behavior, see https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/#parameters

image

from json.

RAnders00 avatar RAnders00 commented on May 25, 2024

In this case, I need to use the adl_serializer specialization everywhere? I see. Thank you for the swift response, I had indeed overlooked this limitation.

Would you be open to tracking this deficiency as a feature request/request for enhancement then?

from json.

nlohmann avatar nlohmann commented on May 25, 2024

Would you be open to tracking this deficiency as a feature request/request for enhancement then?

Sure - though I hope it does not mean adding another dozen of macros for this case...

from json.

gregmarr avatar gregmarr commented on May 25, 2024

This works just fine, but now structs containing this type cannot be json-converted using the macros anymore:
Godbolt link: https://godbolt.org/z/q8oxq61sx (gcc trunk, nlohmann_json trunk)

That's simply because you didn't implement the version of from_json that takes the destination object by reference. You need that in order to support the get_to from the macros. Also, you should use the templated version of the first parameter so that you can also support ordered_json.

NLOHMANN_JSON_NAMESPACE_BEGIN
template <> struct adl_serializer<NonDefaultConstructible> {
    template<typename BasicJsonType>
    static void to_json(BasicJsonType &json_value, const NonDefaultConstructible &data) {
      json_value = data.get_inner_value();
    }

    template<typename BasicJsonType>
    static void from_json(BasicJsonType && j, NonDefaultConstructible &val) 
    {
      // There is no setter for this member, so I had to create a new one and move it into the original.
      // I assume in the original there is a setter, which would make this easier.
      val = NonDefaultConstructible{j.template get<std::string>()};
    }
};
NLOHMANN_JSON_NAMESPACE_END

@nlohmann It would probably be a good idea to update the docs examples to use the template BasicJsonType.

from json.

RAnders00 avatar RAnders00 commented on May 25, 2024
template<typename BasicJsonType>
static void from_json(BasicJsonType && j, NonDefaultConstructible &val) 

Just curious, why the forwarding reference? When I try it, it seems to work with a const BasicJsonType& too, and clangd gives me a warning that the forwarding reference is never forwarded inside the function body?

from json.

gregmarr avatar gregmarr commented on May 25, 2024

It should probably just be const&.

from json.

Related Issues (20)

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.