-
Notifications
You must be signed in to change notification settings - Fork 5k
System.Text.Json - Support property flattening - [JsonPropertyFlatten] #55120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsI propose https://stackoverflow.com/questions/65323277/flatten-an-object-with-system-text-json Imagine I have a class like this: public class ResponseBase
{
[JsonPropertyName("status")]
[JsonConverter(typeof(StatusConverter))]
public bool IsSuccessStatus { get; set; }
[JsonPropertyName("error_message")]
public string? ErrorMessage { get; set; }
[JsonPropertyName("error_code")]
public string? ErrorCode { get; set; }
} If flattening was supported I could refactor it to something like this: public class Error
{
[JsonPropertyName("error_code")]
public string Code { get; set; }
[JsonPropertyName("error_message")]
public string Message { get; set; }
}
public class ResponseBase
{
[JsonPropertyName("status")]
[JsonConverter(typeof(StatusConverter))]
public bool IsSuccessStatus { get; set; }
[JsonPropertyFlatten]
public Error Error { get; set; }
} But it would still serialize and deserialize like
|
This could be implemented using source generators. A source generator would have to:
Using a source generator, the following code: public class ResponseBase
{
[JsonPropertyName("status")]
[JsonConverter(typeof(StatusConverter))]
public bool IsSuccessStatus { get; set; }
[JsonPropertyFlatten]
public Error? Error { get; set; }
} Could be converted to: public class ResponseBase
{
[JsonPropertyName("status")]
[JsonConverter(typeof(StatusConverter))]
public bool IsSuccessStatus { get; set; }
[JsonPropertyFlatten]
[JsonIgnore]
public Error? Error { get; set; }
[JsonPropertyName("error_code")]
private string Code
{
get => Error.ErrorCode;
set => Error.ErrorCode = value;
}
[JsonPropertyName("error_message")]
private string Message
{
get => Error.Message;
set => Error.Message = value;
}
} |
How would such a mechanism deal with potentially conflicting property names (same key occurring in both parent and child objects)? How does serde deal with flattened types whose serialization is a number or array? It's unlikely we would consider such a feature for 7.0.0. My recommendation is to use a custom converter (should be easier to achieve using the new |
This issue has been marked with the When ready to submit an amended proposal, please ensure that the original post in this issue has been updated, following the API proposal template and examples as provided in the guidelines. |
The same way as it deals with this. It would just treat all the properties of the flattened property as they were part of the same object. I think it should just throw an exception. [JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("name")]
public string Name2 { get; set; }
Not sure about that. But it should just support |
ofc we should able to flat array or any class with indexer. Just introduce new attribute (or constructor overload for And we get something like that:
|
I propose
[JsonPropertyFlatten]
attribute that can be used to flatten property. Similar to#[serde(flatten)]
in Rust's Serde.https://stackoverflow.com/questions/65323277/flatten-an-object-with-system-text-json
Imagine I have a class like this:
If flattening was supported I could refactor it to something like this:
But it would still serialize and deserialize like
Error
properties were part of theResponseBase
class instead of"error": {...}
because I'm writing a client for third party api...Possibly blocking? #31257
The text was updated successfully, but these errors were encountered: