Changes between Version 78 and Version 79 of pyson


Ignore:
Timestamp:
07/18/24 14:35:49 (9 months ago)
Author:
wouter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • pyson

    v78 v79  
    33== Pyson
    44
    5 Pyson converts between dicts/lists and python3 objects.
     5Pyson converts between dicts/lists and python3 objects. Note that this differs from jackson (a similar library for java), where conversion is done between strings and java objects.
    66
    77The basic version determines the types for (de)serialization from the {{{__init__}}} function in the involved classes. Polymorphism is supported, so derived classes can (de)serialized from a superclass.
     
    1010
    1111
    12 ===== Why not @datatype
     12===== Existing libraries
     13Existing libraries do not offer the features we need.
     14
    1315The @datatype annotation from python offers serialization through the {{{asdict}}} method.
    1416However it does not support polymorphism, it does not include the information needed to properly support polymorphism, and  it does not offer the reverse method (e.g. from-dict).
     
    1719
    1820But even then polymorphic classes can not be deserialized, because the serialized form just does not contain the information required to do this.
    19 
    20 
    2121
    2222=== Installation
     
    4949
    5050After deserializing the arguments, the default constructor {{{__init__}}} is used to create the object. This enforces the proper construction, including all argument checks etc.
     51
     52If your class has a constructor {{{__init__(self, X)}}} and you add @JsonValue to the getX method in your class, then your class will (de)serialize as if it's just an X, so without wrappers.
    5153
    5254=== Deserialization
     
    6365
    6466=== Serialization
    65 Serialization of an object is much more straightforward.
     67Serialization (conversion to a json object) of a python object is much more straightforward.
     68* If there is a @JsonSerialize annotation, use that
     69* If there is a @JsonValue annotated getter, then call the getter and serialize that.
    6670* Create a json dict, with keys the arguments of the {{{__init__}}} function of the object and the value the serialized value returned by the getter (also considering @JsonGetter)
    6771* If the object is an instance of a class with @JsonSubtypes, add/wrap the json with class info according to the @JsonTypeInfo
     
    8488In JSON, keys of dictionaries/maps are restricted to strings. In Python however, dicts usually contain general objects as keys.
    8589
    86 To work around this JSON restriction, pyson recognises from your specified type (eg {{{Dict[MyObject, str]}}}) whether the key is an object. In that case, a conversion is done between the python object (MyObject) and a json string, recursively using the standard pyson parse and toJson functions.
     90To work around this JSON restriction, pyson recognises from your specified type (eg {{{Dict[MyObject, int]}}}) whether the key is an object. In that case, a conversion is done between the python object (MyObject) and a json string, recursively using the standard pyson parse and toJson functions.
     91
     92If MyObject is using @JsonValue to directly map to a str, then that string is directly used for the mapping to and from json.
    8793
    8894=== Exceptions