Giter VIP home page Giter VIP logo

Comments (5)

skabbes avatar skabbes commented on July 20, 2024

The Json interface is immutable (by design), however you can build up json objects by using Json::object (which is just a map<string, Json>underneath).

  • Json is immutable.
  • Json::array is actually vector<Json> and is therefore mutable
  • Json::object is actually map<string, Json> and is therefore mutable
Json::object mutable_json = Json::object {
    { "key1", "value1" },
    { "key2", false },
    { "key3", JSON::array { 1, 2, 3 } },
    { "key4", 666.333 }
};
mutable_json["key5"] = string {"something else"};

Json immutable_json = Json {mutable_json};

This makes sense (to me) because there is no guarantee that a Json is an object - what would it mean to do this?

Json value = 5;
value["new_key"] = 6; // doesn't compile

from json11.

peterritter avatar peterritter commented on July 20, 2024

Hello skabbes

Many thanks for the response! The maintainer of the library may want to add your explanation to the user guide! I'm not really a JSON specialist, so I can't say what is 'legal' and what is not according to the json specification. However, from a purely practical perspective, I always think of the 'root' of the 'object' as a 'json object'. Would it really be 'Json' if it wasn't?

I am passing json objects around the network, as a quick way of putting messages together, and I needed to add an 'identifier token' to each message, and I just couldn't figure out how to do that. I did mange eventually to add an 'insert' function after struggling for quite a while.

Thanks

from json11.

maverick447 avatar maverick447 commented on July 20, 2024

hello skabbes,
Thanks for the tip! I am able to add a node but I need to add a Dictionary within a new entry into the map how would go ahead with it?

instead of this
mutable_json["key5"] = string {"something else"};
I need something like
mutable_json["key5"] = { add a name and value pair }

from json11.

skabbes avatar skabbes commented on July 20, 2024

@maverick447 give this a try:

mutable_json["key5"] = Json::object {
  { "new_key", "new_value" }
};

from json11.

maverick447 avatar maverick447 commented on July 20, 2024

HI skabbes,
Thanks!

from json11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.