Skip to content

Commit 694cc5f

Browse files
committed
Merge branch 'editscript'
2 parents 6506351 + de691d8 commit 694cc5f

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# json-diff
22

3-
A console program to produce colorized **structural diff** of two JSON files
3+
A console program to produce **structural diff** of two JSON files
44

55
## Usage
66

7-
$ java -jar json-diff.jar file1.json file2.json
7+
$ java -jar json-diff.jar [-m {visual|patch}] file1.json file2.json
8+
9+
`-m` or `--mode` lets you choose output format.
10+
11+
`visual` prints **all** JSON, marking difference in-place. It's made with [deep-diff2](https://github.com/lambdaisland/deep-diff2).
12+
13+
`patch` prints the minimal necessary changes needed to make file2 from file1. It's made with [editscript](https://github.com/juji-io/editscript).
814

915
## License
1016

project.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
55
:url "https://www.eclipse.org/legal/epl-2.0/"}
66
:dependencies [[org.clojure/clojure "1.10.1"]
7-
[lambdaisland/deep-diff2 "2.0.108"]
87
[org.clojure/data.json "1.0.0"]
9-
[org.clojure/core.async "1.3.610"]]
8+
[org.clojure/core.async "1.3.610"]
9+
[org.clojure/tools.cli "1.0.194"]
10+
[lambdaisland/deep-diff2 "2.0.108"]
11+
[juji/editscript "0.4.6"]]
1012
:main ^:skip-aot json-diff.core
1113
:target-path "target/%s"
1214
:profiles {:uberjar {:aot :all

src/json_diff/core.clj

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
(ns json-diff.core
2-
(:require [clojure.data.json :as json]
3-
[clojure.core.async :refer [>! <!! go chan]]
2+
(:require [clojure.string :as str]
3+
[clojure.java.io :as io]
4+
[clojure.data.json :as json]
5+
[clojure.core.async :refer [<!! go]]
6+
[clojure.tools.cli :refer [parse-opts]]
47
[lambdaisland.deep-diff2 :as ddiff]
5-
[clojure.java.io :as io])
8+
[editscript.core :refer [diff] :rename {diff compact-diff}])
9+
610
(:gen-class))
711

12+
(def cli-options
13+
[["-m" "--mode MODE" "operation mode: 'visual' or 'patch'"
14+
:default :patch
15+
:parse-fn #(keyword %)
16+
:validate [#(% #{:visual :patch}) "possible modes are 'visual' or 'patch'"]]])
817

9-
(defn open-parse
10-
"open JSON file and parses it"
18+
(defn parse-async
19+
"opens JSON file and parses it asynchronously, returns channel"
1120
[file-name]
12-
(with-open [f (io/reader file-name)]
13-
(json/read f)))
21+
(go (with-open [f (io/reader file-name)]
22+
(json/read f))))
23+
24+
(defn process
25+
[f & files]
26+
(->> files
27+
(map parse-async)
28+
(map <!!)
29+
(apply f)))
30+
31+
(defn patch
32+
"prints compact patch between two JSON objects"
33+
[left right]
34+
(println (compact-diff left right {:algo :quick})))
1435

1536
(defn diff
16-
"returns diff between JSON files"
17-
[file-name1 file-name2]
18-
(let [c1 (chan)
19-
c2 (chan)]
20-
(go (>! c1 (open-parse file-name1)))
21-
(go (>! c2 (open-parse file-name2)))
22-
(ddiff/diff (<!! c1) (<!! c2))))
37+
"prints visual diff between two JSON objects"
38+
[left right]
39+
(ddiff/pretty-print
40+
(ddiff/diff left right)))
41+
42+
(defn print-error-exit
43+
"prints error message and exits with error code"
44+
[msg code]
45+
(println msg)
46+
(System/exit code))
2347

2448
(defn -main
25-
"Main function"
49+
"main function"
2650
[& args]
27-
(if (> 2 (count args))
28-
(println "Not enough parameters!")
29-
(ddiff/pretty-print
30-
(diff
31-
(first args)
32-
(second args)))))
51+
(let [{files :arguments
52+
errors :errors
53+
{:keys [mode]} :options} (parse-opts args cli-options)]
54+
(cond
55+
errors (print-error-exit (str/join "\n" errors) 1)
56+
(not= 2 (count files)) (print-error-exit "you need to pass two JSON files!" 1)
57+
(= :visual mode) (apply process diff files)
58+
(= :patch mode) (apply process patch files))))

0 commit comments

Comments
 (0)