arduino-libraries / Arduino_JSON

Official JSON Library for Arduino
GNU Lesser General Public License v2.1
151 stars 60 forks source link

Enhancement: check if entry in JSON #65

Closed step135 closed 1 month ago

step135 commented 1 month ago
const char* sensor = doc["sensor"] | "unknown";

That could be ok, but when I use byte type and want to have all 256 numbers disponible, there is no solution. I like that in Python I am able to do the check: using in. I need to implement following logic:

byte temp = 24; //default value
if("temp" in doc["temp"]) temp = doc["temp"]; //ovewriting the value if it is in JSON
step135 commented 1 month ago

OK, chatGPT adviced me to use doc.containsKey(), it works, but it is not documented under header "Extracting errors" at https://arduinojson.org/v7/tutorial/deserialization/

per1234 commented 1 month ago

Hi @step135. This is the repository for the "Arduino_JSON" library. You are referring to the similarly named, but completely separate "ArduinoJson" library:

https://github.com/bblanchon/ArduinoJson

This library has a hasOwnProperty function that is equivalent to the "ArduinoJson" library's containsKey function, which is demonstrated in the example sketch:

https://github.com/arduino-libraries/Arduino_JSON/blob/ea843a9a0b56e3baaa1d26795072f81b9b8787ca/examples/JSONObject/JSONObject.ino#L42-L65

So the feature request is not applicable to this project.