Skip to content

Question about get_ref() #128

Closed
Closed

Description

@dariomt

I see there were some discussions about adding get_ref(), but I can't see it in the code.
What happened to get_ref()?

I believe that having get_ref() is convenient and safe. It could merely call get_ptr() and throw if it returns nullptr. It makes it easier to traverse a json object when I'm pretty sure about the types it holds. Otherwise I have to constantly check against nullptr myself.

I'd like the following code to work seamlessly:

void func(const std::string&);
json value = "foobar";
func( value.get_ref<const std::string>() );

Currently I'd have to write the following:

void func(const std::string&);
json value = "foobar";
auto p = value.get_ptr<const std::string*>();
if (!p) throw some_exception;
func( *p );

Would you accept a PR for this functionality?

Activity

  1. nlohmann commented on Oct 6, 2015

    @nlohmann
    Owner

    After a getter for a value and a pointer I did not follow to implement get_ref. But I would be glad to have a PR for that.

  2. dariomt commented on Oct 7, 2015

    @dariomt
    ContributorAuthor

    I'll try to provide a PR including tests and docs.

    One question: what would be the preferred form?
    get_ref<const std::string>()
    or
    get_ref<const std::string &>()

    To me get_ref is enough to convey that we get a reference, the & in the template parameter is redundant (I feel the same way about the * required by get_ptr by the way).

  3. dariomt commented on Oct 16, 2015

    @dariomt
    ContributorAuthor

    I have this ready in my branch get_ref.
    But I have created that branch off my master where I have all my other PRs already merged.
    Do you know how I can produce a PR with only the actual changes done in my branch?

  4. nlohmann commented on Dec 7, 2015

    @nlohmann
    Owner

    Hi @dariomt, sorry for not answering earlier.

    As for your first question: I would prefer get_ref<const std::string &> - it makes the getting of a reference explicit.

    For your second question, I unfortunately have no answer.

  5. nlohmann commented on Dec 16, 2015

    @nlohmann
    Owner

    Any news here?

  6. dariomt commented on Dec 16, 2015

    @dariomt
    ContributorAuthor

    My changes already implemented get_ref<const std::string &>, so that's good.

    However, I'm still trying to get the branch in proper shape to make the PR easy for you, git newbie here :)

  7. nlohmann commented on Dec 27, 2015

    @nlohmann
    Owner

    Ok - looking forward to it!

  8. nlohmann commented on Jan 14, 2016

    @nlohmann
    Owner

    @dariomt - any news on this?

  9. dariomt commented on Jan 18, 2016

    @dariomt
    ContributorAuthor

    Sorry, I haven't had time to look into this.
    I should have time this week.

  10. dariomt commented on Jan 18, 2016

    @dariomt
    ContributorAuthor

    PR ready!
    I think I've merged everything correctly.

  11. added this to the milestone on Jan 20, 2016
  12. nlohmann commented on Jan 20, 2016

    @nlohmann
    Owner

    Thanks @dariomt for the good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions