@@ -31,25 +31,30 @@ may change.
3131
3232### Supported types
3333
34- * Atoms including ` None ` , ` bool ` , ` int ` , ` float ` , ` str `
35- * The ` decimal.Decimal ` class, represented as itself and in string form.
34+ * Atoms including ` None ` , ` bool ` , ` int ` , ` float ` , ` str ` .
35+ * Floats may optionally be represented as strings.
36+ * The ` decimal.Decimal ` class, represented as itself or in string form.
3637 * The ` datetime.date ` and ` datetime.datetime ` classes, represented in ISO8601 form.
38+ * Preliminary support for ` datetime.timedelta ` as ISO8601 time durations.
3739 * Subclasses of ` enum.Enum ` , represented by the string names.
3840 * Also, a ` faux_enums ` rule will accept an Enum type if you just use strings in your
3941 code.
4042 * The ` typing.Optional[E] ` type allows a JSON ` null ` to be substituted for a value.
4143 * Collections including ` typing.List[E] ` , ` typing.Tuple[E, ...] ` , ` typing.Set[E] ` and
4244 ` typing.FrozenSet[E] ` .
43- * The ellipsis is part of the syntax! It indicates a homogenous tuple, essentially a
45+ * The ` ... ` is [ literal ] [ ellipsis ] and indicates a homogenous tuple, essentially a
4446 frozen list.
4547 * The ` typing.Dict[K, V] ` type allows a JSON object to represent a homogenous ` dict ` .
4648 * Restriction: the keys must be strings, ints, enums or dates.
4749 * Python classes implemented using ` attrs.attrs ` , ` dataclasses.dataclass ` are
4850 represented as JSON dicts and
4951 * Named tuples via ` typing.NamedTuple ` and heterogenous tuples via ` typing.Tuple ` .
50- * Really , you probably want to convert these to ` attrs. `
52+ * Though , you should consider converting these to ` dataclass ` .
5153 * The ` typing.Union[A, B, C] ` rule will recognize alternate types by inspection.
5254
55+ In addition, ` dataclass ` and ` attrs ` classes support hooks to let you completely customize
56+ their JSON representation.
57+
5358## Usage
5459
5560This example is also implemented in unit tests. First, let's declare some classes.
@@ -151,11 +156,11 @@ Thus we have:
151156 * ` dict ` and ` Dict[K, V] `
152157
153158Tuple is a special case. In Python, they're often used to mean "frozenlist", so
154- ` Tuple[E, ...] ` (the ` ... ` is the actual syntax! ) indicates all elements have the type
159+ ` Tuple[E, ...] ` (the ` ... ` is [ the Ellipsis object ] [ ellipsis ] ) indicates all elements have the type
155160` E ` .
156161
157162They're also used to represent an unnamed record. In this case, you can use
158- ` Tuple[A, B, C, D] ` or however many types. But don't do that, just make a ` dataclass ` .
163+ ` Tuple[A, B, C, D] ` or however many types. It's generally better to use a ` dataclass ` .
159164
160165The standard rules don't support:
161166
@@ -299,17 +304,18 @@ _Union types._ You can use `typing.Union` to allow a member to be one of some nu
299304alternates, but there are some caveats. You should use the ` .is_ambiguous() ` method of
300305RuleSet to warn you of these.
301306
302- _ Rules accept subclasses ._ If you subclass ` int ` , the atoms rule will match it, and then
303- the converter will call ` int ` against your instance. I haven't taken the time to examine
304- what exactly to do.
307+ _ Atom rules accept specific types ._ At present , the rules for atomic types ( ` int ` ,
308+ ` str ` , ` bool ` , ` date ` , ` float ` , ` Decimal ` ) must be declared as exactly those types. With
309+ multiple inheritance, it's not clear which rule should apply
305310
306311_ Checks are stricter than converters._ For example, a check for ` int ` will check whether
307312the value is an integer, whereas the converter simply calls ` int ` on it. Thus there are
308- many inputs for where ` MyType ` would pass but ` Union[MyType, Dummy] ` will fail. (Note
313+ inputs for where ` MyType ` would pass but ` Union[MyType, Dummy] ` will fail. (Note
309314that ` Optional ` is special cased to look for ` None ` and doesn't have this problem.)
310315
311316_ Numbers are hard._ See the rules named ` floats ` , ` floats_nan_str ` , ` decimals ` ,
312- ` decimals_as_str ` for details on how to get numbers to transmit reliably.
317+ ` decimals_as_str ` for details on how to get numbers to transmit reliably. There is no rule for
318+ fractions or complex numbers as there's no canonical way to transmit them via JSON.
313319
314320## Maintenance
315321
@@ -360,3 +366,4 @@ union. [↩](#a2)
360366[ attrs ] : https://attrs.readthedocs.io/en/stable/
361367[ dataclasses ] : https://docs.python.org/3/library/dataclasses.html
362368[ sharp ] : https://github.com/UnitedIncome/json-syntax/blob/master/README.md#sharp-edges
369+ [ ellipsis ] : https://docs.python.org/3/library/stdtypes.html#the-ellipsis-object
0 commit comments