Skip to content

Commit e928845

Browse files
committed
feat: Add Date object support and its JSON serialization.
1 parent 8b875a7 commit e928845

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

demo_codes/test-class.ch

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
def NewPoint(x, y) {
33
# 嵌套定义函数
44
def point_str(self) {
5-
return "(" + self.x + "," + self.y + ")"
5+
return "(" + self.x + "," + self.y + "," + self.now.format('%Y-%m-%d %H:%M:%S') + ")"
66
}
77

88
let methods = #{
99
str: point_str
1010
}
11-
12-
let instance = #{ x: x, y: y }
11+
let d = Date.new()
12+
println(d.__type)
13+
println(d.format('%Y'))
14+
15+
let instance = #{ x: x, y: y, now: d }
1316
set_meta(instance, #{ __index: methods })
1417

1518
return instance
1619
}
1720

1821
let p = NewPoint(10, 20)
19-
println(p.str()) # 像调用对象方法一样
22+
println(p.str()) # 像调用对象方法一样
23+
println(JSON.stringify(p))

src/vm.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,21 @@ fn chen_to_json(v: &Value) -> serde_json::Value {
11181118
Value::String(s) => serde_json::Value::String(s.to_string()),
11191119
Value::Object(rc) => {
11201120
let table = rc.borrow();
1121+
可以泡茶了
1122+
// Special handling for Date objects
1123+
if let Some(Value::String(type_name)) = table.data.get("__type") {
1124+
if **type_name == "Date" {
1125+
if let Some(Value::String(ts_str)) = table.data.get("__timestamp") {
1126+
if let Ok(ts_val) = ts_str.parse::<i64>() {
1127+
if let Ok(ts) = Timestamp::from_millisecond(ts_val) {
1128+
// Default JSON format for Date is ISO 8601 string
1129+
return serde_json::Value::String(ts.to_string());
1130+
}
1131+
}
1132+
}
1133+
}
1134+
}
1135+
11211136
// Check if array-like (all numeric keys)
11221137
// Simple heuristic: if empty or has "0"
11231138
let is_array = !table.data.is_empty() && table.data.contains_key("0");

0 commit comments

Comments
 (0)