json: fix [omitempty] with string (#17813)

This commit is contained in:
Felipe Pena 2023-03-30 17:09:47 -03:00 committed by GitHub
parent 57aa4def62
commit cd6cc65ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 19 deletions

View file

@ -0,0 +1,25 @@
import json
pub struct MyStruct {
pub mut:
code int
message string
data string [omitempty]
data2 ?string [omitempty]
}
fn test_simple() {
obj := MyStruct{
code: 1
message: 'yes'
data2: 'a'
}
assert dump(json.encode(obj)) == '{"code":1,"message":"yes","data2":"a"}'
}
fn test_none() {
obj := MyStruct{
code: 1
}
assert dump(json.encode(obj)) == '{"code":1,"message":""}'
}