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 interconversion with json #16

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

moisutsu
Copy link
Owner

@moisutsu moisutsu commented Nov 5, 2022

Add the methods to_json and from_json to the argument instance for interconversion with json.

to_json

from classopt import classopt

@classopt
class Opt:
    arg_int: int

opt = Opt.from_args()
print(opt.to_json())

opt.to_json("sample.json")
$ python sample.py 3
{"arg_int": 3}

$ cat sample.json
{"arg_int": 3}

to_dict

from classopt import classopt

@classopt
class Opt:
    arg_int: int

args_json = '{"arg_int": 3}'

opt = Opt.from_json(args_json)
print(opt)

opt = Opt.from_json("sample.json")
print(opt)
$ cat sample.json
{"arg_int": 3}

$ python sample.py 3
Opt(arg_int=3)
Opt(arg_int=3)

@moisutsu moisutsu marked this pull request as draft February 24, 2024 05:33
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

Successfully merging this pull request may close these issues.

1 participant