Skip to content

Should I include single-header after my to_json and from_json custom functions declaration? Why not? #1663

Closed

Description

@oktonion

So I was going through the code and can not get how the library find user-defined functions for conversion (to_json and from_json)?

Let me explain with code.

simple example could look like this:

#include <json.hpp>

namespace mynamespace
{
    class myclass{};

    void to_json(json&, const myclass&);
    void from_json(const json&, myclass&);
}

// if I put it there then I understand how it finds my to_json and from_json
// #include <json.hpp> 

void func()
{
    mynamespace::myclass val;
    json j = val;
    val = j.get<mynamespace::myclass>();
}

Before include of <json.hpp> there is no declaration of mynamespace::to/from_json so I expect any check in <json.hpp> like has_custom_to_json to fail. If not then how it works?

My guess is that all the magic is in is_detected_exact but I could not figure out how is it possible and what template trick is used?

P.S.: Sorry for multiple edits.

Activity

  1. changed the title [-]Should I unclude single-header after my `to_json` and `from_json` custom functions declaration?[/-] [+]Should I unclude single-header after my to_json and from_json custom functions declaration? Why not?[/+] on Jul 7, 2019
  2. changed the title [-]Should I unclude single-header after my to_json and from_json custom functions declaration? Why not?[/-] [+]Should I include single-header after my to_json and from_json custom functions declaration? Why not?[/+] on Jul 7, 2019
  3. nlohmann commented on Jul 11, 2019

    @nlohmann
    Owner

    It's using ADL. The internals are from @theodelrieu - maybe they can share more insights.

  4. stale commented on Aug 10, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  5. added
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Aug 10, 2019
  6. oktonion commented on Aug 11, 2019

    @oktonion
    Author

    Any comments from @theodelrieu ?

  7. removed
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Aug 11, 2019
  8. theodelrieu commented on Aug 12, 2019

    @theodelrieu
    Contributor

    Hello, this is because of two-phase lookup, you can find a short but accurate overview here.

    Basically, val = j.get<mynamespace::myclass>(); is the point of instantiation, i.e. the lookup's second phase. At that point, your from_json method is visible to the compiler, whereas it wasn't in the first phase.

  9. oktonion commented on Aug 15, 2019

    @oktonion
    Author

    @theodelrieu Is this behaviour required by C++ standard (which one?) or could there be some other approach used by compiler? As I see from RSDN and SO discussions many compilers fail to implement two-phase lookup properly, so I assume this library would not work in such case,

  10. jaredgrubb commented on Aug 16, 2019

    @jaredgrubb
    Contributor

    Two-phase lookup was always the expectation, even pre-C++11. Some compilers (eg, MSVC) didn't do it quite right but at this point, I would expect all the C++14 and later compilers to definitely have it down.

  11. stale commented on Sep 15, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  12. added
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: questionstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions