Skip to content
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

Handle null values in XML as well #49

Merged
merged 1 commit into from
Feb 28, 2025
Merged

Handle null values in XML as well #49

merged 1 commit into from
Feb 28, 2025

Conversation

einarmo
Copy link
Contributor

@einarmo einarmo commented Feb 27, 2025

This is the same as what we do for JSON, we make it possible to leave out values that are null, which is important for certain special values like UAString, where an empty value is different from a null value.

I considered making this a separate trait, but it would be annoying to have to derive a trait that does nothing most of the time, and without specialization there's no better way. We also can't put it on BinaryEncodable since that trait isn't implemented for Option, and can't really be at the moment. It's now effectively duplicated between JsonEncodable and XmlEncodable, which is fine.

This is the same as what we do for JSON, we make it possible to leave
out values that are null, which is important for certain special values
like UAString, where an empty value is different from a null value.

I considered making this a separate trait, but it would be annoying to
have to derive a trait that does nothing most of the time, and without
specialization there's no better way. We also can't put it on
BinaryEncodable since that trait isn't implemented for `Option`, and
can't really be at the moment. It's now effectively duplicated between
JsonEncodable and XmlEncodable, which is fine.
/// This method should return `true` if the value is default
/// and should not be serialized.
fn is_null_xml(&self) -> bool {
false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess this method will never return true. Is that expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the trait default, it's overridden in certain types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's defined in a trait, wouldn't it be better to not implement it as a default false but require it to be implemented by anyone that implements this trait for a type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that, I guess. It would be a lot of noise though, and most implementations would simply be false.

If we do this, I'd rather do it separately, since we would want to do the same for JSON. Maybe combine it with moving this to a separate trait?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondered if this could cause unexpected behavior were a type forgets to implement/overwrite the default. But as the default is false I guess that would be unlikely.

/// This method should return `true` if the value is default
/// and should not be serialized.
fn is_null_xml(&self) -> bool {
false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondered if this could cause unexpected behavior were a type forgets to implement/overwrite the default. But as the default is false I guess that would be unlikely.

@einarmo
Copy link
Contributor Author

einarmo commented Feb 27, 2025

It's not an entirely unreasonable thought, and it's a danger of default implementations. In this case, leaving it false is almost never wrong, however, since the vast majority of types are fine with being deserialized to null.

The only real exception is UAString, which has the unfortunate property that

<String></String>

and

<String />

mean different things. One is an empty string, the other is a null string. Since the type only governs the content of the tag in our XML serializer, it means we can deserialize the latter, and serialize that back as the first without this feature.

For every other type, as well as for all types in JSON, the only advantage is strict standards compliance (the standard wants you to leave out "null" values in some cases), and data size reduction.

@einarmo einarmo merged commit 4923abd into master Feb 28, 2025
6 checks passed
@einarmo einarmo deleted the xml-handle-null branch February 28, 2025 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants