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

Load to object instead of class #45

Closed
silas2471 opened this issue Sep 9, 2020 · 6 comments
Closed

Load to object instead of class #45

silas2471 opened this issue Sep 9, 2020 · 6 comments

Comments

@silas2471
Copy link

Hey,
within the JSON5 Format one can use ObjectKeys, which are not quoted/strings.
Is it possible to load those json files straight to an object, instead of a dict?
This would make accessing and using the data much more convenient, especially if one has a file with many levels.
It should also enable autocompletion in many Editors (if a dummyObject with the same structure as the file is given).
An example is given below:

//dummyFile
{
    config1: {
        dummy1:1,
        dummy2:2
    },
    config2:{
        dummy2:3
    }
}
.....
//Accessing files and converting to object.
with open(dummyFile, "r") as fob:
    data = json5.load(fob)
    
var1 = data.config1.dummy1             // loading and accessing the data as object
var2 = data["config1"]["dummy1"]       // loading and accessing data as a dict (currently)
@dpranke
Copy link
Owner

dpranke commented Sep 10, 2020

Thanks for the suggestion.

You should be able to achieve that easily enough using the object_hook or object_pairs_hook arguments to json5.load().

I would be a little reluctant to implement that directly in the library because I'm not entirely sure what all interfaces users would want the objects to implement (nothing? Everything dict implements? etc.).

If there was a consensus from a bunch of users I'd probably be more open to implementing something, but you're the first person to ask for this.

(Of course, there's no discussion list for this library, either, so no real way to ask other users for opinions, either ...)

As to enabling autocompletion in editors, I'm not actually sure how that would work for generic objects ... If you had a pointer describing what might be needed, I'd be interested in learning more.

How does that sound?

@silas2471
Copy link
Author

Hey,
I figured out a way to make it work using munchify.
Unfortunately I had to abandon using json5 as a config file, as is does not support keeping comments using json5.dump() .
Now I am using .py files as config.

Regarding autocompletion, I am using a "DefaultConfig" in a python class, which has the same structure/attributes as the previously used munchified Json5 config ().

def func(config: DefaultConfig):
     pass # now autocompletion regarding the structure of DefaultConfig is available.

Thanks for your ideas though :)

@dpranke
Copy link
Owner

dpranke commented Sep 12, 2020

Thanks.

How are you solving this problem using python files for configuration? Are you using some library that'll preserve comments, or are you rolling your own logic?

[ Preserving comments in pyjson5 is issue #28 ]

Implementing everything dict does does seem like a reasonable default behavior.

Using a default object as a parameter type makes sense, but I'm not sure how that'd work when trying to
refer to a config file.

I'll leave this open in case others have similar requests in the future.

[ Note to self for future reference: munch (munchify is part of that). ]

@silas2471
Copy link
Author

Uhm I am displaying the .py files using the normal open and .readlines() / .writlines() Operator. In order to retrieve the config, I use the builtin runpy.run_path() function to retrieve the class. In my case all config files have the same structure, just different attributes.
Thus I can use the default config as parameter type, and the passed config, retrieved from runpy.run_path can be accessed with the autocompletion.

@dpranke
Copy link
Owner

dpranke commented Sep 12, 2020

Gotcha.

@dpranke
Copy link
Owner

dpranke commented Jul 21, 2022

Closing as the user found a workaround

@dpranke dpranke closed this as completed Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants