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

Identifying JsonVariant type of a deserialized document #2086

Closed
HamzaHajeir opened this issue May 6, 2024 · 4 comments
Closed

Identifying JsonVariant type of a deserialized document #2086

HamzaHajeir opened this issue May 6, 2024 · 4 comments
Labels

Comments

@HamzaHajeir
Copy link

Hi

I'm building an entity for serializing/deserializing from/to multiple formats for unknown schemes, one of them is JSON.

Over deserialization, I want to detect whether a JsonVariant holds an array by calling:

    std::string data; // The data source
    DynamicJsonDocument doc(1024);
    deserializeJson(doc, data);
    JsonObject root = doc.as<JsonObject>();
    for (JsonPair kv : root){
        std::string out_single;
        std::vector<std::string> out_multiple;
        if (kv.value().getData()->isArray()) /* Append out_multiple by looping inside it */;
        else out_single = kv.value().as<std::string>(); // It's an element, just use this instruction to fetch it.
    }

However, I can't perform this operation due to getData() method being unaccessable.

How can I perform such target using ArduinoJson (I'm on v6, but assume the latest: v7).

With Thanks,
Hamza Hajeir

@bblanchon
Copy link
Owner

kv.value().is<JsonArray>()

@HamzaHajeir
Copy link
Author

kv.value().is<JsonArray>()

Great, and getting it as array would be
kv.value().as<JsonArray>().

It'd be also great to add this to docs or examples ;)

@bblanchon
Copy link
Owner

The documentation already says that kv.value() is a JsonVariant.
What else do you need, and on which page?

@HamzaHajeir
Copy link
Author

HamzaHajeir commented May 13, 2024 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants