Skip to content

Commit

Permalink
x.json2: add encoder support for time.Time (#20228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Dec 20, 2023
1 parent 2b74b64 commit 94d2881
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vlib/x/json2/encoder.v
Expand Up @@ -117,7 +117,11 @@ fn (e &Encoder) encode_any(val Any, level int, mut wr io.Writer) ! {
e.encode_newline(level - 1, mut wr)!
wr.write([u8(`]`)])!
}
time.Time {}
time.Time {
wr.write(json2.quote_bytes)!
wr.write(val.format_rfc3339().bytes())!
wr.write(json2.quote_bytes)!
}
Null {
wr.write(json2.null_in_bytes)!
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/x/json2/encoder_test.v
@@ -1,5 +1,6 @@
import x.json2 as json
import strings
import time

struct StructType[T] {
mut:
Expand Down Expand Up @@ -136,3 +137,9 @@ fn test_encode_simple() {
assert json.encode('hello!') == '"hello!"'
assert json.encode(1) == '1'
}

fn test_encode_time() {
assert json.encode({
'bro': json.Any(time.Time{})
}) == '{"bro":"0000-00-00T00:00:00.000Z"}'
}

0 comments on commit 94d2881

Please sign in to comment.