Code Monkey home page Code Monkey logo

deserialize's People

Contributors

dalemyers avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

deserialize's Issues

what is the difference with json.loads ?

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

Special case deserializing enum errors

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.

JSON compatible attribute names (camelCased)

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!

Unions between base types don't appear to work

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].

Enum support?

I haven't found a way to make it deserialize Enums. Am I missing anything or it is not simply supported?

Attribute parser gets redefined by another subclass of common ancestor

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

null should be checked before the parser is run for optional types

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.

Inverse operations should be supported

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.