8000 add UnmarshalPartially by davidwtf · Pull Request #15 · hjson/hjson-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add UnmarshalPartially #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
43 changes: 41 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (p *hjsonParser) readString(allowML bool) (string, error) {
return "", p.errAt("Bad escape \\" + string(p.ch))
}
} else if p.ch == '\n' || p.ch == '\r' {
return "", p.errAt("Bad string containing newline");
return "", p.errAt("Bad string containing newline")
} else {
res.WriteByte(p.ch)
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func (p *hjsonParser) rootValue() (interface{}, error) {
}

// assume we have a root object without braces
res, err := p.checkTrailing(p.readObject(true));
res, err := p.checkTrailing(p.readObject(true))
if err == nil {
return res, nil
}
Expand Down Expand Up @@ -479,3 +479,42 @@ func Unmarshal(data []byte, v interface{}) (err error) {
rv.Set(reflect.ValueOf(value))
return err
}

func (p *hjsonParser) rootValueWithTrail() (interface{}, error) {
// Braces for the root object are optional

p.white()
switch p.ch {
case '{':
return p.readO 8000 bject(false)
case '[':
return p.readArray()
}

// assume we have a root object without braces
res, err := p.readObject(true)
if err == nil {
return res, nil
}

// test if we are dealing with a single JSON value instead (true/false/null/num/"")
p.resetAt()
if res2, err2 := p.readValue(); err2 == nil {
return res2, nil
}
return res, err
}

// UnmarshalPartially try to partially parse the Hjson-encoded data,
// return the value and the start position of unprocessed parts.
func UnmarshalPartially(data []byte) (v interface{}, next int, err error) {
parser := &hjsonParser{data, 0, ' '}
parser.resetAt()

v, err = parser.rootValueWithTrail()
if err != nil {
return
}
next = parser.at + 1
return
}
31 changes: 31 additions & 0 deletions hjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,34 @@ func TestInvalidDestinationType(t *testing.T) {
panic("An error should occur")
}
}

func TestUnmarshalPartially(t *testing.T) {
var src = `
{
database:
{
host: 127.0.0.1
port: 555
}
}
@@there is sth cannot be parsed as hijson.
`
v, next, err := UnmarshalPartially([]byte(src))
if err != nil {
panic(err)
}
if next != strings.Index(src, "@") {
panic("wrong next value")
}
if v == nil {
panic("v is nil")
}
val, ok := v.(map[string]interface{})
if !ok {
panic("v has wrong type")
}
_, ok = val["database"]
if !ok {
panic("v has wrong value")
}
}
0