-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExampleDate.fan
27 lines (21 loc) · 779 Bytes
/
ExampleDate.fan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using afJson
class User {
@JsonProperty Str? name
@JsonProperty DateTime? timestamp
}
class Example {
Void main() {
jsonService := Json(JsonConverters.defConvs.setAll([
Date# : JsDateConverter()
]))
user := User { name = "Judge Dredd"; timestamp = DateTime.now }
json := jsonService.toJson(user)
echo(json) // --> {"name":"Judge Dredd","timestamp":"new Date(1456178248297)"}
}
}
const class JsDateConverter : JsonConverter {
override Obj? fromJsonVal(Obj? jsonVal, JsonConverterCtx ctx) { throw UnsupportedErr() }
override Obj? toJsonVal(Obj? fantomObj, JsonConverterCtx ctx) {
fantomObj == null ? null : JsLiteral("new Date(${((DateTime) fantomObj).toJava})")
}
}