Skip to content

Commit

Permalink
json number support int64
Browse files Browse the repository at this point in the history
  • Loading branch information
tenfyzhong committed Jan 7, 2024
1 parent c480c1d commit 542b68b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
37 changes: 31 additions & 6 deletions json_parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package st2

import (
"encoding/json"
"fmt"
"io"
"sort"

json "github.com/json-iterator/go"
jsoniter "github.com/json-iterator/go"
)

var (
jsonapi = jsoniter.Config{UseNumber: true}.Froze()
)

type JsonParser struct {
Expand Down Expand Up @@ -35,7 +40,7 @@ func (p *JsonParser) Parse(reader io.Reader) ([]*Struct, error) {
}

var v interface{}
err = json.Unmarshal(data, &v)
err = jsonapi.Unmarshal(data, &v)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -82,11 +87,22 @@ func (p *JsonParser) parseStructs(root *Node) *Member {
}

switch root.Type {
case NullVal, BoolVal, Float64Val, StringVal:
case NullVal,
BoolVal,
Float64Val,
StringVal,
Int8Val,
Int16Val,
Int32Val,
Int64Val,
Uint8Val,
Uint16Val,
Uint32Val,
Uint64Val:
member.Type = root.Type
case ArrayVal:
if len(root.Children) == 0 {
// ignore the current memeber if the array is empty
// ignore the current member if the array is empty
// the type of element is unknown
return nil
}
Expand Down Expand Up @@ -160,8 +176,17 @@ func (p *JsonParser) parseNode(tag string, v interface{}) *Node {
switch c := v.(type) {
case bool:
node.Type = BoolVal
case float64:
node.Type = Float64Val
case json.Number:
_, err := c.Int64()
if err == nil {
node.Type = Int64Val
} else {
node.Type = Float64Val
}
// case int64:
// node.Type = Int64Val
// case float64:
// node.Type = Float64Val
case string:
node.Type = StringVal
case map[string]interface{}:
Expand Down
28 changes: 17 additions & 11 deletions json_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "a",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"a,omitempty"`},
},
Expand All @@ -93,15 +93,15 @@ func TestJsonParser_Parse(t *testing.T) {
},
args: func(t *testing.T) args {
return args{
reader: bytes.NewReader([]byte(`{"a":1, "b":null}`)),
reader: bytes.NewReader([]byte(`{"a":1, "b":null, "c": 1.1}`)),
}
},
want1: []*Struct{
{
Members: []*Member{
{
Field: "a",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"a,omitempty"`},
},
Expand All @@ -111,6 +111,12 @@ func TestJsonParser_Parse(t *testing.T) {
Index: 2,
GoTag: []string{`json:"b,omitempty"`},
},
{
Field: "c",
Type: Float64Val,
Index: 3,
GoTag: []string{`json:"c,omitempty"`},
},
},
Type: &StructLikeType{
Name: "HelloWorld",
Expand Down Expand Up @@ -167,7 +173,7 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "b",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"b,omitempty"`},
},
Expand All @@ -187,13 +193,13 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "b",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"b,omitempty"`},
},
{
Field: "c",
Type: Float64Val,
Type: Int64Val,
Index: 2,
GoTag: []string{`json:"c,omitempty"`},
},
Expand Down Expand Up @@ -257,7 +263,7 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "ggg",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"ggg,omitempty"`},
},
Expand Down Expand Up @@ -384,7 +390,7 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "b",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"b,omitempty"`},
},
Expand All @@ -404,13 +410,13 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "b",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"b,omitempty"`},
},
{
Field: "c",
Type: Float64Val,
Type: Int64Val,
Index: 2,
GoTag: []string{`json:"c,omitempty"`},
},
Expand Down Expand Up @@ -474,7 +480,7 @@ func TestJsonParser_Parse(t *testing.T) {
Members: []*Member{
{
Field: "ggg",
Type: Float64Val,
Type: Int64Val,
Index: 1,
GoTag: []string{`json:"ggg,omitempty"`},
},
Expand Down
18 changes: 9 additions & 9 deletions st2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ func TestConvert(t *testing.T) {
return a
},
wantData: []byte(`type A struct {
B float64 ` + "`json:\"b,omitempty\"`" + `
B int64 ` + "`json:\"b,omitempty\"`" + `
C string ` + "`json:\"c,omitempty\"`" + `
}
type D struct {
B float64 ` + "`json:\"b,omitempty\"`" + `
C float64 ` + "`json:\"c,omitempty\"`" + `
B int64 ` + "`json:\"b,omitempty\"`" + `
C int64 ` + "`json:\"c,omitempty\"`" + `
}
type E struct {
Expand Down Expand Up @@ -220,13 +220,13 @@ type Root struct {
return a
},
wantData: []byte(`message A {
double b = 1;
int64 b = 1;
string c = 2;
}
message D {
double b = 1;
double c = 2;
int64 b = 1;
int64 c = 2;
}
message E {
Expand Down Expand Up @@ -295,13 +295,13 @@ message Root {
return a
},
wantData: []byte(`struct A {
1: double b,
1: i64 b,
2: string c,
}
struct D {
1: double b,
2: double c,
1: i64 b,
2: i64 c,
}
struct E {
Expand Down

0 comments on commit 542b68b

Please sign in to comment.