Description
Go lang's JSON (un)marshal-er includes a handy omitempty
"annotation" for struct fields:
https://golang.org/pkg/encoding/json/
"The 'omitempty' option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string."
There is a similar default behaviour in ta-json, during serialization to JSON:
https://github.com/edcarroll/ta-json/blob/master/lib/methods/serialize.ts#L33
if ((value === null || value === undefined) || p.writeonly) {
return;
}
...as well as during deserialization from JSON:
https://github.com/edcarroll/ta-json/blob/master/lib/methods/deserialize.ts#L41
if ((value === null || value === undefined) || p.readonly) {
return;
}
So, I have been wondering whether an additional @JsonRequired()
decoration could be useful for class members (or conversely: @JsonOptional()
, but this would require changing the current default behaviour). In case of an "empty" value (to be clarified, perhaps not exactly the same definition as Go lang), a property would not be outputted to JSON, and deserialization may "fail" if the property is actually required ("failure" may be an error callback where the API consumer could act accordingly).
...which also brings the question of TypeScript's strictNullChecks
and the ?
question mark to denote optionality:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html
Thoughts welcome :)