ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
288 stars 40 forks source link

NoneType error when mapping None value to str #159

Closed dantebarba closed 2 years ago

dantebarba commented 2 years ago

I'm working with an external API that returns a bunch of products. These products sometimes have description and sometimes they don't. Unfortunately the API owners decided to set None as value when there is no value set for the description. I'm trying to map the values to my own model but I'm getting NoneType cannot be deserialized into str error. I don't know if this is intended behavior or not.

if I test with the following I get unable to deserialize

description: str = ""

If i test with the following the field is not mapped

description = ""

Is this intended behavior or it's a bug?

georgeharker commented 2 years ago

Just type it as

Optional[str]

With an

from typing import Optional

At the top of the file.

None isn't a valid int- but it is a valid value for optional int

dantebarba commented 2 years ago

It works! Thanks :)