Skip to content

Add interconversion with json#16

Draft
moisutsu wants to merge 13 commits intomainfrom
feature/interconversion-with-json
Draft

Add interconversion with json#16
moisutsu wants to merge 13 commits intomainfrom
feature/interconversion-with-json

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