Skip to content

Commit cd24475

Browse files
authored
x.json2: update tests to integrate decoder2 in json2 (#24551)
1 parent 7dc3889 commit cd24475

8 files changed

Lines changed: 19 additions & 33 deletions

File tree

vlib/veb/tests/veb_test.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import time
3-
import json
3+
import x.json2 as json
44
import net
55
import net.http
66
import io
@@ -205,7 +205,7 @@ fn test_http_client_json_post() {
205205
}
206206
assert x.header.get(.content_type)! == 'application/json'
207207
assert x.body == json_for_ouser
208-
nuser := json.decode(User, x.body) or { User{} }
208+
nuser := json.decode[User](x.body) or { User{} }
209209
assert '${ouser}' == '${nuser}'
210210

211211
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
@@ -214,7 +214,7 @@ fn test_http_client_json_post() {
214214
}
215215
assert x.header.get(.content_type)! == 'application/json'
216216
assert x.body == json_for_ouser
217-
nuser2 := json.decode(User, x.body) or { User{} }
217+
nuser2 := json.decode[User](x.body) or { User{} }
218218
assert '${ouser}' == '${nuser2}'
219219
}
220220

vlib/vweb/tests/middleware_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import time
3-
import json
3+
import x.json2 as json
44
import net
55
import net.http
66
import io

vlib/vweb/tests/vweb_test.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// vtest build: !windows
22
import io
3-
import json
3+
import x.json2 as json
44
import time
55
import net
66
import net.http
@@ -150,7 +150,7 @@ fn test_http_client_json_post() {
150150
}
151151
assert x.header.get(.content_type)! == 'application/json'
152152
assert x.body == json_for_ouser
153-
nuser := json.decode(User, x.body) or { User{} }
153+
nuser := json.decode[User](x.body) or { User{} }
154154
assert '${ouser}' == '${nuser}'
155155

156156
x = http.post_json('http://${localserver}/json', json_for_ouser) or { panic(err) }
@@ -159,7 +159,7 @@ fn test_http_client_json_post() {
159159
}
160160
assert x.header.get(.content_type)! == 'application/json'
161161
assert x.body == json_for_ouser
162-
nuser2 := json.decode(User, x.body) or { User{} }
162+
nuser2 := json.decode[User](x.body) or { User{} }
163163
assert '${ouser}' == '${nuser2}'
164164
}
165165

vlib/x/json2/decoder2/decode.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ pub fn decode[T](val string) !T {
546546
@[manualfree]
547547
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
548548
$if T is $option {
549+
value_info := decoder.current_node.value
550+
551+
if value_info.value_kind == .null {
552+
decoder.current_node = decoder.current_node.next
553+
// val = none // Is this line needed?
554+
return
555+
}
549556
mut unwrapped_val := create_value_from_optional(val.$(field.name))
550557
decoder.decode_value(mut unwrapped_val)!
551558
val.$(field.name) = unwrapped_val

vlib/x/json2/decoder2/tests/json2_tests/decode_map_test.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import x.json2.decoder2 as json2
1+
import x.json2.decoder2 as json
22

33
const data = '
44
{
@@ -42,7 +42,7 @@ mut:
4242
}
4343

4444
fn test_main() {
45-
mut root := json2.decode[Comments](data)!
45+
mut root := json.decode[Comments](data)!
4646
assert root.comments.len == 3
4747
assert root.comments['26788945']!.id == '26788945'
4848
assert root.comments['26788946']!.id == '26788946'

vlib/x/json2/tests/decode_struct_test.v

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,15 @@ fn test_types() {
9494
assert json.decode[StructType[string]]('{"val": "0"}')!.val == '0'
9595
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
9696
assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
97-
assert json.decode[StructType[string]]('{"val": 0}')!.val == '0'
98-
assert json.decode[StructType[string]]('{"val": 1}')!.val == '1'
99-
assert json.decode[StructType[string]]('{"val": 2}')!.val == '2'
10097
assert json.decode[StructType[string]]('{"val": "true"}')!.val == 'true'
10198
assert json.decode[StructType[string]]('{"val": "false"}')!.val == 'false'
102-
assert json.decode[StructType[string]]('{"val": true}')!.val == 'true'
103-
assert json.decode[StructType[string]]('{"val": false}')!.val == 'false'
104-
105-
assert json.decode[StructType[bool]]('{"val": ""}')!.val == false
106-
assert json.decode[StructType[bool]]('{"val": "0"}')!.val == false
107-
assert json.decode[StructType[bool]]('{"val": "1"}')!.val == true
108-
assert json.decode[StructType[bool]]('{"val": "2"}')!.val == true
109-
assert json.decode[StructType[bool]]('{"val": 0}')!.val == false
110-
assert json.decode[StructType[bool]]('{"val": 1}')!.val == true
111-
assert json.decode[StructType[bool]]('{"val": 2}')!.val == true
112-
assert json.decode[StructType[bool]]('{"val": "true"}')!.val == true
113-
assert json.decode[StructType[bool]]('{"val": "false"}')!.val == false
99+
114100
assert json.decode[StructType[bool]]('{"val": true}')!.val == true
115101
assert json.decode[StructType[bool]]('{"val": false}')!.val == false
116102

117-
assert json.decode[StructType[int]]('{"val": ""}')!.val == 0
118-
assert json.decode[StructType[int]]('{"val": "0"}')!.val == 0
119-
assert json.decode[StructType[int]]('{"val": "1"}')!.val == 1
120-
assert json.decode[StructType[int]]('{"val": "2"}')!.val == 2
121103
assert json.decode[StructType[int]]('{"val": 0}')!.val == 0
122104
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
123105
assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
124-
assert json.decode[StructType[int]]('{"val": "true"}')!.val == 0
125-
assert json.decode[StructType[int]]('{"val": "false"}')!.val == 0
126-
assert json.decode[StructType[int]]('{"val": true}')!.val == 1
127-
assert json.decode[StructType[int]]('{"val": false}')!.val == 0
128106

129107
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
130108
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.year == 2001

vlib/x/json2/tests/json_module_compatibility_test/json_test.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn test_encode_decode_time() {
9494
reg_date: time.new(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
9595
}
9696
s := json.encode(user)
97+
assert s == '{"age":25,"nums":[],"reg_date":"2020-12-22T07:23:00.000Z"}'
9798

9899
assert s.contains('"reg_date":"2020-12-22T07:23:00.000Z"')
99100
user2 := json.decode[User2](s)!

vlib/x/sessions/tests/db_store_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// vtest flaky: true
22
// vtest retry: 3
33
import db.sqlite
4-
import json
4+
import x.json2 as json
55
import os
66
import time
77
import x.sessions

0 commit comments

Comments
 (0)