Serializing @property
See original GitHub issueQuestion
- 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:
- Created 4 years ago
- Reactions:238
- Comments:62 (12 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I wonder if implementing something like the schema/field as a decorator would be a good approach?
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)
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 🙏