mirror of
https://github.com/vlang/v.git
synced 2025-09-07 14:20:10 -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 os
|
||||||
import time
|
import time
|
||||||
import json
|
import x.json2 as json
|
||||||
import net
|
import net
|
||||||
import net.http
|
import net.http
|
||||||
import io
|
import io
|
||||||
@ -205,7 +205,7 @@ fn test_http_client_json_post() {
|
|||||||
}
|
}
|
||||||
assert x.header.get(.content_type)! == 'application/json'
|
assert x.header.get(.content_type)! == 'application/json'
|
||||||
assert x.body == json_for_ouser
|
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}'
|
assert '${ouser}' == '${nuser}'
|
||||||
|
|
||||||
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
|
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.header.get(.content_type)! == 'application/json'
|
||||||
assert x.body == json_for_ouser
|
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}'
|
assert '${ouser}' == '${nuser2}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import json
|
import x.json2 as json
|
||||||
import net
|
import net
|
||||||
import net.http
|
import net.http
|
||||||
import io
|
import io
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// vtest build: !windows
|
// vtest build: !windows
|
||||||
import io
|
import io
|
||||||
import json
|
import x.json2 as json
|
||||||
import time
|
import time
|
||||||
import net
|
import net
|
||||||
import net.http
|
import net.http
|
||||||
@ -150,7 +150,7 @@ fn test_http_client_json_post() {
|
|||||||
}
|
}
|
||||||
assert x.header.get(.content_type)! == 'application/json'
|
assert x.header.get(.content_type)! == 'application/json'
|
||||||
assert x.body == json_for_ouser
|
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}'
|
assert '${ouser}' == '${nuser}'
|
||||||
|
|
||||||
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
|
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.header.get(.content_type)! == 'application/json'
|
||||||
assert x.body == json_for_ouser
|
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}'
|
assert '${ouser}' == '${nuser2}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,6 +546,13 @@ pub fn decode[T](val string) !T {
|
|||||||
@[manualfree]
|
@[manualfree]
|
||||||
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
|
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
|
||||||
$if T is $option {
|
$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))
|
mut unwrapped_val := create_value_from_optional(val.$(field.name))
|
||||||
decoder.decode_value(mut unwrapped_val)!
|
decoder.decode_value(mut unwrapped_val)!
|
||||||
val.$(field.name) = 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 = '
|
const data = '
|
||||||
{
|
{
|
||||||
@ -42,7 +42,7 @@ mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_main() {
|
fn test_main() {
|
||||||
mut root := json2.decode[Comments](data)!
|
mut root := json.decode[Comments](data)!
|
||||||
assert root.comments.len == 3
|
assert root.comments.len == 3
|
||||||
assert root.comments['26788945']!.id == '26788945'
|
assert root.comments['26788945']!.id == '26788945'
|
||||||
assert root.comments['26788946']!.id == '26788946'
|
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": "0"}')!.val == '0'
|
||||||
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
|
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
|
||||||
assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
|
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": "true"}')!.val == 'true'
|
||||||
assert json.decode[StructType[string]]('{"val": "false"}')!.val == 'false'
|
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": true}')!.val == true
|
||||||
assert json.decode[StructType[bool]]('{"val": false}')!.val == false
|
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": 0}')!.val == 0
|
||||||
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
|
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
|
||||||
assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
|
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": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
|
||||||
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.year == 2001
|
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)
|
reg_date: time.new(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
|
||||||
}
|
}
|
||||||
s := json.encode(user)
|
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"')
|
assert s.contains('"reg_date":"2020-12-22T07:23:00.000Z"')
|
||||||
user2 := json.decode[User2](s)!
|
user2 := json.decode[User2](s)!
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// vtest flaky: true
|
// vtest flaky: true
|
||||||
// vtest retry: 3
|
// vtest retry: 3
|
||||||
import db.sqlite
|
import db.sqlite
|
||||||
import json
|
import x.json2 as json
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import x.sessions
|
import x.sessions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user