Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use value for std::experimental::optional type? #760

Closed
dianambb opened this issue Oct 1, 2017 · 3 comments
Closed

How to use value for std::experimental::optional type? #760

dianambb opened this issue Oct 1, 2017 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@dianambb
Copy link

dianambb commented Oct 1, 2017

I am trying to parse the json value for a field if it exists and set it to std::experimental::nullopt if it does not.

#include "json.hpp"
#include <experimental/optional>

namespace nlohmann {
    template <typename T>
    struct adl_serializer<std::experimental::optional<T>> {
        static void to_json(json& j, const std::experimental::optional<T>& opt) {
            if (opt == std::experimental::nullopt) {
                j = nullptr;
            } else {
                j = *opt;
            }
        }
        
        static void from_json(const json& j, std::experimental::optional<T>& opt) {
            if (j.is_null()) {
                opt = std::experimental::nullopt;
            } else {
                opt = j.get<T>();
            }
        }
    };
}

namespace my_project {
struct Person {
    std::string id;
    std::string name;
    std::experimental::optional<std::string> company;
};

    void to_json(json& j, const Person& p) {
        j = json {
            {"id", p.id},
            {"name", p.name},
            {"company", p.company}
        };
    }
    
    void from_json(const json& j, Person& p) {
        p.id = j.at("id").get<std::string>();
        p.name = j.at("name").get<std::string>();

        // This works

        try {
            p.company = j.at("company").get<std::experimental::optional<std::string>>();
        } catch(std::exception) {
            p.company = std::experimental::nullopt;
        }

        // This does not work, but it would be nice to have it instead if using try/catch for multiple fields.

        p.company = j.value("company", std::experimental::nullopt);
    }
}

@theodelrieu
Copy link
Contributor

I advise you to take a look at #278, don't hesitate to provide feedbacks about the proposed behaviours :)

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Oct 2, 2017
@nlohmann
Copy link
Owner

nlohmann commented Oct 4, 2017

@dianambb Did you have the chance to check the issue mentioned in #760 (comment)?

@nlohmann
Copy link
Owner

nlohmann commented Oct 5, 2017

💤 I closed this issue due to inactivity. Please feel free to add a comment and I shall reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants