I'd like to handle optionals via marshalling:
JSON.registerObjectMarshaller(Optional) { Optional optional ->
if (optional.present)
return optional.get()
else
return null
}
assert (["Test": Optional.empty()] as JSON).toString(false) == '{"Test":null}' // throws out of sequence exception.
It appears to fail because the ClosureObjectMarshaller doesn't write a null value when the closure returns a null.