dataclasses-json
dataclasses-json copied to clipboard
from_json not working properly on typed object
The following example demonstrates my problem: class MIT is not loaded properly from json - the dict keys are strings, where they should have been integers.
import dataclasses
import typing
import dataclasses_json
MyInt = typing.NewType("MyInt", int)
@dataclasses_json.dataclass_json
@dataclasses.dataclass
class MIT:
root: int
records: dict[MyInt, str]
@dataclasses_json.dataclass_json
@dataclasses.dataclass
class TIM:
root: int
records: dict[int, str]
dd = {1: 'str1',
2: 'str2',
3: 'str3',
}
di = {MyInt(1): 'str1',
MyInt(2): 'str2',
MyInt(3): 'str3',
}
xft = MIT(root=0, records=di)
jft = xft.to_json()
yft = MIT.from_json(jft)
print(xft,yft)
xft = TIM(root=0, records=dd)
jft = xft.to_json()
yft = TIM.from_json(jft)
print(xft,yft)
the output:
MIT(root=0, records={1: 'str1', 2: 'str2', 3: 'str3'}) {"root": 0, "records": {"1": "str1", "2": "str2", "3": "str3"}} MIT(root=0, records={'1': 'str1', '2': 'str2', '3': 'str3'})
TIM(root=0, records={1: 'str1', 2: 'str2', 3: 'str3'}) {"root": 0, "records": {"1": "str1", "2": "str2", "3": "str3"}} TIM(root=0, records={1: 'str1', 2: 'str2', 3: 'str3'})
Is it a bug? or did I do something wrong?
@dan-bar-dov please, provide additional information.
- Python version
- dataclasses-json version
- OS name
By now, it looks a duplicate of #315
python 3.10.6 dataclasses-json 0.5.7 ubuntu 20.04.4
I have the same issue, but on Python 3.8.10
This issue doesn't seem to be happening on 3.10.4
Should be re-tested with 0.5.13