Comments (3)
I am not sure what is happening here:
with open('storage.json', 'w') as f:
json.dump(johnny.to_json(), f)
johnny.to_json
already returns a string. If you do f.write(johnny.to_json())
, do you still have an issue?
from dataclasses-json.
johnny.to_json already returns a string. If you do f.write(johnny.to_json()), do you still have an issue?
Yes, the reason I am writing to a file in this example is that Person.from_json(johnny.to_json())
would indeed work as expected, the reason being that johnny.to_json would not change the names
inner elements to string, it would leave them as they are and therefore would not convert them back in the from_json
function.
When this is written to a file, the text: '{"names": ["John Doe"]}'
is dumped into it. when attempting to convert that into Person via the Person.from_json
function, the type of the names elements will indeed be incorrect.
In short, the writing into a file is not required and the following will also reproduce this bug:
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
class Name(str):
...
@dataclass
class Person(DataClassJsonMixin):
names: list[Name]
if __name__ == '__main__':
js = '{"names": ["John Doe"]}'
print(isinstance(Person.from_json(js).names[0], Name)) # prints: False
from dataclasses-json.
I'll retest this on latest commit tomorrow to see if this is some similar issue with 3.10 as with self-types
from dataclasses-json.
Related Issues (20)
- [FEATURE] V1 - JsonSerializer should support `list`, `dict` and types from `typing` library that are supported in v0
- [FEATURE] V1 - Class Hierarchy SerDe
- [FEATURE] V1 - Automatic serializer registration from module
- [BUG] Incompatibility with from __future__ import annotations Affects Overriding of Global Config in Python 3.11 HOT 4
- Dataclasses containing variables that reference themselves in list/dict fail to re-create as original type HOT 13
- [BUG] Generated schema doesn't take into account custom decoder
- [Guess] Looks like we have CRLF sneaked to the source code tree HOT 4
- [BUG] global configuration of encoder/decoder for type T is not used for Optional[T] field HOT 5
- [BUG] Type coercion since 0.5.9 HOT 17
- _decode_dataclass ignores InitVars because of use of fields HOT 5
- [BUG] "Supported types" documentation is not up to date HOT 1
- [FEATURE] V1 - Type coercions feature
- [BUG] PyPI release broken due to GH changes to env protection HOT 16
- [BUG] Type hinting HOT 1
- [FEATURE] Use existing marshmallow schema
- [BUG] Dataclass.from_json with Union type fields infers missing fields and fails in the wrong spot HOT 1
- [minimal-repro-testcase] from v0.5.12 on, tuple fields get truncated HOT 5
- [BUG] No way to default explicit nulls/Nones to a value
- [BUG] Numpy decode issue...
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataclasses-json.