Skip to content

Commit b771b66

Browse files
committed
- [+] add -u, --unescape option to fmt
1 parent cebc845 commit b771b66

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

cmd_fmt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020

2121
// The FmtCommand type defines all the configurable options from cli.
2222
type FmtCommand struct {
23-
Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
24-
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
23+
Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
24+
Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
25+
Unescape bool `short:"u" long:"unescape" description:"Unescape unicode of form \u003c to their literal characters"`
2526
}
2627

2728
var fmtCommand FmtCommand

imp_fmt.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"bytes"
1111
"encoding/json"
1212
"fmt"
13+
"regexp"
14+
"strconv"
1315

1416
"github.com/go-easygen/go-flags/clis"
1517
)
@@ -20,6 +22,9 @@ func (x *FmtCommand) Exec(args []string) error {
2022
fileI := clis.GetInputStream(x.Filei)
2123
defer fileI.Close()
2224
data := readJson(fileI)
25+
if x.Unescape {
26+
data = unescapeUnicode(data)
27+
}
2328

2429
var out bytes.Buffer
2530
var err error
@@ -35,3 +40,16 @@ func (x *FmtCommand) Exec(args []string) error {
3540
fmt.Fprintln(fileO)
3641
return nil
3742
}
43+
44+
func unescapeUnicode(b []byte) []byte {
45+
// Unescape Unicode escape sequences like \u003c to actual characters
46+
re := regexp.MustCompile(`\\u[0-9a-fA-F]{4}`)
47+
return re.ReplaceAllFunc(b, func(match []byte) []byte {
48+
// Convert \uXXXX to actual character
49+
r, err := strconv.ParseInt(string(match[2:]), 16, 32)
50+
if err != nil {
51+
return match
52+
}
53+
return []byte(string(rune(r)))
54+
})
55+
}

jsonfiddle_cli.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ Command:
7575
Value: "-"
7676
Usage: 'the output, default to stdout'
7777

78+
- Name: Unescape
79+
Type: bool
80+
Flag: 'u,unescape'
81+
Usage: "Unescape unicode of form \\u003c to their literal characters"
82+
7883
- Name: sort
7984
Desc: "Sort json fields recursively"
8085
Text: ''

jsonfiddle_cliDef.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package main
77
////////////////////////////////////////////////////////////////////////////
88
// Program: jsonfiddle
99
// Purpose: JSON Fiddling
10-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
10+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
1111
////////////////////////////////////////////////////////////////////////////
1212

1313
import (
@@ -28,7 +28,7 @@ import (
2828
// var (
2929
// progname = "jsonfiddle"
3030
// version = "0.1.0"
31-
// date = "2023-01-23"
31+
// date = "2025-01-15"
3232

3333
// // opts store all the configurable options
3434
// opts optsT
@@ -62,7 +62,7 @@ import (
6262
// func showVersion() {
6363
// fmt.Fprintf(os.Stderr, "jsonfiddle - JSON Fiddling, version %s\n", version)
6464
// fmt.Fprintf(os.Stderr, "Built on %s\n", date)
65-
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
65+
// fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
6666
// fmt.Fprintf(os.Stderr, "Tool to fiddle with json strings\n")
6767
// os.Exit(0)
6868
// }
@@ -92,7 +92,7 @@ type optsT struct {
9292
////////////////////////////////////////////////////////////////////////////
9393
// Program: jsonfiddle
9494
// Purpose: JSON Fiddling
95-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
95+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
9696
////////////////////////////////////////////////////////////////////////////
9797

9898
// package main
@@ -130,7 +130,7 @@ type optsT struct {
130130
//
131131
// func (x *EscCommand) Execute(args []string) error {
132132
// fmt.Fprintf(os.Stderr, "Escape json string\n")
133-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
133+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
134134
// clis.Setup("jsonfiddle::esc", opts.Verbose)
135135
// clis.Verbose(1, "Doing Esc, with %+v, %+v", opts, args)
136136
// // fmt.Println(x.Filei, x.Fileo)
@@ -151,7 +151,7 @@ type optsT struct {
151151
////////////////////////////////////////////////////////////////////////////
152152
// Program: jsonfiddle
153153
// Purpose: JSON Fiddling
154-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
154+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
155155
////////////////////////////////////////////////////////////////////////////
156156

157157
// package main
@@ -172,6 +172,7 @@ type optsT struct {
172172
// type FmtCommand struct {
173173
// Filei string `short:"i" long:"input" description:"the source to get json string from (mandatory)" required:"true"`
174174
// Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"`
175+
// Unescape bool `short:"u" long:"unescape" description:"Unescape unicode of form \u003c to their literal characters"`
175176
// }
176177

177178
//
@@ -189,10 +190,10 @@ type optsT struct {
189190
//
190191
// func (x *FmtCommand) Execute(args []string) error {
191192
// fmt.Fprintf(os.Stderr, "Format json string\n")
192-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
193+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
193194
// clis.Setup("jsonfiddle::fmt", opts.Verbose)
194195
// clis.Verbose(1, "Doing Fmt, with %+v, %+v", opts, args)
195-
// // fmt.Println(x.Filei, x.Fileo)
196+
// // fmt.Println(x.Filei, x.Fileo, x.Unescape)
196197
// return x.Exec(args)
197198
// }
198199
//
@@ -210,7 +211,7 @@ type optsT struct {
210211
////////////////////////////////////////////////////////////////////////////
211212
// Program: jsonfiddle
212213
// Purpose: JSON Fiddling
213-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
214+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
214215
////////////////////////////////////////////////////////////////////////////
215216

216217
// package main
@@ -248,7 +249,7 @@ type optsT struct {
248249
//
249250
// func (x *SortCommand) Execute(args []string) error {
250251
// fmt.Fprintf(os.Stderr, "Sort json fields recursively\n")
251-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
252+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
252253
// clis.Setup("jsonfiddle::sort", opts.Verbose)
253254
// clis.Verbose(1, "Doing Sort, with %+v, %+v", opts, args)
254255
// // fmt.Println(x.Filei, x.Fileo)
@@ -269,7 +270,7 @@ type optsT struct {
269270
////////////////////////////////////////////////////////////////////////////
270271
// Program: jsonfiddle
271272
// Purpose: JSON Fiddling
272-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
273+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
273274
////////////////////////////////////////////////////////////////////////////
274275

275276
// package main
@@ -311,7 +312,7 @@ type optsT struct {
311312
//
312313
// func (x *J2sCommand) Execute(args []string) error {
313314
// fmt.Fprintf(os.Stderr, "JSON to struct\n")
314-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
315+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
315316
// clis.Setup("jsonfiddle::j2s", opts.Verbose)
316317
// clis.Verbose(1, "Doing J2s, with %+v, %+v", opts, args)
317318
// // fmt.Println(x.FmtType, x.Filei, x.Fileo, x.Name, x.Pkg, x.SubStruct)
@@ -332,7 +333,7 @@ type optsT struct {
332333
////////////////////////////////////////////////////////////////////////////
333334
// Program: jsonfiddle
334335
// Purpose: JSON Fiddling
335-
// Authors: Tong Sun (c) 2017-2023, All rights reserved
336+
// Authors: Tong Sun (c) 2017-2025, All rights reserved
336337
////////////////////////////////////////////////////////////////////////////
337338

338339
// package main
@@ -370,7 +371,7 @@ type optsT struct {
370371
//
371372
// func (x *X2jCommand) Execute(args []string) error {
372373
// fmt.Fprintf(os.Stderr, "XML to JSON\n")
373-
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n")
374+
// // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2025, Tong Sun\n\n")
374375
// clis.Setup("jsonfiddle::x2j", opts.Verbose)
375376
// clis.Verbose(1, "Doing X2j, with %+v, %+v", opts, args)
376377
// // fmt.Println(x.Filei, x.Fileo)

0 commit comments

Comments
 (0)