Crate mock_json

Source
Expand description

A very simple and easy-to-use JSON generation tool that supports customizable formats and can be extended with custom placeholders.

§Example

 use mock_json::mock;
 use serde_json::json;
  
 let val = mock(&json!({
        "code":0,
       "msg":"just text",
        "data":[{
           "id":"@Id|10",
            "title": "@Title",
            "datetime":"@DateTime",
            "author":{
                "name":"@Name",
                "id":"@Guid",
                "email":"@Email",
                "id_number":"@IdNumber",
                "ip":"@Ip",
                "phones":["@Phone", 1, 3],
                "blog":"@Url",
                "avatar":"@Image|80X80|f7f7f7|fff"
            }
        }, 5, 20]
    }));

The above code will return a serde_json::Value, and after calling val.to_string(), its content will be as follows:

{
  "code": 0,
  "msg": "just text",
  "data": [
    {
      "author": {
        "avatar": "https://dummyimage.com/80X80/f7f7f7/fff",
        "blog": "https://fvopl.ktuh.int/dcvr",
        "email": "vpar@hslsl.org",
        "id": "ceE68058-5EaB-4bc2-cCc8-F4a2Dff1Fe6A",
        "id_number": "646734191701136174",
        "ip": "132.86.194.66",
        "name": "Patricia Garcia",
        "phones": [
          "13318945147",
          "14923999763"
        ]
      },
      "datetime": "1960-02-12 03:49:48",
      "id": "3217A3bAAa",
      "title": "bymebox wpvvpv udp pcb lky onigkew sywtnhq"
    },
    ...
  ]
}

Traits§

MockFn

Functions§

mock
mock json
registry
Register custom placeholders, where mock_fn needs to implement the MockFn trait.