A simple mixin that adds .to_hjson and .from_hjson to the dataclasses-json mixin
Reason this release was yanked:
0.0.2 has the same functionality, but fixed docs
Project description
dataclasses-hjson
This package provides a simple way to serialize and deserialize dataclasses to and from Hjson, using dataclasses-json
and hjson
.
Opposed to dataclasses-json
, this package does only provide a mixin class to add the functionality to a dataclass, instead of a decorator. You can change the configuration of dataclasses-json
by either using the with_config
decorator or by manually setting the dataclasses_json_config
attribute on the dataclass, using the hjson_config
function.
If you have any problems, ideas or suggestions, feel free to open an issue or a pull request!
Installation
pip install dataclasses-hjson
Usage
Example adding config using the with_config
decorator:
from dataclasses import dataclass
from dataclasses_json import Undefined, LetterCase
from dataclasses_hjson import DataClassHjsonMixin, with_config
@with_config(undefined=Undefined.EXCLUDE, letter_case=LetterCase.CAMEL) # (These are the default values)
@dataclass
class Person(DataClassHjsonMixin):
name: str
age: int
Alternatively, you can use the hjson_config
function:
from dataclasses import dataclass
from dataclasses_json import Undefined, LetterCase
from dataclasses_hjson import DataclassHjsonMixin, hjson_config
@dataclass
class Person(DataclassHjsonMixin):
dataclasses_json_config = hjson_config(undefined=Undefined.EXCLUDE, letter_case=LetterCase.CAMEL) # (These are the default values)
name: str
age: int
Adding the config is optional however!
Now you can serialize and deserialize the dataclass to and from Hjson:
person = Person(name='John Doe', age=30)
# Serialize to Hjson
hjson_str = person.to_hjson()
# Deserialize from Hjson
person = Person.from_hjson(hjson_str)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dataclasses_hjson-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b35c14b3e9745b9d5b030b6e1c792fbdbde9ccce492b3ffd7c37fd1abe594f23 |
|
MD5 | d41391fe193a3ca87ef1cade38bdda09 |
|
BLAKE2b-256 | 47ccab42f92b93d82931bcfa3389d244c256c284394df2ad145210a174513e34 |