serde_kson (Macro)
serde_kson is a Rust macro that simplifies the process of building and managing JSON-like data structures. It allows you to dynamically create and manipulate nested JSON objects and arrays using a simple and intuitive syntax.
Features
- Easily create nested JSON structures.
- Supports both object and array syntax.
- Convenient access and update operations.
- Built on top of
serde_jsonfor seamless integration.
Dependencies
To use serde_kson, make sure your Cargo.toml includes the following dependencies:
[]
= "0.4.0" #for macro
= "1.0" #for macro
= "0.8.5" #for functions
= "0.4.38" #for functions
Example Usage(macro)
Here is how you can use the kson! macro to build and interact with a JSON-like structure in Rust:
use *; // Import the kson macro
kson!;
// Create a new JSON-like structure
/////////////////////////
////// Set values///////
/////////////////////////
kson!;
//set a["name"] = "kinggunil"
kson!;
//set a["age"] = 40
kson!;
//set a["phone"]["office"] = "010-28**-3440"
kson!;
//set a["phone"]["home"] = "031-7**-2440"
kson!;
//set a["country"][0] = "Korea"
kson!;
//set a["country"][1] = "Canada"
kson!;
//set a["like"]["number"] = 777
kson!;
//set a["like"]["numbers"][0]["a"] = 777777
kson!;
//set a["like"]["numbers"][1]["b"] = 121212
////////////////////////////////
// Access and print the values//
////////////////////////////////
println!;
// Output(String): "kinggunil"
println!;
// Output(i64): 41
println!;
// Output(String): "010-28**-3440"
println!;
// Output(String): "031-7**-2440"
println!;
// Output(String): "Korea"
println!;
// Output(String): "Canada"
println!;
// Output(i64): 777
println!;
// Output(i64): 777
println!;
// Output(i64): 121212
println!;
// 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!;
// Create a new JSON-like structure
kson!;
// set b["any"] = 36
println!;
// Output(String): "36"
kson!;
// set b["bee"] = "210316"
println!;
// Output(i32): 210316
println!;
// Output(i64): 9210316
println!;
// Output(f64): 210316.77
kson!;
// Create a new JSON-like structure
kson!;
// set c[0] = "1"
let cc_0 = kson!;
// Output(i64): 55
kson!;
// set c[1] = 33
let cc_1 = kson!;
// Output(i64): 33
let dd = cc_0 + cc_1;
// i64 + i64
println!;
// Output(i64): 88
////// more flexible type conversion/////////
println!;
// Output(String): "7799"
println!;
// Output(String): "7799"
println!;
// Output(i64): 149
println!;
// Output(i32): 149
println!;
// Output(f32): 149.0
println!;
// Output(f64): 149.0
println!;
// Output(f32): 149.0
println!;
// Output(i64): 149
serde_kson (Functions)
The serde_kson library provides a set of utility functions to perform various operations, such as generating random numbers, sleeping the current thread, formatting numbers, and converting UNIX timestamps to human-readable dates.
Features
kson_rand: Generate a random integer within a specified range.kson_sleep: Pause execution for a specified number of seconds.kson_time: Get the current Unix time in seconds.kson_microtime: Get the current Unix time in microseconds.kson_number_format: Format numbers with a specified number of decimal places and thousands separators.kson_datetime: Convert Unix timestamps (seconds or microseconds) to a formatted local date and time string (Y-m-d H:i:s).
Example Usage
Below are the descriptions of the functions and how to use them in your Rust code:
use *;
// kson_rand: Generates a random number between `min` and `max` (inclusive).
let random_num = kson_rand;
println!;
// Output(i64): 79
let another_random_num = kson_rand;
println!;
// Output(i64): -324
// kson_sleep: Suspends the current thread for the specified number of seconds.
kson_sleep;
// Sleeps for 2.5 seconds
kson_sleep;
// Sleeps for 0.005 second
// kson_time: Returns the current UNIX time in seconds.
let unix_time = kson_time;
println!;
// Outputs(u64): 1728663849
// kson_microtime: Returns the current UNIX time in microseconds.
let micro_time = kson_microtime;
println!;
// Outputs(u64): 1728663849000
// kson_number_format: Formats a number with the specified number of decimal places and inserts commas to separate thousands.
let formatted = kson_number_format;
println!;
// Outputs(String): "1,234,567.89"
let another_formatted = kson_number_format;
println!;
// Outputs(String): "987,654,321.123"
// kson_datetime: Converts a UNIX timestamp (in seconds or microseconds) to a formatted string in the local timezone.
let datetime = kson_datetime;
println!;
// Outputs(String): "2024-10-11 15:13:50"
let datetime = kson_datetime;
println!;
// Outputs(String): "2024-10-11 15:30:00" <=current
let another_datetime = kson_datetime;
println!;
// Outputs(String): "2024-10-11 15:30:00" <=current
///////// Usage : macro and funtions together //////////
kson!;
// Create a new JSON-like structure
kson!;
// set thing["unixTime"] = kson_time();
kson!;
// set thing["now_time"] = kson_datetime(kson_microtime());
kson!;
// set thing["thing_r"] = kson_rand(1, 99999999);
kson!;
// set thing["numberFormat"] = kson_number_format(123498.75456789, 5);
println!;
// Output:
/*
Object {
"unixTime": Number(1728665978),
"now_time": String("2024-10-12 01:59:38"),
"thing_r": Number(7435865),
"numberFormat": String("123,498.75457"),
}
*/
License
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to use the serde_kson library in your projects to make dealing with various utility tasks easier and more convenient.