Skip to content
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

unmarshal *Json into an interface value #76

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
9 changes: 9 additions & 0 deletions simplejson_go10.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func (j *Json) UnmarshalJSON(p []byte) error {
return json.Unmarshal(p, &j.data)
}

// To unmarshal *Json into an interface value
func (j *Json) Unmarshal(val interface{}) error {
p, err := j.Encode()
if err != nil {
return err
}
return json.Unmarshal(p, val)
}

// Float64 coerces into a float64
func (j *Json) Float64() (float64, error) {
switch j.data.(type) {
Expand Down
11 changes: 11 additions & 0 deletions simplejson_go11.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func (j *Json) UnmarshalJSON(p []byte) error {
return dec.Decode(&j.data)
}

// To unmarshal *Json into an interface value
func (j *Json) Unmarshal(val interface{}) error {
p, err := j.Encode()
if err != nil {
return err
}
dec := json.NewDecoder(bytes.NewBuffer(p))
dec.UseNumber()
return dec.Decode(val)
}

// NewFromReader returns a *Json by decoding from an io.Reader
func NewFromReader(r io.Reader) (*Json, error) {
j := new(Json)
Expand Down