dalemyers / deserialize Goto Github PK
View Code? Open in Web Editor NEWA Python deserialization tool
License: MIT License
A Python deserialization tool
License: MIT License
Hello,
I am very interested in your project, but I would like to understand what is the difference between deserialize and json.loads
when using object_hook
parameter. https://docs.python.org/3.8/library/json.html#json.loads
Also does it have to be a json necessary deserialize.deserialize(MyThing, json_data)
or I can use any string ?
flat_path: str = "file://path_to_file"
myFile : File = deserialize.deserialize(File, flat_path)
I am looking for tool in order to convert any string to a python type (int, float, dict, str, uri, file, custom_format).
Thx
Sometimes we want to ensure we've parsed everything and should flag on unhandled fields.
Errors like this aren't helpful:
deserialize.exceptions.DeserializeException: Cannot deserialize '<class 'str'>' to '<enum 'PBXProductType'>' for 'PBXNativeTarget.product_type'
It should say what the value is for enums so we know what has to be added.
Hi there.
Is there any way there can be added another argument like case_function which can be used to convert camelCase field names from JSON string to snake_case which will be used in setattr()?
For example if we have class definition like this
class Test:
field_name: int
and the JSON string looks like this '{"fieldName": 0}'
and deserialize(Test, json.loads('{"fieldName": 0}'))
the current implementation will not be able to deserialize: deserialize.exceptions.DeserializeException: Unexpected missing value for: Test.field_name
Thanks!
Sample class definition:
class Thing:
value: Union[int, float]
Sample data:
{
"value": 4
}
Expected results:
The data deserializes correctly.
Actual results:
The type int
doesn't match the type Union[int, float]
.
Without the name, it can be difficult to figure out which type is incorrect if it fails to deserialize.
I haven't found a way to make it deserialize Enums. Am I missing anything or it is not simply supported?
When a decorator defines a parser for an attribute in a class then that definition is also applied to other subclasses of the same base class.
import decimal
from typing import Any
import attr
import deserialize
def _money_amount(value: Any):
return decimal.Decimal(value).quantize(decimal.Decimal("0.01"), decimal.ROUND_HALF_UP) if value else None
@attr.s(auto_attribs=True)
@deserialize.parser("a", _money_amount)
class Base:
a: decimal.Decimal
@attr.s(auto_attribs=True)
class Foo(Base):
b: str
@attr.s(auto_attribs=True)
@deserialize.parser("b", _money_amount)
class Bar(Base):
b: decimal.Decimal
def test_deserialize_base():
deserialize.deserialize(Base, {"a": 1.23})
def test_deserialize_foo():
deserialize.deserialize(Foo, {"a": 1.23, "b": "b"})
def test_deserialize_bar():
deserialize.deserialize(Bar, {"a": 1.23, "b": 1.23})
test_deserlalize.py::test_deserialize_base PASSED
test_deserlalize.py::test_deserialize_bar PASSED
tests/test_deserlalize.py:32 (test_deserialize_foo)
def test_deserialize_foo():
deserialize.deserialize(Foo, {"a": 1.23, "b": "b"})
test_deserlalize.py:34:
../../../opt/anaconda3/envs/fractal-python/lib/python3.9/site-packages/deserialize/init.py:93: in deserialize
return _deserialize(
../../../opt/anaconda3/envs/fractal-python/lib/python3.9/site-packages/deserialize/init.py:175: in _deserialize
_deserialize_dict(
../../../opt/anaconda3/envs/fractal-python/lib/python3.9/site-packages/deserialize/init.py:370: in _deserialize_dict
property_value = parser_function(value)
value = 'b'
def _money_amount(value: Any):
return decimal.Decimal(value).quantize(decimal.Decimal("0.01"), decimal.ROUND_HALF_UP) if value else None
E decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
test_deserlalize.py:9: InvalidOperation
Sample class definition:
@deserialize.parser("value", float)
class Thing:
value: Optional[float]
Sample data:
{
"value": null
}
Expected results:
We get an instance of Thing
with value
set to None
.
Actual results:
We fail to deserialize since we try and run the parser before we assign None
. Since the float()
method doesn't work on None
, it throws an exception.
Discussion:
I'm not actually 100% sure which way we should be doing this. If we parse first, it allows us to do things like set defaults, etc. when it is None
. We can also easily fix the above by creating a simple wrapper around the float
function to check for None
first. I suspect that the existing implementation is the best option. This issue is for record keeping more than anything.
We can convert a dictionary/list/whatever to objects, but we can't yet go back the other way. This could be extremely useful when doing things like working with APIs where requests need to be generated too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.