Permalink
Browse files

fix 4 fields object decoding

  • Loading branch information...
1 parent c70437c commit 101dfdbb2a19d8ce1a803e1eb8d37d778167a1a9 @taowen taowen committed Jan 6, 2017
View
Oops, something went wrong.
View
@@ -1,6 +1,9 @@
package jsoniter
-import "io"
+import (
+ "io"
+ "unsafe"
+)
// Unmarshal adapts to json/encoding APIs
func Unmarshal(data []byte, v interface{}) error {
@@ -11,3 +14,13 @@ func Unmarshal(data []byte, v interface{}) error {
}
return iter.Error
}
+
+func UnmarshalString(str string, v interface{}) error {
+ data := *(*[]byte)(unsafe.Pointer(&str))
+ iter := ParseBytes(data)
+ iter.Read(v)
+ if iter.Error == io.EOF {
+ return nil
+ }
+ return iter.Error
+}
View
@@ -25,139 +25,6 @@ type Decoder interface {
decode(ptr unsafe.Pointer, iter *Iterator)
}
-type stringDecoder struct {
-}
-
-func (decoder *stringDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*string)(ptr)) = iter.ReadString()
-}
-
-type intDecoder struct {
-}
-
-func (decoder *intDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*int)(ptr)) = iter.ReadInt()
-}
-
-type int8Decoder struct {
-}
-
-func (decoder *int8Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*int8)(ptr)) = iter.ReadInt8()
-}
-
-type int16Decoder struct {
-}
-
-func (decoder *int16Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*int16)(ptr)) = iter.ReadInt16()
-}
-
-type int32Decoder struct {
-}
-
-func (decoder *int32Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*int32)(ptr)) = iter.ReadInt32()
-}
-
-type int64Decoder struct {
-}
-
-func (decoder *int64Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*int64)(ptr)) = iter.ReadInt64()
-}
-
-type uintDecoder struct {
-}
-
-func (decoder *uintDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*uint)(ptr)) = iter.ReadUint()
-}
-
-type uint8Decoder struct {
-}
-
-func (decoder *uint8Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*uint8)(ptr)) = iter.ReadUint8()
-}
-
-type uint16Decoder struct {
-}
-
-func (decoder *uint16Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*uint16)(ptr)) = iter.ReadUint16()
-}
-
-type uint32Decoder struct {
-}
-
-func (decoder *uint32Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*uint32)(ptr)) = iter.ReadUint32()
-}
-
-type uint64Decoder struct {
-}
-
-func (decoder *uint64Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*uint64)(ptr)) = iter.ReadUint64()
-}
-
-type float32Decoder struct {
-}
-
-func (decoder *float32Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*float32)(ptr)) = iter.ReadFloat32()
-}
-
-type float64Decoder struct {
-}
-
-func (decoder *float64Decoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*float64)(ptr)) = iter.ReadFloat64()
-}
-
-type boolDecoder struct {
-}
-
-func (decoder *boolDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*bool)(ptr)) = iter.ReadBool()
-}
-
-type interfaceDecoder struct {
-}
-
-func (decoder *interfaceDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*interface{})(ptr)) = iter.ReadAny().Get()
-}
-
-type anyDecoder struct {
-}
-
-func (decoder *anyDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- *((*Any)(ptr)) = *iter.ReadAny()
-}
-
-type stringNumberDecoder struct {
- elemDecoder Decoder
-}
-
-func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- c := iter.nextToken()
- if c != '"' {
- iter.reportError("stringNumberDecoder", `expect "`)
- return
- }
- decoder.elemDecoder.decode(ptr, iter)
- if iter.Error != nil {
- return
- }
- c = iter.readByte()
- if c != '"' {
- iter.reportError("stringNumberDecoder", `expect "`)
- return
- }
-}
-
type optionalDecoder struct {
valueType reflect.Type
valueDecoder Decoder
@@ -216,7 +83,17 @@ type oneFieldStructDecoder struct {
}
func (decoder *oneFieldStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
+ if !iter.readObjectStart() {
+ return
+ }
+ field := iter.readObjectField()
+ if field == decoder.fieldName {
+ decoder.fieldDecoder.decode(ptr, iter)
+ } else {
+ iter.Skip()
+ }
+ for iter.nextToken() == ',' {
+ field = iter.readObjectField()
if field == decoder.fieldName {
decoder.fieldDecoder.decode(ptr, iter)
} else {
@@ -237,7 +114,20 @@ type twoFieldsStructDecoder struct {
}
func (decoder *twoFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
+ if !iter.readObjectStart() {
+ return
+ }
+ field := iter.readObjectField()
+ switch field {
+ case decoder.fieldName1:
+ decoder.fieldDecoder1.decode(ptr, iter)
+ case decoder.fieldName2:
+ decoder.fieldDecoder2.decode(ptr, iter)
+ default:
+ iter.Skip()
+ }
+ for iter.nextToken() == ',' {
+ field = iter.readObjectField()
switch field {
case decoder.fieldName1:
decoder.fieldDecoder1.decode(ptr, iter)
@@ -263,7 +153,22 @@ type threeFieldsStructDecoder struct {
}
func (decoder *threeFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
- for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
+ if !iter.readObjectStart() {
+ return
+ }
+ field := iter.readObjectField()
+ switch field {
+ case decoder.fieldName1:
+ decoder.fieldDecoder1.decode(ptr, iter)
+ case decoder.fieldName2:
+ decoder.fieldDecoder2.decode(ptr, iter)
+ case decoder.fieldName3:
+ decoder.fieldDecoder3.decode(ptr, iter)
+ default:
+ iter.Skip()
+ }
+ for iter.nextToken() == ',' {
+ field = iter.readObjectField()
switch field {
case decoder.fieldName1:
decoder.fieldDecoder1.decode(ptr, iter)
@@ -309,8 +214,8 @@ func (decoder *fourFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterato
default:
iter.Skip()
}
- for iter.nextToken() != ',' {
- field := iter.readObjectField()
+ for iter.nextToken() == ',' {
+ field = iter.readObjectField()
switch field {
case decoder.fieldName1:
decoder.fieldDecoder1.decode(ptr, iter)
@@ -391,30 +296,30 @@ func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
return
}
offset := uintptr(0)
- decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
+ decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
if !iter.ReadArray() {
slice.Len = 1
return
}
offset += decoder.elemType.Size()
- decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
+ decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
if !iter.ReadArray() {
slice.Len = 2
return
}
offset += decoder.elemType.Size()
- decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
+ decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
if !iter.ReadArray() {
slice.Len = 3
return
}
offset += decoder.elemType.Size()
- decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
+ decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
slice.Len = 4
for iter.ReadArray() {
growOne(slice, decoder.sliceType, decoder.elemType)
offset += decoder.elemType.Size()
- decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
+ decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
}
}
Oops, something went wrong.

0 comments on commit 101dfdb

Please sign in to comment.