Giter VIP home page Giter VIP logo

jsonmarshal's People

Contributors

georgettica avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

eirikmortensen

jsonmarshal's Issues

jsonmarshal.utils is ignored

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

Cannot unmarshal inherited dataclass

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.

add more explicit example for newbies

  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)

optional json field fails to unmarshal when the element is a nested dataclass

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 ) )

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.