mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
json: make enums work with json encode+decode (serialised as string names by default; the old integer one is supported too, using [json_as_number]
) (#17696)
This commit is contained in:
parent
c9345be6de
commit
278e747c7a
4 changed files with 340 additions and 34 deletions
|
@ -18,7 +18,7 @@ fn test_simple() {
|
|||
x := Employee{'Peter', 28, 95000.5, .worker}
|
||||
s := json.encode(x)
|
||||
// eprintln('Employee x: $s')
|
||||
assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
|
||||
assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":"worker"}'
|
||||
y := json.decode(Employee, s)!
|
||||
// eprintln('Employee y: $y')
|
||||
assert y.name == 'Peter'
|
||||
|
@ -95,14 +95,15 @@ fn test_encode_decode_sumtype() {
|
|||
enc := json.encode(game)
|
||||
// eprintln('Encoded Game: $enc')
|
||||
|
||||
assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},1,"Stool",{"_type":"Time","value":${t.unix_time()}}]}'
|
||||
assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},"cat","Stool",{"_type":"Time","value":${t.unix_time()}}]}'
|
||||
|
||||
dec := json.decode(SomeGame, enc)!
|
||||
// eprintln('Decoded Game: $dec')
|
||||
|
||||
assert game.title == dec.title
|
||||
assert game.player == dec.player
|
||||
assert (game.other[2] as Animal) == (dec.other[2] as Animal)
|
||||
assert (game.other[2] as Animal) == .cat
|
||||
assert dec.other[2] == Entity('cat')
|
||||
assert (game.other[4] as time.Time).unix_time() == (dec.other[4] as time.Time).unix_time()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue