mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
json: fix decode struct ptr (#20828)
This commit is contained in:
parent
9aeb8229ae
commit
4f742ad1b2
2 changed files with 30 additions and 1 deletions
22
vlib/json/json_decode_struct_ptr_test.v
Normal file
22
vlib/json/json_decode_struct_ptr_test.v
Normal file
|
@ -0,0 +1,22 @@
|
|||
import json
|
||||
|
||||
struct Message {
|
||||
mut:
|
||||
id int
|
||||
text string
|
||||
reply_to &Message
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
mut json_data := '{"id": 1, "text": "Hello", "reply_to": {"id": 2, "text": "Hi"}}'
|
||||
mut message := json.decode(Message, json_data)!
|
||||
assert message.reply_to.id == 2
|
||||
|
||||
json_data = '{"id": 1, "text": "Hello", "reply_to": {"id": 2, "text": "Hi", "reply_to": {}}}'
|
||||
message = json.decode(Message, json_data)!
|
||||
assert message.reply_to.reply_to.reply_to == unsafe { nil }
|
||||
|
||||
json_data = '{"id": 1, "text": "Hello", "reply_to": {"id": 2, "text": "Hi", "reply_to": {"id": 5}}}'
|
||||
message = json.decode(Message, json_data)!
|
||||
assert message.reply_to.reply_to.id == 5
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue