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

Iterate through TransientObject items? #6

Open
mattdm opened this issue May 30, 2024 · 2 comments
Open

Iterate through TransientObject items? #6

mattdm opened this issue May 30, 2024 · 2 comments

Comments

@mattdm
Copy link

mattdm commented May 30, 2024

This is somewhat related to #5. Although it is probably safe to assume that the JSON dictionary I'm trying to parse will always be in the same order... it might not.

Therefore, I'd like to do something like:

hourly_file = io.open("hourly.json",'rb')
json_data = adafruit_json_stream.load(hourly_file)
periods = json_data['properties']['periods']

for period in periods:
    for (key,value) in period.items():
        print(f"{key} = {value}")

But, there is no items() in TransientObject. How hard would this be to implement? (The above code works with full json_stream, as does something like:

    for (key,value) in period.items():
        if key == 'number':
            print(f"Number: {value}")
        elif key == 'temperature':
            print(f"Temp:   {value}")
        elif key == 'probabilityOfPrecipitation':
            print(f"Rain%:  {value['value']}")

which is basically exacatly what I want to do (except more than just printing of course).

@mattdm
Copy link
Author

mattdm commented May 30, 2024

I tried briefly to implement this, but ran into problems (not surprisingly) nested dictionaries -- _IterToStream.next_value() returns those as transient objects, which presents a conundrum. If the iterator skips past that, that item can never be read. But if it doesn't, things are kind of in a weird state. (I think this is what the active_child value is for?)

I think maybe the solution is to "realize" any nested dictionaries or lists. That is, if the buffer expected to be a value starts with { or [ , buffer everything until the matching } or ] (including any further nested dictionaries or lists, so it might not necessarily be the next end-char encountered!) and then json.loads() that buffer and return the result as value. And then, document this as an implementation detail, warning people not to iterate over dictionaries with large nested objects unless there's plenty of memory.

Anyway, this turned into a longer lunch break than I intended :) so I am going to stop the yak-shaving here.

@justmobilize
Copy link
Contributor

@mattdm on this the added .as_object() could help (if the object isn't too big) and there is active work on this that may add items() in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants