Skip to content

Commit b07cedb

Browse files
committed
- [+] add cmd esc, Escape json string, OK
1 parent 71b3f56 commit b07cedb

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.e.md

+22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The `jsonfiddle` makes it easy to look at the JSON data from different aspects.
2020

2121
### $ {{exec "jsonfiddle" | color "sh"}}
2222

23+
### $ {{shell "jsonfiddle esc" | color "sh"}}
24+
2325
### $ {{shell "jsonfiddle fmt" | color "sh"}}
2426

2527
### $ {{shell "jsonfiddle sort" | color "sh"}}
@@ -28,6 +30,26 @@ The `jsonfiddle` makes it easy to look at the JSON data from different aspects.
2830

2931
# Examples
3032

33+
## Escape with `jsonfiddle esc`
34+
35+
### $ {{shell "jsonfiddle esc -i test/Customer.ref" | color "json"}}
36+
37+
### Usage
38+
39+
`jsonfiddle esc` will escape any arbitrary string so as to embed it as content of json string. This seems useless at first, but it actually allows you to embed any arbitrary file into [GitHub Gists JSON API](https://developer.github.com/v3/gists/), so as to post any arbitrary file onto GitHub Gist:
40+
41+
42+
echo '{"description":"SmartyStreets API Demo","public":true,"files":{"SmartyStreets.json":{"content":'"`jsonfiddle fmt -i test/SmartyStreets.json | jsonfiddle esc -i`"'}}}' | curl --data @- https://api.github.com/gists
43+
44+
This will give you
45+
https://gist.github.com/anonymous/1423d4768dd9b88262ca513626e68d8e
46+
47+
48+
By "_arbitrary file_" I do mean arbitrary file. Check this out:
49+
https://gist.github.com/anonymous/a51798ce99ff59d8d4ba536cbf4b6996
50+
51+
This is why `jsonfiddle esc` is a command on its own, instead of being part of functionalities of `jsonfiddle fmt` or `jsonfiddle sort`.
52+
3153
## Format with `jsonfiddle fmt`
3254

3355
### Pretty print

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
- [jsonfiddle - JSON Fiddling](#jsonfiddle---json-fiddling)
1111
- [Usage](#usage)
1212
- [$ jsonfiddle](#-jsonfiddle)
13+
- [$ jsonfiddle esc](#-jsonfiddle-esc)
1314
- [$ jsonfiddle fmt](#-jsonfiddle-fmt)
1415
- [$ jsonfiddle sort](#-jsonfiddle-sort)
1516
- [$ jsonfiddle j2s](#-jsonfiddle-j2s)
1617
- [Examples](#examples)
18+
- [Escape with `jsonfiddle esc`](#escape-with-`jsonfiddle-esc`)
19+
- [$ jsonfiddle esc -i test/Customer.ref](#-jsonfiddle-esc--i-testcustomerref)
20+
- [Usage](#usage-1)
1721
- [Format with `jsonfiddle fmt`](#format-with-`jsonfiddle-fmt`)
1822
- [Pretty print](#pretty-print)
1923
- [> test/CustomerSI.ref](#-testcustomersiref)
@@ -47,7 +51,7 @@ The `jsonfiddle` makes it easy to look at the JSON data from different aspects.
4751
### $ jsonfiddle
4852
```sh
4953
JSON Fiddling
50-
Version v0.2.0 built on 2017-08-12
54+
Version v0.3.0 built on 2017-08-14
5155

5256
Tool to fiddle with json strings
5357

@@ -61,11 +65,27 @@ Options:
6165

6266
Commands:
6367

68+
esc Escape json string so as to embed it as content of json string
6469
fmt Format json string
6570
sort Sort json fields recursively
6671
j2s JSON to struct
6772
```
6873

74+
### $ jsonfiddle esc
75+
```sh
76+
Escape json string so as to embed it as content of json string
77+
78+
Options:
79+
80+
-h, --help display help information
81+
-c, --compact Compact JSON data, remove all whitespaces
82+
--prefix prefix for json string output
83+
-d, --indent[= ] indent for json string output
84+
-v, --verbose Verbose mode (Multiple -v options increase the verbosity.)
85+
-i, --input *the source to get json string from (mandatory)
86+
-o, --output the output (default: stdout)
87+
```
88+
6989
### $ jsonfiddle fmt
7090
```sh
7191
Format json string
@@ -117,6 +137,29 @@ Options:
117137

118138
# Examples
119139

140+
## Escape with `jsonfiddle esc`
141+
142+
### $ jsonfiddle esc -i test/Customer.ref
143+
```json
144+
"{\n \"firstName\": \"John\",\n \"lastName\": \"Smith\",\n \"age\": 25,\n \"address\": {\n \"streetAddress\": \"21 2nd Street\",\n \"city\": \"New York\",\n \"state\": \"NY\",\n \"postalCode\": \"10021\"\n },\n \"phoneNumber\": [\n {\n \"type\": \"home\",\n \"number\": \"212 555-1234\"\n },\n {\n \"type\": \"fax\",\n \"number\": \"646 555-4567\"\n }\n ]\n}\n\n"
145+
```
146+
147+
### Usage
148+
149+
`jsonfiddle esc` will escape any arbitrary string so as to embed it as content of json string. This seems useless at first, but it actually allows you to embed any arbitrary file into [GitHub Gists JSON API](https://developer.github.com/v3/gists/), so as to post any arbitrary file onto GitHub Gist:
150+
151+
152+
echo '{"description":"SmartyStreets API Demo","public":true,"files":{"SmartyStreets.json":{"content":'"`jsonfiddle fmt -i test/SmartyStreets.json | jsonfiddle esc -i`"'}}}' | curl --data @- https://api.github.com/gists
153+
154+
This will give you
155+
https://gist.github.com/anonymous/1423d4768dd9b88262ca513626e68d8e
156+
157+
158+
By "_arbitrary file_" I do mean arbitrary file. Check this out:
159+
https://gist.github.com/anonymous/a51798ce99ff59d8d4ba536cbf4b6996
160+
161+
This is why `jsonfiddle esc` is a command on its own, instead of being part of functionalities of `jsonfiddle fmt` or `jsonfiddle sort`.
162+
120163
## Format with `jsonfiddle fmt`
121164

122165
### Pretty print

cmdEsc.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77
package main
88

99
import (
10+
"encoding/json"
1011
"fmt"
12+
"io/ioutil"
1113

1214
"github.com/mkideal/cli"
1315
)
1416

1517
func escCLI(ctx *cli.Context) error {
1618
rootArgv = ctx.RootArgv().(*rootT)
1719
argv := ctx.Argv().(*escT)
18-
fmt.Printf("[esc]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
20+
// fmt.Printf("[esc]:\n %+v\n %+v\n %v\n", rootArgv, argv, ctx.Args())
21+
Opts.Prefix, Opts.Indent, Opts.Compact, Opts.Verbose =
22+
rootArgv.Prefix, rootArgv.Indent, rootArgv.Compact, rootArgv.Verbose.Value()
23+
24+
data, err := ioutil.ReadAll(argv.Filei)
25+
argv.Filei.Close()
26+
abortOn("[::esc] Reading input", err)
27+
// fmt.Printf("] %s", data)
28+
29+
out, err := json.Marshal(string(data))
30+
abortOn("[::esc] Formatting input", err)
31+
fmt.Fprintf(argv.Fileo, "%s", out)
32+
1933
return nil
2034
}

0 commit comments

Comments
 (0)