Skip to content

Getting member discarding qualifyer #159

Closed

Description

@EvilPudding

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?

Activity

  1. nlohmann commented on Dec 20, 2015

    @nlohmann
    Owner

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

  2. EvilPudding commented on Dec 21, 2015

    @EvilPudding
    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.

  3. nlohmann commented on Dec 21, 2015

    @nlohmann
    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.

  4. EvilPudding commented on Dec 21, 2015

    @EvilPudding
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions