You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
voidfunc(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:
voidfunc(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?
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:
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.
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.
Activity
nlohmann commentedon Dec 20, 2015
You may want to have a look at the discussions in #133.
EvilPudding commentedon Dec 21, 2015
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:
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.
re-adding const version operator[] (#135, #159)
nlohmann commentedon Dec 21, 2015
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
nullfor nonexisting object values), I think mimicking JavaScript would introduce a lot of surprises to the world of C++. Instead, you may usevalueand define the value you would like to receive if the key does not exist yourself.EvilPudding commentedon Dec 21, 2015
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!