mikewooster / jsonmarshal Goto Github PK
View Code? Open in Web Editor NEWmarshal json to/from dataclasses
License: MIT License
marshal json to/from dataclasses
License: MIT License
python3.11 setup.py build
Installing 'jsonmarshal.utils' as data is deprecated, please list it in `packages`.
!!
############################
# Package would be ignored #
############################
Python recognizes 'jsonmarshal.utils' as an importable package,
but it is not listed in the `packages` configuration of setuptools.
'jsonmarshal.utils' has been automatically added to the distribution only
because it may contain data files, but this behavior is likely to change
in future versions of setuptools (and therefore is considered deprecated).
Please make sure that 'jsonmarshal.utils' is included as a package by using
the `packages` configuration field or the proper discovery methods
(for example by using `find_namespace_packages(...)`/`find_namespace:`
instead of `find_packages(...)`/`find:`).
You can read more about "package discovery" and "data files" on setuptools
documentation page.
!!
this seems to be solved by adding the utils into the setup.py, but as I am just stumbling on this I am showing what I have
First things first: thank you for this great package!
@dataclass
...: class XXX:
...: a: int
...:
In [4]: marshal(XXX(a=1))
Out[4]: {'a': 1}
In [5]: x = marshal(XXX(a=1))
In [6]: unmarshal(x, XXX)
Out[6]: XXX(a=1)
In [7]: @dataclass
...: class YYY(XXX):
...: b: int
...:
In [8]: x = marshal(YYY(a=1, b=2))
In [9]: x
Out[9]: {'a': 1, 'b': 2}
In [10]: unmarshal(x, YYY)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-10-dc946d818557> in <module>
----> 1 unmarshal(x, YYY)
~/.local/lib/python3.8/site-packages/jsonmarshal/unmarshal.py in unmarshal(response, schema, datetime_fmt, date_fmt)
40 """
41 unmarshaller = _Unmarshaller(response, schema, datetime_fmt, date_fmt)
---> 42 return unmarshaller.unmarshal()
43
44
~/.local/lib/python3.8/site-packages/jsonmarshal/unmarshal.py in unmarshal(self)
126
127 processor = self.processors[item.schema_type]
--> 128 processor(item)
129
130 self.promote()
~/.local/lib/python3.8/site-packages/jsonmarshal/unmarshal.py in process_dict(self, item)
170 if not item.cleaned:
171 # Go through each known field and fix the keys in the original dictionary
--> 172 item = self.clean_item(item)
173
174 if not self.dump and item.unmarshalled is False:
~/.local/lib/python3.8/site-packages/jsonmarshal/unmarshal.py in clean_item(self, item)
205
206 for data_key, v in item.data.items():
--> 207 schema_type = item.schema.__annotations__[data_key]
208 child = _ResultContainer(
209 data=v,
KeyError: 'a'
So it's possible to marshal an inherited dataclass, but not to unmarshal it.
from jsonmarshal import unmarshal, json_field
import json
from dataclasses import dataclass
@dataclass
class Item:
first_key: str = json_field()
second_key: int = json_field()
txt_data = '{"first_key":"hi", "second_key":3}'
json_obj = json.loads(txt_data)
item = unmarshal(json_obj, Item)
print(item)
calling unmarshal
fails if the class contains an optional nested dataclass, even when the json in question was created using the marshal
method.
to reproduce, copy and paste this into iPython
from jsonmarshal import unmarshal, marshal, json_field
from dataclasses import dataclass, field
from typing import List, Set, Optional
from dataclasses import dataclass
@dataclass
class Child:
name : str
@dataclass
class Parent:
child : Optional[ Child ] = json_field( omitempty = True, default = None )
parent = Parent()
child = Child( 'name' )
print('jsonmarshal can marshal/unmarshal both object types individually...\n')
print( marshal( parent ) )
print( marshal( child ) )
print( unmarshal( marshal( parent ), Parent ) )
print( unmarshal( marshal( child ), Child ) )
print('\n\n\n')
parent.child = child
parent_json = marshal( parent )
print( 'jsonmarshal can marshal the object\n' )
print( parent_json )
print( '\n' )
try:
print('unmarshalling with an optional parameter that is a nested class fails')
unmarshal( parent_json, Parent ) # throws an error
except AttributeError as e:
print( str( e ) )
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.