Comments (4)
@nathan5280, thanks for answer!
It will be grate to have functionality like in Jakson(java lib) @JsonIgnoreProperties(ignoreUnknown = true)
:
@dataclass_json(unknown=EXCLUDE)
@dataclass(frozen=True)
class Minion:
name: str
from dataclasses-json.
I've started poking around with dataclasses-json as a replacement for my homegrown version of this functionality and anticipate running into this issue. I haven't had a chance to look at how to solve this cleanly, but came up with this work around. It wouldn't be too hard to do this recursively.
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import List
from marshmallow import EXCLUDE, ValidationError
@dataclass_json
@dataclass(frozen=True)
class Minion:
name: str
@dataclass_json
@dataclass(frozen=True)
class Boss:
minions: List[Minion]
boss_json = """
{
"minions": [
{
"name": "evil minion",
"UNKNOWN_PROPERTY" : "value"
},
{
"name": "very evil minion"
}
],
"UNKNOWN_PROPERTY" : "value"
}
""".strip()
def main():
boss_schema = Boss.schema(unknown=EXCLUDE)
try:
boss_schema.loads(boss_json, unknown="exclude")
except ValidationError:
pass
boss_schema.fields["minions"].container.unknown = EXCLUDE
boss = boss_schema.loads(boss_json)
print(boss)
if __name__ == "__main__":
main()
What I've done is to generate the Boss Schema and then go in and get the nested Minion Schema and change it's unknown attribute to EXCLUDE. This now runs cleanly and gets the Expected result.
from dataclasses-json.
At the risk of being too verbose and exposing some potential ignorance, I'd just like to say
- Awesome project lidatong
- The functionality arturstorm describes would also be amazing; in my case I'm only interested in some high-level portions of a rest-api response but based on my understanding of the project I had to completely type out the entire response to use
dataclass_json
.
from dataclasses-json.
I will submit a pull request for a general solution to this.
I have it working using the from_dict
methods, but not the schema yet. But I think I know how to add it there as well, I will look at that tomorrow.
There will be options such as
@dataclass_json(undefined_parameters=UndefinedParameters.EXCLUDE)
@dataclass_json(undefined_parameters=UndefinedParameters.RAISE)
@dataclass_json(undefined_parameters=UndefinedParameters.INCLUDE)
I'll link the pull request here when I make it.
from dataclasses-json.
Related Issues (20)
- [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...
- [BUG] 0.6.2 changes CatchAll behavior in unexpected ways HOT 2
- [FEATURE] Omitting undefined Optional field when serializing HOT 2
- [BUG] Nested tuple with union of tuple does not deserialize correctly HOT 7
- [BUG] Nullable fields are not showing up when deserializing with field(default=None, metadata=config(exclude=lambda x: x is None)) HOT 4
- [BUG] Generic Collections fields - not respecting `default_factory` `field` specifications
- [BUG] new key "dataclass_json_config": null HOT 1
- AttributeError: 'list' object has no attribute 'keys' HOT 3
- [BUG] Decoding incompatible with Freezegun
- AttributeError: 'list' object has no attribute 'items' HOT 1
- [BUG] encoders / decoders don't apply to list / dict of the class
- [BUG] Getting `UserWarning` for defining an attribute with type `dict[str, bytes] | None`
- decoding optional decimal fields
- Parse JSON without modifying the dataclass HOT 1
- [BUG] SNAKE LetterCase conversion adding extra underscores for numeric parts HOT 1
- [BUG] SNAKE LetterCase conversion adding extra underscores for numeric parts
- [BUG] decorator @validates_schema not working when schema loads
- [FEATURE] Raise ValidationError if JSON doesn't match any of the types in Union
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.