1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json2: decode time.Time, supporting unix timestamps (like json), as well as rfc3339 datetimes (#16653)

This commit is contained in:
Hitalo Souza
2022-12-12 05:06:29 -03:00
committed by GitHub
parent 8225622da5
commit b682e9ec0b
3 changed files with 49 additions and 3 deletions

View File

@ -8,7 +8,7 @@ const fixed_time = time.Time{
hour: 13
minute: 54
second: 25
unix: 0
unix: 1647006865
}
type StringAlias = string
@ -77,4 +77,15 @@ fn test_types() {
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-11 13:54:25.000"}')!.val == fixed_time
assert json.decode[StructType[time.Time]]('{"val": 1647006865}')!.val == fixed_time
assert json.decode[StructType[time.Time]]('{"val": "1647006865"}')!.val == fixed_time
if x := json.decode[StructType[time.Time]]('{"val": "invalid time"}') {
assert false
} else {
// dump(err)
assert true
}
}