-
Notifications
You must be signed in to change notification settings - Fork 284
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
why optional changes behaviour for null values #2673
Comments
This is because it treats a vibe.d/data/vibe/data/serialization.d Line 911 in 7161dc1
I think this is an incorrect design decision, but I'm not sure if it's changeable. Though, it might be possible to say if the type has a construction mechanism that accepts either |
Then, the only solution to manage undefined/null as "not the same" is to avoid @optional and coding manually the Json conversion... a little annoying :-) struct Person {
DVal!string name;
static Person fromRepresentation(const Json json)
{
Person result = {
name: json["name"].deserializeJson!(DVal!string)
};
return result;
}
Json toRepresentation() const =>
Json([
"name": name.serializeToJson
]);
} |
I kind of missed its introduction to the code base, but it looks like using |
I will try to summarize: I have an struct Example: When calling deserializeJson!Person there is 3 possible cases for each member:
After adding
- Why Json(null) is treated differently when @optional is present? it is a valid json value and it can be deserialized the same way it was without de @optional attribute. Conclusion: It's impossible to implement an algebraic Undefined | Null | T type based on vibe-d Json deserialization. |
What I meat is something like this: struct DNVal(T) {
Kind kind;
T value;
Json toJson() const { return kind == Kind.is_null ? Json(null) : Json(value); }
static DNVal fromJson(Json v) { return v.isNull ? DNVal(Null._) : DNVal(v.get!string); }
}
alias DVal(T) = Nullable!(DNVal!T);
struct Person { @embedNullable DVal!string name; } So the "undefined" state wouldn't be represented by the algebraic type anymore, but by the wrapping BTW, I agree that the handling of That could be an additional UDA, or it could use the presence/signature of the Previous issue: #1793 |
Third option is what I personally expected when begun to implement I finally implemented a template mixin that generates struct Person{
DVal!string name;
DVal!string surname;
DVal!int tallness;
mixin JsonRepresentationMixin!Person
} I can close this issue if you prefer (there is #1793 pointing the same problem) |
@optional attribute, thought for managing "not present" json values (undefined ones) is changing how "null" value is treated:
I have an struct that can represent, internally, Null, Undefined or Value.
This is a simplified version
I create a simple struct to validate how null is seriallized/deserialized
Then, I add the @optional attribute to manage cases where a property is not present in the json (it is the real reason to use the DVal). This cases are deserialized properly.
The problem appears when "name" is present on JSON and its value is null . The "toRepresentation" method is not called
@optional is perfect for managing cases where a property is not present on a JSON, but - why it changes behaviour of present properties? Why null is treated in a different way when @optional is present (Null is a valid value, not an undefined one).
The text was updated successfully, but these errors were encountered: