Skip to content

Support strict setting in all decoders - #434

Merged
jcrist merged 10 commits into
mainfrom
lax-mode
Jun 11, 2023
Merged

Support strict setting in all decoders#434
jcrist merged 10 commits into
mainfrom
lax-mode

Conversation

@jcrist

@jcrist jcrist commented Jun 11, 2023

Copy link
Copy Markdown
Member

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.

@jcrist
jcrist merged commit e93128a into main Jun 11, 2023
@jcrist
jcrist deleted the lax-mode branch June 11, 2023 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling of misformatted JSON

1 participant