question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing @property

See original GitHub issue

Question

  • OS: Linux(Ubuntu 18)
  • Python version 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
  • Pydantic version 1.0

I’ve pored over the documentation and issue and can’t seem to find an example for serializing members other than class attributes.

class Rectangle(BaseModel):
    width: int
    length: int

    @property
    def area(self) -> int:
        return self.width * self.length

r = Rectangle(width=10, length=5)

print(r.json())

# What I'd like to see:
{ "width": 10, "length": 5, "area": 50 }

I’d also think that properties without setters would default to readOnly on the schema side as well.

It did seem there was a way to get something close to what I’m after via ORM mode and masking a property on the ORM model, but I’d hate to need to create a class just to contain properties.

Going over BaseModel._calculate_keys it seems BaseModel will not yield what I’m after.

Still, I’m hoping that it’s just been a long day and I overlooked something 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:238
  • Comments:62 (12 by maintainers)

github_iconTop GitHub Comments

56reactions
bradodarbcommented, Oct 25, 2019

I wonder if implementing something like the schema/field as a decorator would be a good approach?

class Rectangle(BaseModel):
    width: bool
    length: int

    @property
    @field(title='Area')
    def area(self) -> int:
        return self.width * self.length

And on init, the internal fields dict could grab attributes that have the decorator and act accordingly.

I’d say the property above would set to readOnly on the schema output.

Could possibly used on methods to support writeOnly ?

I’d be happy to get a PR going with some guidance if this feature is desired by others (esp the property serializtion, not so much the method)

34reactions
PrettyWoodcommented, Jul 16, 2021

Hey everyone I took some time to work again on computed fields to add some validation and also support the syntax suggested by @JosXa. You can have a full example of what’s possible in the description of #2625 or in the doc that I just wrote. I’m really looking for feedback. Since Samuel is currently overwhelmed, the more it’s tested and approved beforehand the more confident we can be when the review is done. Have a great weekend! Thanks 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why are properties without a setter not serialized
A property setter is not required for serialization. However, if a property does not have a setter, the XML will not deserialize to...
Read more >
How to serialize properties of derived classes with System ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >
Scripting API: SerializedProperty - Unity - Manual
SerializedProperty and SerializedObject are classes for editing properties on objects in a completely generic way that automatically handles undo, multi-object ...
Read more >
Conditional Property Serialization - Json.NET
Json.NET has the ability to conditionally serialize properties by placing a ShouldSerialize method on a class. This functionality is similar to the ...
Read more >
Serialize C# Properties (how-to with code) - Unity Forum
Oh, I see. You can only serialize data. A property that basically acts as a getter/setter for your own data, still has a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found