How to use the mashumaro.MissingField function in mashumaro

To help you get started, we’ve selected a few mashumaro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Fatal1ty / mashumaro / tests / test_json.py View on Github external
def test_from_json_with_custom_decoder():
    @dataclass
    class DataClass(DataClassJSONMixin):
        x: List[int]
        x_count: int

    def decoder(s):
        d = json.loads(s)
        d['x_count'] = len(d.get('x', []))
        return d

    instance = DataClass(x=[1, 2, 3], x_count=3)
    dumped = json.dumps({'x': [1, 2, 3]})
    assert DataClass.from_json(dumped, decoder=decoder) == instance
    with pytest.raises(MissingField):
        assert DataClass.from_json(dumped)