Giter Site home page Giter Site logo

Comments (4)

arturstorm avatar arturstorm commented on June 16, 2024 2

@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.

nathan5280 avatar nathan5280 commented on June 16, 2024

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.

r-richmond avatar r-richmond commented on June 16, 2024

At the risk of being too verbose and exposing some potential ignorance, I'd just like to say

  1. Awesome project lidatong
  2. 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.

RunOrVeith avatar RunOrVeith commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.