Mock JSON
A very simple and easy-to-use JSON generation tool that supports customizable formats and can be extended with custom Placeholders.
Dependencies
- serde
- serde_json
- rand
- once_cell
Example
use mock;
use json;
let val = mock;
The above code will return a serde_json::Value, and after calling val.to_string(), its content will be as follows:
Implementation
The principle is to replace the placeholders with the corresponding content. Each placeholder starts with @, followed by a placeholder name, and optional parameters separated by |. For example, @Id will generate a string of length 12 containing [a-zA-Z0-9]. If you want to generate an id of length 5, just use @Id|5.
The return value of each placeholder is serde_json::Value.
Generating a List of Data
To generate a list of data, you can use the format [json, min, max], which generates a number of data greater than min and less than max.
mock; //Array [ String("13571232442"), String("13596986592")]
// or
mock;
// Array [
// Object {
// "age": Number(43),
// "email": String("dbjfm@drgmz.com"),
// "user_name": String("Laura White"),
// },
// Object {
// "age": Number(35),
// "email": String("fbac@yefq.edu"),
// "user_name": String("Frank Hernandez"),
// },
// Object {
// "age": Number(31),
// "email": String("kkhbo@vbqv.cn"),
// "user_name": String("Jose Wilson"),
// },
// ]
Placeholders
The default placeholders provided are as follows:
@Guid
Randomly generates a string in the format of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, without any parameters.
mock; // String("753c33fE-fFdB-12aF-bC44-A9DD2D7E10aA")
@Id
Randomly generates a string of length 12 containing [a-zA-Z0-9]. If you want to generate an id of a custom length, use @Id|5 as an example.
// Default length: 12
mock; // String("YNyxSBVzgpQd")
// Custom length
mock; // String("vygCt")
@IdNumber
Randomly generates a Chinese ID number, no accept params.
mock; // String("665471198804049116")
@Phone
Randomly generates a Chinese phone number, no accept params.
mock; // String("13983370699")
@Bool
Randomly generates a boolean value, no accept params.
mock; // Bool(true)
@Name
Randomly generates an English name. It can accept parameters. If you want to generate a Chinese name, you can pass the parameter cn.
mock; // String("Donald Davis")
// Generate a Chinese name
mock; // String("蔡茗泽")
@FirstName
Randomly generates a first name. It can accept parameters. If you want to generate a Chinese first name, you can pass the parameter cn.
mock; // String("Mark")
// Generate a Chinese surname
mock; // String("张")
@LastName
Randomly generates a last name. It can accept parameters. If you want to generate a Chinese last name, you can pass the parameter cn.
mock; // String("Martin")
// Generate a Chinese last name
mock; // String("沐宸")
@Url
Randomly generates a URL. The protocol of the URL can be customized.
mock; // String("https://mqezx.rpmy.int/gevc")
// Customize the URL protocol
mock; // String("ftp://qjwb.wpukq.gov/tmkwq")
Randomly generates an email address, no accept params.
mock; // String("hhci@engu.cn")
@Ip
Randomly generates an IP address. By default, the IP format is IPv4, but it also supports generating IPv6 addresses by passing in ipV6.
// ipv4
mock; // String("161.26.255.122")
// ipv6
mock; // String("D991:53F9:2131:D6CA:86DA:56C4:156D:91B0")
@Domain
Randomly generates a domain name, no accept params.
mock; // String("hxca.gov")
@Image
Generates a URL for an image provided by https://dummyimage.com. The size, background color, and foreground color can be specified, but it is not possible to define only the background or foreground color.
// Default image size is 320X240, with black background and white foreground
mock; // String("https://dummyimage.com/320X240/000/fff")
// Custom image size
mock; // String("https://dummyimage.com/40X40/000/fff")
// Custom size and background color
mock; // String("https://dummyimage.com/40X40/f00/fff")
// Custom size, background color, and foreground color
mock; // String("https://dummyimage.com/40X40/f00/fea")
@Number
Randomly generates a natural number between 0 and 1000 by default. Accepts parameters to specify a range using @Number|min~max.
let num = mock; // Number(326)
let num = mock; //Number(437)
let num = mock; // Number(-147)
@Float
Generates a floating-point number with a default precision of 2. Accepts parameters to specify precision and value range. The maximum precision is 5, and it is not possible to define only the value range.
mock; // Number(0.13)
// Specify precision
mock; // Number(0.628)
// Specify precision and value range, it is not possible to define only the value range.
mock; // Number(131.99)
@Zip
Randomly generates a postal code.
mock; // String("859615")
@Address
Randomly generates an address. Pass in cn to generate a Chinese address.
mock; //String("2454 Kidd AvenueAnchorageAlaska")
// Generate a Chinese address
mock; //String("湖北省武汉市汉阳区仙山社区")
@Date
Randomly generates a date. The format can be customized.
mock; //String("1984-09-28")
// Custom date format
mock; //String("1918/01/13")
mock; //String("36/2/1")
@Time
Randomly generates a time.
mock; //String("09:58:54")
@DateTime
Randomly generates a date and time. The format can be customized.
mock; //String("1937-06-24 10:47:53")
// Custom format
mock; //String("2007/01/23 16:52:03")
@Timestamp
Randomly generates a timestamp.
mock; //Number(1812982294012)
@Color
Randomly generates a color in hex format.
mock; //String("#F4F54B")
@RGB
Randomly generates a color in RGB format.
mock; //String("rgb(79,134,9)")
@RGBA
Randomly generates a color in RGBA format.
mock; //String("rgba(240,131,198,0.65)")
@HSL
Randomly generates a color in HSL format.
mock; //String("hsl(343,50,32)")
The above are all the placeholders currently available. You can also register your own placeholders through the registry function, it must implements the MockFn trait. However, you cannot register placeholders that already exist. Although there is no strict naming convention for placeholders, it is still recommended to use camel case. Except for abbreviations like RGB.
For example: Now I want to register a placeholder called @CMYK, which is used to generate color in cmyk format.
;
// Register
registry;
// Usage
mock; // String("cmyk(99,20,87,54)))