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

Getting member discarding qualifyer #159

Closed
EvilPudding opened this issue Dec 18, 2015 · 4 comments
Closed

Getting member discarding qualifyer #159

EvilPudding opened this issue Dec 18, 2015 · 4 comments

Comments

@EvilPudding
Copy link

I know there has been a lot of discussion on this topic in
the issues, but I don't know what conclusion you guys
arrived to, all I know is that I'm need to use json as a
const reference, and what I've been doing to access the
object entries isn't very efficient, what I want to do is:

void func(const json &event)
{
    float x = event["x"];
}

But I get a warning that this discards the const qualifier,
so what I'm doing to fix it is the horrible:

void func(const json &event)
{
    json ev = event;
    float x = ev["x"];
}

Which works, but is silly and not efficient as hell, seen
this function is supposed to be called maybe thousands
of times per second. Is there a plan to fix it, and what
should I do meanwhile so that I'm not copying the
memory every time I call the function?

@nlohmann
Copy link
Owner

You may want to have a look at the discussions in #133.

@EvilPudding
Copy link
Author

This doesn't suit me at all. From what I gather from
that discussion is that instead of using the operator
that most people who have seen js before are familiar
with (obj["key"]), I'm supposed to use obj.value("key")?
why? I don't understand the reasoning behind it, is it
so you can have it behave like a map would?

I'm using the json objects to be the storage of event
information, and because of that, I wanted there to be
an intuitive way of accessing the members without my
team having to learn specific methods.

I also thought that because of the use of modern C++,
this would be the one repo where accessing json members
would be closest to how their accessed in javascript,
you know? Like this:

var foo = 1;
console.log(foo['a']);
// null
foo = ['a', 'b'];
console.log(foo['0']);
// 'a'
foo = {'a':1};
console.log(foo['b']);
// null

This is probably the best json lib there is, but I
think you've made a poor dicision in this case, I may
be wrong, and if so I would appreciate an elucidation
in that regard, but until I understand, I just have
really messy code sitting there.

I really didn't want to do this, but so my code is
readable, I think I might have to keep a patch to make
this work like I want it to.

@nlohmann nlohmann reopened this Dec 21, 2015
nlohmann added a commit that referenced this issue Dec 21, 2015
It was a good idea to implement a const version of operator[] it in the
first place. I was a pity that this implementation was flawed. It was a
mistake to remove the const version completely. This commit
re-introduces the const version. My apologies for all the inconvenience.
@nlohmann
Copy link
Owner

Hi @EvilPudding, just to let you know, I reintroduced the const operator[], see http://nlohmann.github.io/json/classnlohmann_1_1basic__json_a8e34088252a3ee6b2377f3a1f26dd1ba.html#a8e34088252a3ee6b2377f3a1f26dd1ba. Sorry for all the inconvenience.

For the implicit conversions (e.g., returning null for nonexisting object values), I think mimicking JavaScript would introduce a lot of surprises to the world of C++. Instead, you may use value and define the value you would like to receive if the key does not exist yourself.

@EvilPudding
Copy link
Author

Your response and modification was quick and
perfect.

The part about the implicit conversions was just a
flavour thing, it would be fun, but probably bring
more problems than it would solve.

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants