Code Monkey home page Code Monkey logo

Comments (7)

bblanchon avatar bblanchon commented on June 19, 2024

Hi @judge2005,

Thank you for this feedback but I don't see any problem since this behavior is consistent with the conversion to bool.
For example:

JsonVariant var;
var.as<bool>();  // false

JsonDocument doc;
var = doc.to<JsonVariant>();
var.as<bool>();  // false

var.set(false);
var.as<bool>();  // false

var.set(true);
var.as<bool>();  // true

var.set(0);
var.as<bool>();  // false

var.set(1);
var.as<bool>();  // true

var.set(nullptr);
var.as<bool>();  // false

var.set("hello world");
var.as<bool>();  // true

var = doc.to<JsonArray>();
var.as<bool>();  // true

var = doc.to<JsonObject>();
var.as<bool>();  // true

Best regards,
Benoit

from arduinojson.

judge2005 avatar judge2005 commented on June 19, 2024

The problem is that using operator[] I can’t tell the difference between an attribute that doesn’t exist and one that does exist but who’s value is zero. However that is the solution recommended by the documentation. In reality I have to use containsKey().

from arduinojson.

bblanchon avatar bblanchon commented on June 19, 2024

You can use value.is<int>().
Not only does it tell you that the value is present, but it also confirms it has the expected type, making your code even more robust.

from arduinojson.

judge2005 avatar judge2005 commented on June 19, 2024

That is good, but I am trying to make the point that the documentation is misleading. If it is followed it would lead to code that does not function correctly. The documentation states that operator[] can be used to determine if an attribute is present or not because it returns false if the attribute is not present. In fact it will also return false if the attribute is present but is either a numeric zero or a boolean false.

from arduinojson.

bblanchon avatar bblanchon commented on June 19, 2024

Which part of the documentation are you referring to?

from arduinojson.

judge2005 avatar judge2005 commented on June 19, 2024

https://arduinojson.org/v7/api/jsonvariant/containskey/

The text under the heading "Avoid this function when you can!"

Because ArduinoJson implements the Null Object Pattern, it is always safe to read the object: if the key doesn’t exist, it returns an empty value.

Which is only true in the sense that it will always return something rather than throwing an exception.

from arduinojson.

bblanchon avatar bblanchon commented on June 19, 2024

I'm sorry I don't see any problem with this paragraph.
How would you rephrase it?

from arduinojson.

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.