Skip to content

Commit 3c237cd

Browse files
author
Tao Wen
committed
add ReadRawMessage
1 parent e3e5d38 commit 3c237cd

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

gen/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ func genDecodeStmt(node ast.Node, ptr string) {
264264
_f(" "+genAnonymousStruct(x), ptr)
265265
case *ast.MapType:
266266
_f(" "+genAnonymousMap(x), ptr)
267+
case *ast.InterfaceType:
268+
if nodeToString(node) == "interface{}" {
269+
_f(" iter.ReadInterface(%s)", ptr)
270+
} else {
271+
reportError(fmt.Errorf("unknown type: %s", nodeToString(node)))
272+
return
273+
}
267274
default:
268275
reportError(fmt.Errorf("unknown type: %s", nodeToString(node)))
269276
return

iter_skip.go

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ func (iter *Iterator) skipString() error {
9393
return iter.ReportError("skipString", "incomplete string")
9494
}
9595

96+
func (iter *Iterator) ReadRawMessage(out *RawMessage) (ret error) {
97+
before := iter.head
98+
ret = iter.Skip()
99+
if ret == nil {
100+
after := iter.head
101+
*out = iter.buf[before:after]
102+
}
103+
return
104+
}
105+
96106
// Skip skips a json object and positions to relatively the next json object
97107
func (iter *Iterator) Skip() error {
98108
return iter.skip(iter.nextToken())

iter_tests/skip_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ func Test_skip(t *testing.T) {
3434
}
3535
}
3636

37+
func Test_read_raw_message(t *testing.T) {
38+
var msg jsoniter.RawMessage
39+
iter := jsoniter.ParseBytes([]byte(`"hello",100`))
40+
iter.ReadRawMessage(&msg)
41+
if string(msg) != `"hello"` {
42+
t.Fatal()
43+
}
44+
}
45+
3746
func Test_skip_null(t *testing.T) {
3847
parseNull := func() *jsoniter.Iterator {
3948
return jsoniter.ParseBytes([]byte(`null`))

value_tests/NamedStruct.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import "bytes"
55
//go:generate go run github.com/json-iterator/tinygo/gen
66
type NamedStruct struct {
77
Name string
8-
Price int
8+
Price interface{}
99
privateField *bytes.Buffer
1010
}

value_tests/NamedStruct_json.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func NamedStruct_json_unmarshal(iter *jsoniter.Iterator, out *NamedStruct) {
1010
case field == `Name`:
1111
iter.ReadString(&(*out).Name)
1212
case field == `Price`:
13-
iter.ReadInt(&(*out).Price)
13+
iter.ReadInterface(&(*out).Price)
1414
default:
1515
iter.Skip()
1616
}

0 commit comments

Comments
 (0)