Skip to content

Commit 8e52ba7

Browse files
committed
add options, readme
1 parent d0b8f7c commit 8e52ba7

File tree

8 files changed

+147
-11
lines changed

8 files changed

+147
-11
lines changed

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# hjson-go
2+
3+
[![Build Status](https://img.shields.io/travis/laktak/hjson-go.svg?style=flat-square)](http://travis-ci.org/laktak/hjson-go)
4+
[![Releases](https://img.shields.io/github/release/laktak/hjson-go.svg?style=flat-square)](https://github.com/laktak/hjson-go/releases)
5+
6+
![Hjson Intro](http://hjson.org/hjson1.gif)
7+
8+
```
9+
{
10+
# specify rate in requests/second (because comments are helpful!)
11+
rate: 1000
12+
13+
// prefer c-style comments?
14+
/* feeling old fashioned? */
15+
16+
# did you notice that rate doesn't need quotes?
17+
hey: look ma, no quotes for strings either!
18+
19+
# best of all
20+
notice: []
21+
anything: ?
22+
23+
# yes, commas are optional!
24+
}
25+
```
26+
27+
The Go implementation of Hjson is based on [hjson-js](https://github.com/laktak/hjson-js). For other platforms see [hjson.org](http://hjson.org).
28+
29+
# Install
30+
31+
Make sure you have a working Go environment. See the [install instructions](http://golang.org/doc/install.html).
32+
33+
To install hjson, simply run:
34+
35+
`go get -u github.com/laktak/hjson-go`
36+
37+
## From the Commandline
38+
39+
Install with `go get -u github.com/laktak/hjson-go/hjson-cli`
40+
41+
```
42+
usage: hjson-cli [OPTIONS] [INPUT]
43+
hjson can be used to convert JSON from/to Hjson.
44+
45+
hjson will read the given JSON/Hjson input file or read from stdin.
46+
47+
Options:
48+
-allowMinusZero
49+
Allow -0.
50+
-bracesSameLine
51+
Print braces on the same line.
52+
-c Output as JSON.
53+
-h Show this screen.
54+
-indentBy string
55+
The indent string. (default " ")
56+
-j Output as formatted JSON.
57+
-omitRootBraces
58+
Omit braces at the root.
59+
-quoteAlways
60+
Always quote string values.
61+
```
62+
63+
Sample:
64+
- run `hjson-cli test.json > test.hjson` to convert to Hjson
65+
- run `hjson-cli -j test.hjson > test.json` to convert to JSON
66+
67+
# Usage
68+
69+
```go
70+
71+
package main
72+
73+
import (
74+
"github.com/laktak/hjson-go"
75+
"fmt"
76+
)
77+
78+
func main() {
79+
80+
// Now let's look at decoding JSON data into Go
81+
// values.
82+
sample := []byte(`{"num":6.13,"strs":["a","b"]}`)
83+
84+
// We need to provide a variable where Hjson
85+
// can put the decoded data.
86+
var dat map[string]interface{}
87+
88+
// Decode and a check for errors.
89+
if err := hjson.Unmarshal(sample, &dat); err != nil {
90+
panic(err)
91+
}
92+
fmt.Println(dat)
93+
94+
// In order to use the values in the decoded map,
95+
// we'll need to cast them to their appropriate type.
96+
97+
num := dat["num"].(float64)
98+
fmt.Println(num)
99+
100+
strs := dat["strs"].([]interface{})
101+
str1 := strs[0].(string)
102+
fmt.Println(str1)
103+
104+
105+
// To encode to Hjson with default options:
106+
sampleMap := map[string]int{"apple": 5, "lettuce": 7}
107+
hjson, _ := hjson.Marshal(sampleMap)
108+
// this is short for:
109+
// options := hjson.DefaultOptions()
110+
// hjson, _ := hjson.MarshalWithOptions(sampleMap, options)
111+
fmt.Println(string(hjson))
112+
}
113+
114+
```

encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (e *hjsonEncoder) str(value reflect.Value, noIndent bool, separator string,
269269

270270
showBraces := !isRootObject || e.EmitRootBraces
271271
indent1 := e.indent
272-
e.indent++
272+
if showBraces { e.indent++ }
273273

274274
if (showBraces) {
275275
if !noIndent && !e.BracesSameLine {
@@ -285,7 +285,7 @@ func (e *hjsonEncoder) str(value reflect.Value, noIndent bool, separator string,
285285

286286
// Join all of the member texts together, separated with newlines
287287
for i := 0; i < len; i++ {
288-
e.writeIndent(e.indent)
288+
if i > 0 || showBraces { e.writeIndent(e.indent) }
289289
e.WriteString(e.quoteName(keys[i].String()))
290290
e.WriteString(":")
291291
if err := e.str(value.MapIndex(keys[i]), false, " ", false); err != nil { return err }

hjson-cli/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func fixJson(data []byte) ([]byte) {
2424
func main() {
2525

2626
flag.Usage = func() {
27-
fmt.Println("usage: hjson [OPTIONS] [INPUT]")
27+
fmt.Println("usage: hjson-cli [OPTIONS] [INPUT]")
2828
fmt.Println("hjson can be used to convert JSON from/to Hjson.")
2929
fmt.Println("")
3030
fmt.Println("hjson will read the given JSON/Hjson input file or read from stdin.")
@@ -36,11 +36,17 @@ func main() {
3636
var help = flag.Bool("h", false, "Show this screen.")
3737
var showJson = flag.Bool("j", false, "Output as formatted JSON.")
3838
var showCompact = flag.Bool("c", false, "Output as JSON.")
39+
40+
var indentBy = flag.String("indentBy", " ", "The indent string.")
41+
var bracesSameLine = flag.Bool("bracesSameLine", false, "Print braces on the same line.")
42+
var omitRootBraces = flag.Bool("omitRootBraces", false, "Omit braces at the root.")
43+
var quoteAlways = flag.Bool("quoteAlways", false, "Always quote string values.")
44+
var allowMinusZero = flag.Bool("allowMinusZero", false, "Allow -0.")
45+
3946
// var showVersion = flag.Bool("V", false, "Show version.")
4047

4148
flag.Parse()
4249
if *help || flag.NArg() > 1 {
43-
fmt.Println("{}", flag.NArg())
4450
flag.Usage()
4551
os.Exit(1)
4652
}
@@ -66,11 +72,17 @@ func main() {
6672
if err != nil { panic(err) }
6773
out = fixJson(out)
6874
} else if *showJson {
69-
out, err = json.MarshalIndent(value, "", " ")
75+
out, err = json.MarshalIndent(value, "", *indentBy)
7076
if err != nil { panic(err) }
7177
out = fixJson(out)
7278
} else {
73-
out, err = hjson.Marshal(value)
79+
opt := hjson.DefaultOptions()
80+
opt.IndentBy = *indentBy
81+
opt.BracesSameLine = *bracesSameLine
82+
opt.EmitRootBraces = !*omitRootBraces
83+
opt.QuoteAlways = *quoteAlways
84+
opt.AllowMinusZero = *allowMinusZero
85+
out, err = hjson.MarshalWithOptions(value, opt)
7486
}
7587

7688
fmt.Println(string(out))

test/assets/sorted/strings_result.hjson

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,7 @@
6969
text1: This is a valid string value.
7070
text2: a \ is just a \
7171
text3: "You need quotes\tfor escapes"
72-
text4: " untrimmed "
72+
text4a: " untrimmed "
73+
text4b: " untrimmed"
74+
text4c: "untrimmed "
7375
}

test/assets/sorted/strings_result.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@
4949
"text1": "This is a valid string value.",
5050
"text2": "a \\ is just a \\",
5151
"text3": "You need quotes\tfor escapes",
52-
"text4": " untrimmed "
52+
"text4a": " untrimmed ",
53+
"text4b": " untrimmed",
54+
"text4c": "untrimmed "
5355
}

test/assets/strings_result.hjson

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
text1: This is a valid string value.
33
text2: a \ is just a \
44
text3: "You need quotes\tfor escapes"
5-
text4: " untrimmed "
5+
text4a: " untrimmed "
6+
text4b: " untrimmed"
7+
text4c: "untrimmed "
68
multiline1:
79
'''
810
first line

test/assets/strings_result.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"text1": "This is a valid string value.",
33
"text2": "a \\ is just a \\",
44
"text3": "You need quotes\tfor escapes",
5-
"text4": " untrimmed ",
5+
"text4a": " untrimmed ",
6+
"text4b": " untrimmed",
7+
"text4c": "untrimmed ",
68
"multiline1": "first line\n indented line\nlast line",
79
"multiline2": "first line\n indented line\nlast line",
810
"multiline3": "first line\n indented line\nlast line\n",

test/assets/strings_test.hjson

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
text3: "You need quotes\tfor escapes"
88

9-
text4: " untrimmed "
9+
text4a: " untrimmed "
10+
text4b: " untrimmed"
11+
text4c: "untrimmed "
1012

1113
# multiline string
1214

0 commit comments

Comments
 (0)