Comments (2)
Thanks for the information! I'll give that a try.
from undictify.
Hi shawalli,
thanks for the feedback and sorry for the inconvenience.
In version 0.1.x it was:
my_thing = undictify.unpack_json(Thing, json_str)`
Now with version 0.2.x it is
my_thing = undictify.type_checked_apply(Thing, **json.loads(json_str))
I realized that unpacking JSON is just one specific use-case of a more general problem this library can solve, and I tried to reflect that.
Luckily the change needed on client side (see above) is quite small. And since the library has not yet reached version 1.x, I was hoping such a change would be acceptable. 🙂
If you still want a function named unpack_json
, that you can use like my_thing = unpack_json(Thing, json_str)
(and that has the optional convert_types
flag), you can create it like this:
TypeT = TypeVar('TypeT')
def unpack_json(target_func: Callable[..., TypeT],
object_repr: str, convert_types: bool = False) -> TypeT:
if convert_types:
return type_checked_apply_skip_convert(target_func,
**json.loads(object_repr))
return type_checked_apply_skip(target_func,
**json.loads(object_repr))
I also just added it to examples/readme_examples.py
.
However I'd like to encourage you to also try the new version, which is adding the @type_checked_call_skip
decorator to your target class and unpacking you JSON like this:
my_thing = Thing(**json.loads(json_str))
from undictify.
Related Issues (14)
- Custom conversions HOT 10
- Optional Unions on dataclasses cannot be omitted HOT 2
- Respect InitVars in type_checked_constructor HOT 1
- Inheritance with @type_checked_constructor() HOT 6
- Force conversion independently of type HOT 6
- Branch `master` will be renamed to `main` in November HOT 1
- dataclass variable Optional type causing issue HOT 1
- incorrect output in first example HOT 1
- How to contribute? HOT 1
- Dates HOT 2
- dataclass compatibility HOT 2
- Future-compatible annotations HOT 6
- String to boolean conversion HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from undictify.