jcrist · GitHub

This adds a new strict boolean config kwarg to all decode functions (as well as all Decoder classes). The default is True, matching the existing strict type coercion rules. Setting to False enables a wider set of coercion rules, which may be useful in some scenarios. Currently setting strict=False enables coercing:

  • the string "null" (case insensitive) to None
  • the strings "false" or "0" (case insensitive) to False
  • the strings "true" or "1" (case insensitive) to True
  • any int-like string to an integer
  • any float-like string to a float

Additional coercion rules may be added to strict=False ("lax mode") in the future.

Note that coercion still only happens if the requested type (e.g. int) doesn't match the provided type (e.g. str). When not using msgspec.json.decode without a type specified no coercion will happen.

In [1]: import msgspec
In [2]: msg = b'["1", "2"]'  # a list of int-like strings
In [3]: msgspec.json.decode(msg, strict=False)  # untyped decoding, no coercion
Out[3]: ['1', '2']
In [4]: msgspec.json.decode(msg, type=list[int])  # typed decoding, errors by default
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[4], line 1
----> 1 msgspec.json.decode(msg, type=list[int])
ValidationError: Expected `int`, got `str` - at `$[0]`
In [5]: msgspec.json.decode(msg, type=list[int], strict=False)  # typed decoding, strict=False, coerces str -> int
Out[5]: [1, 2]

Fixes #424.

Read the original on github.com ↗