Changes between Version 78 and Version 79 of pyson
- Timestamp:
- 07/18/24 14:35:49 (9 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
pyson
v78 v79 3 3 == Pyson 4 4 5 Pyson converts between dicts/lists and python3 objects. 5 Pyson 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. 6 6 7 7 The 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. … … 10 10 11 11 12 ===== Why not @datatype 12 ===== Existing libraries 13 Existing libraries do not offer the features we need. 14 13 15 The @datatype annotation from python offers serialization through the {{{asdict}}} method. 14 16 However 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). … … 17 19 18 20 But even then polymorphic classes can not be deserialized, because the serialized form just does not contain the information required to do this. 19 20 21 21 22 22 === Installation … … 49 49 50 50 After deserializing the arguments, the default constructor {{{__init__}}} is used to create the object. This enforces the proper construction, including all argument checks etc. 51 52 If 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. 51 53 52 54 === Deserialization … … 63 65 64 66 === Serialization 65 Serialization of an object is much more straightforward. 67 Serialization (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. 66 70 * 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) 67 71 * If the object is an instance of a class with @JsonSubtypes, add/wrap the json with class info according to the @JsonTypeInfo … … 84 88 In JSON, keys of dictionaries/maps are restricted to strings. In Python however, dicts usually contain general objects as keys. 85 89 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. 90 To 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 92 If MyObject is using @JsonValue to directly map to a str, then that string is directly used for the mapping to and from json. 87 93 88 94 === Exceptions