Closed mattjohnsonpint closed 5 months ago
I'll add a few ways to configure properly in the next major release
Including this
Okay, so what I'm doing would be used like so:
@json
class Foo {
@omitnull
optionalNumber: Box<i32> | null = null;
@omitif("this.tristateValue!.unwrap() == false")
tristateValue: Box<bool> | null = null;
}
const foo: Foo = {
optionalNumber: null,
tristateValue: Box.from(true)
}
console.log(JSON.stringify(foo));
const p1 = JSON.parse<Box<i32> | null>("null");
console.log(JSON.stringify<Box<i32> | null>(p1));
console.log(changetype<usize>(p1).toString())
const p2 = JSON.parse<Foo>("{\"optionalNumber\":null,\"tristateValue\":false}");
console.log(JSON.stringify(p2));
the user can insert their own conditions for it to be omitted
Its a bit unconventional, but it should work
It would be useful for
JSON.stringify
to have some capability to omit empty fields.Something like this:
Though it may be tricky to define "empty". If a field is type
string | null
(orSomeObject | null
) then empty would imply nullable, not empty string:Though, AssemblyScript doesn't allow primitives to be nullable (ie.,
i32 | null
won't compile). So maybe there's also a need for another syntax, such as:Or maybe
@omitwhen("")
and@omitwhen(null)
could work, then you'd only need one.Your call, of course. 😄