Add check to ensure serialization code only added when underlying value can be serialized.
Build and Test Module / build-and-test (push) Successful in 6s Details
Build and Test Module / build-and-test-with-asdf-library (push) Successful in 11s Details

This commit is contained in:
Andrew Lalis 2026-06-14 13:54:10 -04:00
parent 416e0b220f
commit 8abb3a2c42
1 changed files with 32 additions and 28 deletions

View File

@ -88,36 +88,40 @@ struct Optional(T) {
version (Have_asdf) {
import asdf;
/**
* Deserializes an optional value from raw ASDF data. This function
* is defined to allow for automatic deserialization of Optionals when
* parsing JSON or other data types.
* Params:
* data = The data representing an optional value.
* Returns: An exception if one is thrown.
*/
SerdeException deserializeFromAsdf(Asdf data) {
if (data == null) {
this.isNull = true;
this.value = T.init;
} else {
this.isNull = false;
this.value = deserialize!T(data);
static if (
__traits(compiles, deserialize!T(Asdf.init))
) {
/**
* Deserializes an optional value from raw ASDF data. This function
* is defined to allow for automatic deserialization of Optionals when
* parsing JSON or other data types.
* Params:
* data = The data representing an optional value.
* Returns: An exception if one is thrown.
*/
SerdeException deserializeFromAsdf(Asdf data) {
if (data == null) {
this.isNull = true;
this.value = T.init;
} else {
this.isNull = false;
this.value = deserialize!T(data);
}
return null;
}
return null;
}
/**
* Serializes an optional value to allow for the automatic serialization
* of Optionals when writing JSON or other data types.
* Params:
* serializer = The serializer to use (provided by ASDF).
*/
void serialize(S)(ref S serializer) {
if (this.isNull) {
serializer.putValue(null);
} else {
serializer.putValue(this.value);
/**
* Serializes an optional value to allow for the automatic serialization
* of Optionals when writing JSON or other data types.
* Params:
* serializer = The serializer to use (provided by ASDF).
*/
void serialize(S)(ref S serializer) {
if (this.isNull) {
serializer.putValue(null);
} else {
serializer.putValue(this.value);
}
}
}
}