Quickstart¶
Writing Compatible Classes¶
Pyckson will assume that the classes you want to transform are written in a certain way :
- All class fields must be named parameters in your __init__ method
- All parameters must be assigned to the object with the same name
- All parameters must be type annotated
class Example:
def __init__(self, foo: str, bar: List[int]):
self.foo = foo
self.bar = bar
Dataclasses¶
Pyckson also works with dataclasses in python3.7+
@dataclass
class Example:
foo: str
bar: List[int]
Conventions¶
Pyckson will produce and read json where your member names will have been tranformed to camelCase.
class CaseTest:
def __init__(self, some_parameter: str):
self.some_parameter = some_parameters
>>> pyckson.serialize(CaseTest('foo'))
{'someParameter': 'foo'}
Serializing Objects¶
The function pyckson.serialize()
takes an object and return a dict-like structure.
>>> from pyckson import serialize
>>> serialize(Example('foo', [1, 2]))
{'foo': 'foo', 'bar': [1, 2]}
-
pyckson.
serialize
(obj)¶ Takes a object and produces a dict-like representation
Parameters: obj – the object to serialize
You can also serialize lists, pyckson will handle the recursion
>>> from pyckson import serialize
>>> serialize([Example('foo', [1, 2]), Example('bar', [3, 4])])
[{'foo': 'foo', 'bar': [1, 2]}, {'foo': 'bar', 'bar': [3, 4]}]
Parsing Objects¶
The function pyckson.parse()
takes a class and a dictionnary and return an instance of the class.
>>> form pyckson import parse
>>> example = parse(Example, {'foo': 'thing', 'bar': [1, 2, 3]})
>>> example
<__main__.Example object at 0x7fb177d86f28>
>>> example.foo
'thing'
>>> example.bar
[1, 2, 3]
-
pyckson.
parse
(cls, value)¶ Takes a class and a dict and try to build an instance of the class
Parameters: - cls – The class to parse
- value – either a dict, a list or a scalar value
Similarily to pyckson.serialize()
you can also use the specific type typing.List[cls] to parse lists.
Utility Functions¶
Pyckson also includes some convenient wrappers to directly manipulate json strings with the json module.
-
pyckson.
dump
(obj, fp, **kwargs)¶ wrapper for
json.dump()
-
pyckson.
dumps
(obj, **kwargs)¶ wrapper for
json.dumps()
-
pyckson.
load
(cls, fp, **kwargs)¶ wrapper for
json.load()
-
pyckson.
loads
(cls, s, **kwargs)¶ wrapper for
json.loads()