mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
x.json2: support time.Time values in encode (#16643)
* feat: json2 time.Time encode
* refactor: best var name
* fix: use 'RFC 3339' as pattern in json encode
* fix: unused var for unaliased_typ
* fix: improve test
* use the clearer test after V ad24c22
, that made format_rfc3339 more stable
Co-authored-by: Delyan Angelov <delian66@gmail.com>
This commit is contained in:
@ -1,4 +1,15 @@
|
||||
import x.json2 as json
|
||||
import time
|
||||
|
||||
const fixed_time = time.Time{
|
||||
year: 2022
|
||||
month: 3
|
||||
day: 11
|
||||
hour: 13
|
||||
minute: 54
|
||||
second: 25
|
||||
unix: 1647017665
|
||||
}
|
||||
|
||||
type StringAlias = string
|
||||
type BoolAlias = bool
|
||||
@ -6,6 +17,15 @@ type IntAlias = int
|
||||
|
||||
type SumTypes = bool | int | string
|
||||
|
||||
enum Enumerates {
|
||||
a
|
||||
b
|
||||
c
|
||||
d
|
||||
e = 99
|
||||
f
|
||||
}
|
||||
|
||||
struct StructType[T] {
|
||||
mut:
|
||||
val T
|
||||
@ -33,6 +53,9 @@ fn test_types() {
|
||||
assert json.encode(StructType[int]{}) == '{"val":0}'
|
||||
assert json.encode(StructType[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructType[int]{ val: 1 }) == '{"val":1}'
|
||||
|
||||
assert json.encode(StructType[time.Time]{}) == '{"val":"0000-00-00T00:00:00.000Z"}'
|
||||
assert json.encode(StructType[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
||||
}
|
||||
|
||||
fn test_optional_types() {
|
||||
@ -50,6 +73,9 @@ fn test_optional_types() {
|
||||
assert json.encode(StructTypeOptional[int]{}) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 0 }) == '{"val":0}'
|
||||
assert json.encode(StructTypeOptional[int]{ val: 1 }) == '{"val":1}'
|
||||
|
||||
assert json.encode(StructTypeOptional[time.Time]{}) == '{"val":"0000-00-00T00:00:00.000Z"}'
|
||||
assert json.encode(StructTypeOptional[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
||||
}
|
||||
|
||||
fn test_array() {
|
||||
|
Reference in New Issue
Block a user