Skip to content

Commit

Permalink
Handle json.Number (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
slshen authored Sep 10, 2020
1 parent 6978cd1 commit beaeb1d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
17 changes: 16 additions & 1 deletion jnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (n *Node) GetType() NodeType {
case string:
return Text
case int, int8, int16, int32, int64, float32, float64,
uint, uint8, uint16, uint32, uint64:
uint, uint8, uint16, uint32, uint64, json.Number:
return Number
case bool:
return Bool
Expand Down Expand Up @@ -214,6 +214,11 @@ func (n *Node) IsNull() bool {
return n == nil || n.value == nil
}

// IsNumber returns true fi the Node is a number
func (n *Node) IsNumber() bool {
return n.GetType() == Number
}

// Unwrap returns the generic value from a Node
func (n *Node) Unwrap() interface{} {
switch n.GetType() {
Expand Down Expand Up @@ -281,6 +286,11 @@ func (n *Node) AsInt() int {
return int(v)
case float64:
return int(v)
case json.Number:
if i, err := v.Int64(); err == nil {
return int(i)
}
return 0
default:
return 0
}
Expand Down Expand Up @@ -325,6 +335,11 @@ func (n *Node) AsFloat() float64 {
return float64(v)
case float64:
return v
case json.Number:
if f, err := v.Float64(); err == nil {
return f
}
return 0
default:
return 0
}
Expand Down
20 changes: 20 additions & 0 deletions jnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package jnode
import (
"bytes"
"encoding/json"
"strings"
"testing"
)

Expand Down Expand Up @@ -369,3 +370,22 @@ func TestRemove(t *testing.T) {
t.Error(n)
}
}

func TestJSONNumber(t *testing.T) {
d := json.NewDecoder(strings.NewReader(`{"i":123456789,"f":3.141592,"s":"hello"}`))
d.UseNumber()
var m map[string]interface{}
if err := d.Decode(&m); err != nil {
t.Fatal(err)
}
if i, _ := m["i"].(json.Number).Int64(); i != int64(123456789) {
t.Error(i)
}
n := FromMap(m)
if i := n.Path("i"); i.AsInt() != 123456789 {
t.Error(i)
}
if f := n.Path("f"); f.AsFloat() != 3.141592 {
t.Error(f)
}
}

6 comments on commit beaeb1d

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

@if6was9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ… We found 0 issues in your pull request. πŸŽ‰

Summary: 0 Issues Found
  • Open Secrets : 0

πŸ’» Please visit the Job build url for more information.

πŸ’¬ Share your feedback with us.

Please sign in to comment.