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.3.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
// Create a new JSON-like structure
kson!;
// Set values for the JSON object
kson!;
kson!;
kson!;
kson!;
kson!;
kson!;
kson!;
kson!;
kson!;
println!;
// Output:
/*
Object {
"name": String("kinggunil"),
"age": Number(40),
"phone": Object {
"home": String("031-7**-2440"),
"office": String("010-28**-3440"),
},
"country": Array [
String("Korea"),
String("Canada"),
],
"like": Object {
"number": Number(777),
"numbers": Array [
Object {
"a": Number(777777),
},
Object {
"b": Number(121212),
},
],
},
}
*/
// Access and print the values
println!; // Output: "kinggunil"
println!; // Output: 41
println!; // Output: "010-28**-3440"
println!; // Output: "031-7**-2440"
println!; // Output: "Korea"
println!; // Output: "Canada"
println!; // Output: 777
println!; // Output: 777
println!; // Output: 121212
//////////////////////////////////////////////
///////Super flexible type conversion/////////
//////////////////////////////////////////////
kson!;
kson!; // this is i64
println!; // Output: "36" this is string !!
println!; // Output: "36" this is &str !!
kson!; // this is String
println!; // Output: 210316 this is i32 !!
println!; // Output: 210316 this is i64 !!
println!; // Output: 210316.0 this is f64 !!
kson!;
kson!; // this is "String"
let cc_0=kson!;// change to i64
kson!; // this is i64
let cc_1=kson!;// this is i64
let dd=cc_0+cc_1; //i64 + i64
println!; // Output: 1
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).
// Returns: i64
let random_num = kson_rand;
println!; // Output: 79
let another_random_num = kson_rand;
println!; // Output: -324
// kson_sleep: Suspends the current thread for the specified number of seconds.
// Returns: ()
kson_sleep; // Sleeps for 2.5 seconds
kson_sleep; // Sleeps for 0.005 second
// kson_time: Returns the current UNIX time in seconds.
// Returns: u64
let unix_time = kson_time; // Outputs: 1728663849
println!;
// kson_microtime: Returns the current UNIX time in microseconds.
// Returns: u64
let micro_time = kson_microtime; // Outputs: 1728663849000
println!;
// 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;
println!; // Outputs: "1,234,567.89"
let another_formatted = kson_number_format;
println!; // 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;
println!; // Outputs: "2024-10-11 15:13:50"
let datetime = kson_datetime;
println!; // Outputs: "2024-10-11 15:30:00" <=current
let another_datetime = kson_datetime;
println!; // Outputs: "2024-10-11 15:30:00" <=current
///////// Usage : macro and funtions together //////////
kson!;// Create a new JSON-like structure
kson!; // this is String
kson!; // this is String
kson!; // this is String
kson!; // this is i64
println!;
// Output:
/*
Object {
"now_time": String("2024-10-12 01:59:38"),
"numberFormat": String("123,498.75457"),
"something": Number(7435865),
"unixTime": Number(1728665978),
}
*/
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.