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

Enhancing the new Filtering with a wildcard #1309

Closed
joysfera opened this issue Jul 11, 2020 · 4 comments
Closed

Enhancing the new Filtering with a wildcard #1309

joysfera opened this issue Jul 11, 2020 · 4 comments
Labels
enhancement v6 ArduinoJson 6

Comments

@joysfera
Copy link

The cool filtering while deserializing introduced in v6.15 is great but it could be even better if there was a possibility to specify a wildcard for parents of nested documents if you need to filter somewhere deeper in the tree.

Imagine JSON document like this:

{
    "somekey": { "name": "some uninteresting name", "value": 2.718281828 },
    "otherkey": { "name": "guess what the value is", "value": 3.1415926535 },
    ...
    "xyzkey":  { "name": "ran out of interesting constants", "value": 1000 }
}

And I'd need to filter out the names (because they're long and consume a lot of precious RAM) and preserve just the key names (that unfortunately hold some important information) and the values inside of their objects (because the values are what matters).

So the Filtering document would need to look kind of like this:

{ "*": { "value": true } }

Would this be possible to implement easily and cleanly, please? Or any better idea?

Thank you!

@jshep321
Copy link

jshep321 commented Jan 30, 2021

Great feature @bblanchon! My wildcard filtering is not working exactly as expected and I'd really appreciate some feedback.
My input:

String fullUserDB = 
{
    "USER1": { "data" : { "1" : "item 1's description", 
                          "2" : "item 2's description"}, 
              "associated user data" : "value"},

    "USER2":  { "data" : { "10" : "item 10's description",
                          "11" : "item 11's description"} ,
              "associated user data" : "value"}
};

Say that I want to filter on the key "10". I did this:

filter["*"]["data"]["10"] = true;
DeserializationError err = deserializeJson(jsonDoc, fullUserDB, DeserializationOption::Filter(filter));  //deserialize in order to apply filter

and I expected to retain only USER2's details and higher levels (including associated user data).
What I was expecting:

{
    "USER2:  { "data" : { "10" : "item 10's description} ,
              "associated user data" : "value"}
}

However, I'm not keeping the "associated" data, but I am keeping some of USER1 details as below:
Example result after above filtering (and re-serialization to a String and print):
{"USER1":{"data":{}},"USER2":{"data":{"10":"item 10's description}}}

Am I missing something or is the wildcard not as flexible as I was hoping?
Thanks!

@bblanchon
Copy link
Owner

Hi @joysfera,

This is the expected behavior: the filter doesn't trim empty objects.
We could argue that this is a limitation of the library; however, your JSON document is very peculiar and not representative of most use cases.
If you need further assistance, please open a new issue.

Best regards,
Benoit

@joysfera
Copy link
Author

FYI, it was @jshep321 :-)

@jshep321
Copy link

jshep321 commented Feb 1, 2021

Will do. I will write a test program instead of manually edited json in the comments (as I have omitted several quotes above!).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2021
@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