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