Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON validation only? #1833

Closed
Jeroen88 opened this issue Dec 12, 2022 · 8 comments
Closed

Add JSON validation only? #1833

Jeroen88 opened this issue Dec 12, 2022 · 8 comments
Labels
enhancement v6 ArduinoJson 6

Comments

@Jeroen88
Copy link

Is your feature request related to a problem? Please describe.
Not a problem. just a useful function..

Describe the solution you'd like
A function DeserializationError error = deserializeJson(json) that just reports if the input JSON is a valid JSON.

Describe alternatives you've considered
I do:

  StaticJsonDocument<0> emptyDoc;
  StaticJsonDocument<0> filter;

  DeserializationError error = deserializeJson(emptyDoc, json, DeserializationOption::Filter(filter));

and that does exactly what I want. But I depend on undefined semantics: keys not present in the filter might as well be skipped instead of parsed, but it seems they are parsed too.

Additional context
None. It is not a big deal, the above alternative is also OK.

@bblanchon
Copy link
Owner

Hi @Jeroen88,

I repeatedly stated that I didn't plan to add a JSON validation feature to ArduinoJson.
Although, to be fair, the previous requests ask me to check the validity of the input according to a JSON schema (#781).
What you ask here is much simpler, maybe too simple...

The reason I don't plan on adding this feature is that the parser is relatively lax.
Indeed, while optimizing the parser, I made some compromises that can let invalid documents slip through.
For example:

It also allows syntaxes that are not supported by JSON, namely:

  • String delimited by single quotes
  • Object keys without any quotes

(although, this will probably move into a new deserializeJson5() someday, see #1687)

The workaround you found is excellent!
I would never have thought of it myself, and I think it could have its place in the "How to" section of the site.

While skipping the values, the deserializeJson() still parses most of the document but will not detect any UTF-16 error.

I suggest we keep this issue open to collect some feedback.
We'll see if more users are interested in this simple form of validation.

Best regards,
Benoit

@Jeroen88
Copy link
Author

Hi Benoit,

Thank you for your excellent answers! That is really appreciated. Sorry for not checking all the issues, although I did a search for similar questions.

Also thanks for the explanation of how the parsing is done with an empty filter, now I know what to expect of the call: not perfect but good enough.

It would be nice if you add the suggestion to the "How to" section!

Best regards, Jeroen

@bblanchon
Copy link
Owner

Here is the new page: How to validate a JSON document?

@Jeroen88
Copy link
Author

Very nice and thanks for the reference! One typo in the text:

it will oversees some errors in the input

@HamzaHajeir
Copy link

Hi there, I was to raise issue but found commenting here is better

I have a problem in validating Json, as it returns true always:

MVCE (:

#include "ArduinoJson.h"

bool validateJson(const char* input) {
    StaticJsonDocument<0> doc, filter;
    return deserializeJson(doc, input, DeserializationOption::Filter(filter)) == DeserializationError::Ok;
}
void setup(){

    char data[] = "HelloWorld";
    Serial.printf("validateJson(%s) --> %d\n", data, validateJson(data));
}

Output:

validateJson(HelloWorld) --> 1

Tested with ArduinoJson version 6.19.4

@bblanchon
Copy link
Owner

Thanks for reporting this, @HamzaHajeir.

I updated the website to make it more evident that this solution is not bulletproof, but your comment makes me wonder what this function is good for if it cannot detect such an obvious mistake.

Honestly, I think I should remove it from the documentation.
@HamzaHajeir and @Jeroen88, please let me know what you think.

@HamzaHajeir
Copy link

No problem @bblanchon, I'm wondering whether there's an effective way of checking the validity of a JSON object around?

Best,

@bblanchon
Copy link
Owner

If the validateJson() is too forgiving for your needs, you'll have to do full deserialization (i.e., without filter) and check the error code.
Again, I don't plan to add any validation feature in ArduinoJson.

@bblanchon bblanchon closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2023
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

3 participants