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:
@ -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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user