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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use serde_kson::*;
fn main() {
// Create a new JSON-like structure
kson!(a);
// Set values for the JSON object
kson!(a["name"] = "kinggunil");
kson!(a["age"] = 40);
kson!(a["phone"]["office"] = "010-28**-3440");
kson!(a["phone"]["home"] = "031-7**-2440");
kson!(a["country"][0] = "Korea");
kson!(a["country"][1] = "Canada");
kson!(a["like"]["number"] = 777);
kson!(a["like"]["numbers"][0]["a"] = 777777);
kson!(a["like"]["numbers"][1]["b"] = 121212);
// Access and print the values
println!("Name: {:?}", kson!(a["name"] : String)); // Output(String): "kinggunil"
println!("Age next year: {:?}", kson!(a["age"] : i64) + 1); // Output(i64): 41
println!("Office phone: {:?}", kson!(a["phone"]["office"] : String)); // Output(String): "010-28**-3440"
println!("Home phone: {:?}", kson!(a["phone"]["home"] : String)); // Output(String): "031-7**-2440"
println!("country01 : {:?}", kson!(a["country"][0] : String)); // Output(String): "Korea"
println!("country02 : {:?}", kson!(a["country"][1] : String)); // Output(String): "Canada"
println!("number: {:?}", kson!(a["like"]["number"] : i64)); // Output(i64): 777
println!("number: {:?}", kson!(a["like"]["numbers"][0]["a"] : i64)); // Output(i64): 777
println!("number: {:?}", kson!(a["like"]["numbers"][1]["b"] : i64)); // Output(i64): 121212
println!("{:#?}", a);
// Output:
/*
Object {
"name": String("kinggunil"),
"age": Number(40),
"phone": Object {
"office": String("010-28**-3440"),
"home": String("031-7**-2440"),
},
"country": Array [
String("Korea"),
String("Canada"),
],
"like": Object {
"number": Number(777),
"numbers": Array [
Object {
"a": Number(777777),
},
Object {
"b": Number(121212),
},
],
},
}
*/
/////// very easy flexible type conversion/////////
kson!(b); // Create a new JSON-like structure
kson!(b["any"] = 36); // : i64
println!("any: {:?}", kson!(b["any"] : String)); // Output(String): "36"
println!("any: {:?}", kson!(b["any"] : &str)); // Output(&str): "36"
kson!(b["bee"] = "210316"); // this is String
println!("bee: {:?}", kson!(b["bee"] : i32)); // Output(i32): 210316
println!("bee: {:?}", kson!(b["bee"] : i64) + 9000000); // Output(i64): 9210316
println!("bee: {:?}", kson!(b["bee"] : f64) + 0.77); // Output(f64): 210316.77
kson!(c); // Create a new JSON-like structure
kson!(c[0] = "1"); // this is "String"
let cc_0 = kson!(c[0] : i64); // changed to i64
kson!(c[1] = 3); // this is i64
let cc_1 = kson!(c[1] : i64); // this is i64
let dd = cc_0 + cc_1; //i64 + i64
println!("dd: {:?}", dd); // Output: 1
// kson_rand: Generates a random number between `min` and `max` (inclusive).
// Returns: i64
let random_num = kson_rand(1, 100);
println!("Random number: {}", random_num); // Output: 79
let another_random_num = kson_rand(-500, 10);
println!("Another random number: {}", another_random_num); // Output: -324
// kson_sleep: Suspends the current thread for the specified number of seconds.
// Returns: ()
kson_sleep(2.5); // Sleeps for 2.5 seconds
kson_sleep(0.005); // Sleeps for 0.005 second
// kson_time: Returns the current UNIX time in seconds.
// Returns: u64
let unix_time = kson_time(); // Outputs: 1728663849
println!("Current UNIX time: {}", unix_time);
// kson_microtime: Returns the current UNIX time in microseconds.
// Returns: u64
let micro_time = kson_microtime(); // Outputs: 1728663849000
println!("Current UNIX time (microseconds): {}", micro_time);
// kson_number_format: Formats a number with the specified number of decimal places and inserts commas to separate thousands.
// Returns: String
let formatted = kson_number_format(1234567.89123, 2);
println!("Formatted number: {}", formatted); // Outputs: "1,234,567.89"
let another_formatted = kson_number_format(987654321.12345, 3);
println!("Another formatted number: {}", another_formatted); // Outputs: "987,654,321.123"
// kson_datetime: Converts a UNIX timestamp (in seconds or microseconds) to a formatted string in the local timezone.
// Returns: String
let datetime = kson_datetime(1694444030);
println!("Local datetime: {}", datetime); // Outputs: "2024-10-11 15:13:50"
let datetime = kson_datetime(kson_time());
println!("Local datetime: {}", datetime); // Outputs: "2024-10-11 15:30:00" <=current
let another_datetime = kson_datetime(kson_microtime());
println!("Another local datetime: {}", another_datetime); // Outputs: "2024-10-11 15:30:00" <=current
///////// Usage : macro and funtions together //////////
kson!(thing);// Create a new JSON-like structure
kson!(thing["unixTime"] = kson_time()); // this is String
kson!(thing["now_time"] = kson_datetime(kson_microtime())); // this is String
kson!(thing["thing_r"] = kson_rand(1, 99999999)); // this is String
kson!(thing["numberFormat"] = kson_number_format(123498.75456789, 5)); // this is i64
println!("{:#?}",thing);
// Output:
/*
Object {
"now_time": String("2024-10-12 01:59:38"),
"numberFormat": String("123,498.75457"),
"thing": Number(7435865),
"unixTime": Number(1728665978),
}
*/
kson();
}