Git Product home page Git Product logo

Comments (2)

ramonhagenaars avatar ramonhagenaars commented on September 27, 2024 1

Hi @nightan42643,

The reason for .json to return an object and not a dict is a bit theoretical. One could be writing a class that is not to be represented as a dict when turned into json, for example a custom collection class:

class MyCollection(list, JsonSerializable):
    ...

c = MyCollection((1, 2, 3))
c.json  # <-- this results in a list, not a dict.

MyCollection could not override .json to hint the right type (list instead of dict) as that would be a violation of the Liskov Substitution Principle. But since JsonSerializable hints .json as returning an object, it is possible to override and hint list (or dict in your case) since that is a subtype of object. Liskov can rest assured.

In your case, you could override as follows:

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

    @property
    def json(self) -> dict[str, str]:
        return JsonSerializable.json.fget(self)

Or maybe even like this:

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

    json: dict[str, str] = field(init=False, repr=False, compare=False)

Alternatively, you could use typing.cast:

arno_json = typing.cast(dict[str, str], arno.json)

I hope this helps.

from jsons.

nightan42643 avatar nightan42643 commented on September 27, 2024

@ramonhagenaars Very clear, thanks!

from jsons.

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.