dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

from_json not working properly on typed object

Open dan-bar-dov opened this issue 3 years ago • 4 comments

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 avatar Aug 31 '22 14:08 dan-bar-dov

@dan-bar-dov please, provide additional information.

  • Python version
  • dataclasses-json version
  • OS name

By now, it looks a duplicate of #315

USSX-Hares avatar Sep 13 '22 09:09 USSX-Hares

python 3.10.6 dataclasses-json 0.5.7 ubuntu 20.04.4

dan-bar-dov avatar Sep 13 '22 10:09 dan-bar-dov

I have the same issue, but on Python 3.8.10

This issue doesn't seem to be happening on 3.10.4

isFakeAccount avatar Sep 20 '22 02:09 isFakeAccount

Should be re-tested with 0.5.13

george-zubrienko avatar Jul 20 '23 19:07 george-zubrienko