Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Go Pkg](https://img.shields.io/github/release/hjson/hjson-go.svg?style=flat-square&label=go-pkg)](https://github.com/hjson/hjson-go/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/hjson/hjson-go?style=flat-square)](https://goreportcard.com/report/github.com/hjson/hjson-go)
[![coverage](https://img.shields.io/badge/coverage-ok-brightgreen.svg?style=flat-square)](https://gocover.io/github.com/hjson/hjson-go/)
[![godoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/hjson/hjson-go/v4)
[![godoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/bingoohuang/hjson)

![Hjson Intro](https://hjson.github.io/hjson1.gif)

Expand All @@ -29,7 +29,7 @@

The Go implementation of Hjson is based on [hjson-js](https://github.com/hjson/hjson-js). For other platforms see [hjson.github.io](https://hjson.github.io).

More documentation can be found at https://pkg.go.dev/github.com/hjson/hjson-go/v4
More documentation can be found at https://pkg.go.dev/github.com/bingoohuang/hjson

# Install

Expand All @@ -39,11 +39,11 @@ If you instead want to build locally, make sure you have a working Go environmen

- In order to use Hjson from your own Go source code, just add an import line like the one here below. Before building your project, run `go mod tidy` in order to download the Hjson source files. The suffix `/v4` is required in the import path, unless you specifically want to use an older major version.
```go
import "github.com/hjson/hjson-go/v4"
import "github.com/bingoohuang/hjson"
```
- If you instead want to use the **hjson-cli** command line tool, run the command here below in your terminal. The executable will be installed into your `go/bin` folder, make sure that folder is included in your `PATH` environment variable.
```bash
go install github.com/hjson/hjson-go/v4/hjson-cli@latest
go install github.com/bingoohuang/hjson/hjson-cli@latest
```
# Usage as command line tool
```
Expand Down Expand Up @@ -81,7 +81,7 @@ Sample:
package main

import (
"github.com/hjson/hjson-go/v4"
"github.com/bingoohuang/hjson"
"fmt"
)

Expand Down Expand Up @@ -142,7 +142,7 @@ If you prefer, you can also unmarshal to Go structs (including structs implement
package main

import (
"github.com/hjson/hjson-go/v4"
"github.com/bingoohuang/hjson"
"fmt"
)

Expand Down Expand Up @@ -193,7 +193,7 @@ By using key `comment` in struct field tags you can specify comments to be writt
package main

import (
"github.com/hjson/hjson-go/v4"
"github.com/bingoohuang/hjson"
"fmt"
)

Expand Down Expand Up @@ -256,7 +256,7 @@ package main
import (
"fmt"

"github.com/hjson/hjson-go/v4"
"github.com/bingoohuang/hjson"
)

func main() {
Expand Down Expand Up @@ -324,7 +324,7 @@ The ambiguity can be avoided by using typed destinations when unmarshalling. A s
package main

import (
"github.com/hjson/hjson-go/v4"
"github.com/bingoohuang/hjson"
"fmt"
)

Expand Down Expand Up @@ -370,7 +370,7 @@ In order to avoid stack overflow all unmarshal-functions return an error if the

# API

[![godoc](https://godoc.org/github.com/hjson/hjson-go/v4?status.svg)](https://godoc.org/github.com/hjson/hjson-go/v4)
[![godoc](https://godoc.org/github.com/bingoohuang/hjson?status.svg)](https://godoc.org/github.com/bingoohuang/hjson)

# History

Expand Down
2 changes: 1 addition & 1 deletion build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function build() {
OUT=${BINARIES}/hjson_${VERSION}_${GOOS}_${GOARCH}
mkdir $OUT
cd $OUT
go build -ldflags "-w -s -X main.Version=${VERSION}" github.com/hjson/hjson-go/v4/hjson-cli
go build -ldflags "-w -s -X main.Version=${VERSION}" github.com/bingoohuang/hjson/hjson-cli
if [[ $3 == "zip" ]]; then
mv $OUT/hjson-cli.exe $OUT/hjson.exe
zip -j ${OUT}.zip $OUT/*
Expand Down
42 changes: 42 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hjson

// Style is the color style
type Style struct {
Key, String, Number [2]string
True, False, Null [2]string
Escape [2]string
Remark [2]string
Append func(dst []byte, c byte) []byte
}

// TerminalStyle is for terminals
var TerminalStyle *Style

func init() {
TerminalStyle = &Style{
Key: [2]string{"\x1B[94m", "\x1B[0m"},
String: [2]string{"\x1B[92m", "\x1B[0m"},
Number: [2]string{"\x1B[93m", "\x1B[0m"},
True: [2]string{"\x1B[96m", "\x1B[0m"},
False: [2]string{"\x1B[96m", "\x1B[0m"},
Null: [2]string{"\x1B[91m", "\x1B[0m"},
Escape: [2]string{"\x1B[35m", "\x1B[0m"},
Remark: [2]string{"\x1B[90m", "\x1B[0m"},
Append: func(dst []byte, c byte) []byte {
if c < ' ' && (c != '\r' && c != '\n' && c != '\t' && c != '\v') {
dst = append(dst, "\\u00"...)
dst = append(dst, hexp((c>>4)&0xF))
return append(dst, hexp((c)&0xF))
}
return append(dst, c)
},
}
}
func hexp(p byte) byte {
switch {
case p < 10:
return p + '0'
default:
return (p - 10) + 'a'
}
}
76 changes: 36 additions & 40 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,14 @@ func (p *hjsonParser) readObject(
elemType = newDestType

if newDest.IsValid() {
if newDest.Kind() != reflect.Struct {
if newDest.Kind() == reflect.Struct {
newDest = newDest.Field(i)
} else {
// We are only keeping track of newDest in case it contains a
// tree that we will partially update. But here we have not found
// any tree, so we can ignore newDest and just look at
// newDestType instead.
newDest = reflect.Value{}
} else {
newDest = newDest.Field(i)
}
}
}
Expand Down Expand Up @@ -872,47 +872,43 @@ func (p *hjsonParser) rootValue(dest reflect.Value) (ret interface{}, err error)
return
}

if ret == nil {
// Assume we have a root object without braces.
ret, errSyntax = p.readObject(true, dest, t, ciBefore)
ciAfter, err = p.checkTrailing()
if errSyntax != nil || err != nil {
// Syntax error, or maybe a single JSON value.
ret = nil
err = nil
} else {
if p.nodeDestination {
if node, ok := ret.(*Node); ok {
p.setComment1(&node.Cm.After, ciAfter)
}
// Assume we have a root object without braces.
ret, errSyntax = p.readObject(true, dest, t, ciBefore)
ciAfter, err = p.checkTrailing()
if errSyntax != nil || err != nil {
// Syntax error, or maybe a single JSON value.
ret = nil
err = nil
} else {
if p.nodeDestination {
if node, ok := ret.(*Node); ok {
p.setComment1(&node.Cm.After, ciAfter)
}
return
}
return
}

if ret == nil {
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
p.resetAt()
ret, err = p.readValue(dest, t)
if err == nil {
ciAfter, err = p.checkTrailing()
}
if err == nil {
if p.nodeDestination {
if node, ok := ret.(*Node); ok {
// ciBefore has been read again and set on the node inside the
// function p.readValue().
existingAfter := node.Cm.After
p.setComment1(&node.Cm.After, ciAfter)
if node.Cm.After != "" {
existingAfter += "\n"
}
node.Cm.After = existingAfter + node.Cm.After
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
p.resetAt()
ret, err = p.readValue(dest, t)
if err == nil {
ciAfter, err = p.checkTrailing()
}
if err == nil {
if p.nodeDestination {
if node, ok := ret.(*Node); ok {
// ciBefore has been read again and set on the node inside the
// function p.readValue().
existingAfter := node.Cm.After
p.setComment1(&node.Cm.After, ciAfter)
if node.Cm.After != "" {
existingAfter += "\n"
}
node.Cm.After = existingAfter + node.Cm.After
}

return
}

return
}

if errSyntax != nil {
Expand Down Expand Up @@ -950,7 +946,7 @@ func orderedUnmarshal(
) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return nil, fmt.Errorf("Cannot unmarshal into non-pointer %v", reflect.TypeOf(v))
return nil, fmt.Errorf("cannot unmarshal into non-pointer %v", reflect.TypeOf(v))
}

parser := &hjsonParser{
Expand Down Expand Up @@ -1021,7 +1017,7 @@ func UnmarshalWithOptions(data []byte, v interface{}, options DecoderOptions) er
*inOM = *outOM
return nil
}
return fmt.Errorf("Cannot unmarshal into hjson.OrderedMap: Try %v as destination instead",
return fmt.Errorf("cannot unmarshal into hjson.OrderedMap: Try %v as destination instead",
reflect.TypeOf(v))
}

Expand All @@ -1037,7 +1033,7 @@ func UnmarshalWithOptions(data []byte, v interface{}, options DecoderOptions) er
// and merging.
buf, err := json.Marshal(value)
if err != nil {
return errors.New("Internal error")
return errors.New("internal error")
}

dec := json.NewDecoder(bytes.NewBuffer(buf))
Expand Down
Loading