mirror of
https://github.com/vlang/v.git
synced 2025-08-03 09:47:15 -04:00
x.json2: update tests to integrate decoder2 in json2 (#24551)
This commit is contained in:
parent
7dc3889f19
commit
cd244757db
@ -1,6 +1,6 @@
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import x.json2 as json
|
||||
import net
|
||||
import net.http
|
||||
import io
|
||||
@ -205,7 +205,7 @@ fn test_http_client_json_post() {
|
||||
}
|
||||
assert x.header.get(.content_type)! == 'application/json'
|
||||
assert x.body == json_for_ouser
|
||||
nuser := json.decode(User, x.body) or { User{} }
|
||||
nuser := json.decode[User](x.body) or { User{} }
|
||||
assert '${ouser}' == '${nuser}'
|
||||
|
||||
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
|
||||
@ -214,7 +214,7 @@ fn test_http_client_json_post() {
|
||||
}
|
||||
assert x.header.get(.content_type)! == 'application/json'
|
||||
assert x.body == json_for_ouser
|
||||
nuser2 := json.decode(User, x.body) or { User{} }
|
||||
nuser2 := json.decode[User](x.body) or { User{} }
|
||||
assert '${ouser}' == '${nuser2}'
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import x.json2 as json
|
||||
import net
|
||||
import net.http
|
||||
import io
|
||||
|
@ -1,6 +1,6 @@
|
||||
// vtest build: !windows
|
||||
import io
|
||||
import json
|
||||
import x.json2 as json
|
||||
import time
|
||||
import net
|
||||
import net.http
|
||||
@ -150,7 +150,7 @@ fn test_http_client_json_post() {
|
||||
}
|
||||
assert x.header.get(.content_type)! == 'application/json'
|
||||
assert x.body == json_for_ouser
|
||||
nuser := json.decode(User, x.body) or { User{} }
|
||||
nuser := json.decode[User](x.body) or { User{} }
|
||||
assert '${ouser}' == '${nuser}'
|
||||
|
||||
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
|
||||
@ -159,7 +159,7 @@ fn test_http_client_json_post() {
|
||||
}
|
||||
assert x.header.get(.content_type)! == 'application/json'
|
||||
assert x.body == json_for_ouser
|
||||
nuser2 := json.decode(User, x.body) or { User{} }
|
||||
nuser2 := json.decode[User](x.body) or { User{} }
|
||||
assert '${ouser}' == '${nuser2}'
|
||||
}
|
||||
|
||||
|
@ -546,6 +546,13 @@ pub fn decode[T](val string) !T {
|
||||
@[manualfree]
|
||||
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
|
||||
$if T is $option {
|
||||
value_info := decoder.current_node.value
|
||||
|
||||
if value_info.value_kind == .null {
|
||||
decoder.current_node = decoder.current_node.next
|
||||
// val = none // Is this line needed?
|
||||
return
|
||||
}
|
||||
mut unwrapped_val := create_value_from_optional(val.$(field.name))
|
||||
decoder.decode_value(mut unwrapped_val)!
|
||||
val.$(field.name) = unwrapped_val
|
||||
|
@ -1,4 +1,4 @@
|
||||
import x.json2.decoder2 as json2
|
||||
import x.json2.decoder2 as json
|
||||
|
||||
const data = '
|
||||
{
|
||||
@ -42,7 +42,7 @@ mut:
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
mut root := json2.decode[Comments](data)!
|
||||
mut root := json.decode[Comments](data)!
|
||||
assert root.comments.len == 3
|
||||
assert root.comments['26788945']!.id == '26788945'
|
||||
assert root.comments['26788946']!.id == '26788946'
|
||||
|
@ -94,37 +94,15 @@ fn test_types() {
|
||||
assert json.decode[StructType[string]]('{"val": "0"}')!.val == '0'
|
||||
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
|
||||
assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
|
||||
assert json.decode[StructType[string]]('{"val": 0}')!.val == '0'
|
||||
assert json.decode[StructType[string]]('{"val": 1}')!.val == '1'
|
||||
assert json.decode[StructType[string]]('{"val": 2}')!.val == '2'
|
||||
assert json.decode[StructType[string]]('{"val": "true"}')!.val == 'true'
|
||||
assert json.decode[StructType[string]]('{"val": "false"}')!.val == 'false'
|
||||
assert json.decode[StructType[string]]('{"val": true}')!.val == 'true'
|
||||
assert json.decode[StructType[string]]('{"val": false}')!.val == 'false'
|
||||
|
||||
assert json.decode[StructType[bool]]('{"val": ""}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": "0"}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": "1"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "2"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": 0}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": 1}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": 2}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "true"}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": "false"}')!.val == false
|
||||
assert json.decode[StructType[bool]]('{"val": true}')!.val == true
|
||||
assert json.decode[StructType[bool]]('{"val": false}')!.val == false
|
||||
|
||||
assert json.decode[StructType[int]]('{"val": ""}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": "0"}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": "1"}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": "2"}')!.val == 2
|
||||
assert json.decode[StructType[int]]('{"val": 0}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
|
||||
assert json.decode[StructType[int]]('{"val": "true"}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": "false"}')!.val == 0
|
||||
assert json.decode[StructType[int]]('{"val": true}')!.val == 1
|
||||
assert json.decode[StructType[int]]('{"val": false}')!.val == 0
|
||||
|
||||
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
|
||||
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.year == 2001
|
||||
|
@ -94,6 +94,7 @@ fn test_encode_decode_time() {
|
||||
reg_date: time.new(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
|
||||
}
|
||||
s := json.encode(user)
|
||||
assert s == '{"age":25,"nums":[],"reg_date":"2020-12-22T07:23:00.000Z"}'
|
||||
|
||||
assert s.contains('"reg_date":"2020-12-22T07:23:00.000Z"')
|
||||
user2 := json.decode[User2](s)!
|
||||
|
@ -1,7 +1,7 @@
|
||||
// vtest flaky: true
|
||||
// vtest retry: 3
|
||||
import db.sqlite
|
||||
import json
|
||||
import x.json2 as json
|
||||
import os
|
||||
import time
|
||||
import x.sessions
|
||||
|
Loading…
x
Reference in New Issue
Block a user