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

Bug `with_dump` only support `json`, not support `dumps` #175

Open nightan42643 opened 2 years ago

nightan42643 commented 2 years ago

Hey

Does with_dump only support json method but not support dumps method?

@dataclass
class Person(JsonSerializable
                .with_dump(key_transformer=KEY_TRANSFORMER_CAMELCASE)
                .with_load(key_transformer=KEY_TRANSFORMER_SNAKECASE)):
    first_name: str
    last_name: str

arno = Person('Arno','Y')

print(arno.dumps())
print(arno.json)

Result:

{"first_name": "Arno", "last_name": "Y"}
{'firstName': 'Arno', 'lastName': 'Y'}
ramonhagenaars commented 2 years ago

Hi @nightan42643,

The .dumps() works, it returns a string. It's just not clear when you only use print, but if you call type, you'll see:

@dataclass
class Person(JsonSerializable
                .with_dump(key_transformer=KEY_TRANSFORMER_CAMELCASE)
                .with_load(key_transformer=KEY_TRANSFORMER_SNAKECASE)):
    first_name: str
    last_name: str

arno = Person('Arno','Y')

print(type(arno.dumps()))
print(type(arno.json))

Result:

<class 'str'>
<class 'dict'>

😉

nightan42643 commented 2 years ago

Hi @nightan42643,

The .dumps() works, it returns a string. It's just not clear when you only use print, but if you call type, you'll see:

@dataclass
class Person(JsonSerializable
                .with_dump(key_transformer=KEY_TRANSFORMER_CAMELCASE)
                .with_load(key_transformer=KEY_TRANSFORMER_SNAKECASE)):
    first_name: str
    last_name: str

arno = Person('Arno','Y')

print(type(arno.dumps()))
print(type(arno.json))

Result:

<class 'str'>
<class 'dict'>

😉

But in this case, the dumps() didn't transfer to the CAMELCASE, right?