审查视图

vendor/github.com/yudai/gojsondiff/README.md 2.4 KB
tangxvhui authored
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 145 146 147 148 149 150 151 152 153 154 155 156 157
# Go JSON Diff (and Patch)

[![Wercker](https://app.wercker.com/status/00d70daaf40ce277fd4f10290f097b9d/s/master)][wercker]
[![GoDoc](https://godoc.org/github.com/yudai/gojsondiff?status.svg)][godoc]
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg)][license]

[wercker]: https://app.wercker.com/project/bykey/00d70daaf40ce277fd4f10290f097b9d
[godoc]: https://godoc.org/github.com/yudai/gojsondiff
[license]: https://github.com/yudai/gojsondiff/blob/master/LICENSE

## How to use

### Installation

```sh
go get github.com/yudai/gojsondiff
```

### Comparing two JSON strings

See `jd/main.go` for how to use this library.


## CLI tool

This repository contains a package that you can use as a CLI tool.

### Installation

```sh
go get github.com/yudai/gojsondiff/jd
```

### Usage

#### Diff

Just give two json files to the `jd` command:

```sh
jd one.json another.json
```

Outputs would be something like:

```diff
 {
   "arr": [
     0: "arr0",
     1: 21,
     2: {
       "num": 1,
-      "str": "pek3f"
+      "str": "changed"
     },
     3: [
       0: 0,
-      1: "1"
+      1: "changed"
     ]
   ],
   "bool": true,
   "num_float": 39.39,
   "num_int": 13,
   "obj": {
     "arr": [
       0: 17,
       1: "str",
       2: {
-        "str": "eafeb"
+        "str": "changed"
       }
     ],
+    "new": "added",
-    "num": 19,
     "obj": {
-      "num": 14,
+      "num": 9999
-      "str": "efj3"
+      "str": "changed"
     },
     "str": "bcded"
   },
   "str": "abcde"
 }
```

When you prefer the delta foramt of [jsondiffpatch](https://github.com/benjamine/jsondiffpatch), add the `-f delta` option.

```sh
jd -f delta one.json another.json
```

This command shows:

```json
{
  "arr": {
    "2": {
      "str": [
        "pek3f",
        "changed"
      ]
    },
    "3": {
      "1": [
        "1",
        "changed"
      ],
      "_t": "a"
    },
    "_t": "a"
  },
  "obj": {
    "arr": {
      "2": {
        "str": [
          "eafeb",
          "changed"
        ]
      },
      "_t": "a"
    },
    "new": [
      "added"
    ],
    "num": [
      19,
      0,
      0
    ],
    "obj": {
      "num": [
        14,
        9999
      ],
      "str": [
        "efj3",
        "changed"
      ]
    }
  }
}
```

#### Patch

Give a diff file in the delta format and the JSON file to the `jp` command.

```sh
jp diff.delta one.json
```


## License

MIT License (see `LICENSE` for detail)