上传编解码库代码
This commit is contained in:
parent
906afe87d0
commit
c79db39d2b
@ -4,9 +4,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go_dreamfactory/lego/sys/codec/core"
|
"go_dreamfactory/lego/sys/codec/core"
|
||||||
"go_dreamfactory/lego/sys/codec/extractor"
|
|
||||||
"go_dreamfactory/lego/sys/codec/factory"
|
"go_dreamfactory/lego/sys/codec/factory"
|
||||||
"go_dreamfactory/lego/sys/codec/stream"
|
"go_dreamfactory/lego/sys/codec/render"
|
||||||
|
|
||||||
"github.com/modern-go/reflect2"
|
"github.com/modern-go/reflect2"
|
||||||
)
|
)
|
||||||
@ -18,12 +17,12 @@ func newSys(options core.Options) (sys *codec, err error) {
|
|||||||
encoderCache: new(sync.Map),
|
encoderCache: new(sync.Map),
|
||||||
streamPool: &sync.Pool{
|
streamPool: &sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return stream.NewStream(sys, 512)
|
return render.NewStream(sys, 512)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extraPool: &sync.Pool{
|
extraPool: &sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return extractor.NewExtractor(sys)
|
return render.NewExtractor(sys)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
45
lego/sys/codec/render/core.go
Normal file
45
lego/sys/codec/render/core.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package render
|
||||||
|
|
||||||
|
import "go_dreamfactory/lego/sys/codec/core"
|
||||||
|
|
||||||
|
const maxDepth = 10000
|
||||||
|
|
||||||
|
var hexDigits []byte
|
||||||
|
var valueTypes []core.ValueType
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
hexDigits = make([]byte, 256)
|
||||||
|
for i := 0; i < len(hexDigits); i++ {
|
||||||
|
hexDigits[i] = 255
|
||||||
|
}
|
||||||
|
for i := '0'; i <= '9'; i++ {
|
||||||
|
hexDigits[i] = byte(i - '0')
|
||||||
|
}
|
||||||
|
for i := 'a'; i <= 'f'; i++ {
|
||||||
|
hexDigits[i] = byte((i - 'a') + 10)
|
||||||
|
}
|
||||||
|
for i := 'A'; i <= 'F'; i++ {
|
||||||
|
hexDigits[i] = byte((i - 'A') + 10)
|
||||||
|
}
|
||||||
|
valueTypes = make([]core.ValueType, 256)
|
||||||
|
for i := 0; i < len(valueTypes); i++ {
|
||||||
|
valueTypes[i] = core.InvalidValue
|
||||||
|
}
|
||||||
|
valueTypes['"'] = core.StringValue
|
||||||
|
valueTypes['-'] = core.NumberValue
|
||||||
|
valueTypes['0'] = core.NumberValue
|
||||||
|
valueTypes['1'] = core.NumberValue
|
||||||
|
valueTypes['2'] = core.NumberValue
|
||||||
|
valueTypes['3'] = core.NumberValue
|
||||||
|
valueTypes['4'] = core.NumberValue
|
||||||
|
valueTypes['5'] = core.NumberValue
|
||||||
|
valueTypes['6'] = core.NumberValue
|
||||||
|
valueTypes['7'] = core.NumberValue
|
||||||
|
valueTypes['8'] = core.NumberValue
|
||||||
|
valueTypes['9'] = core.NumberValue
|
||||||
|
valueTypes['t'] = core.BoolValue
|
||||||
|
valueTypes['f'] = core.BoolValue
|
||||||
|
valueTypes['n'] = core.NilValue
|
||||||
|
valueTypes['['] = core.ArrayValue
|
||||||
|
valueTypes['{'] = core.ObjectValue
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package extractor
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -12,47 +12,6 @@ import (
|
|||||||
"github.com/modern-go/reflect2"
|
"github.com/modern-go/reflect2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxDepth = 10000
|
|
||||||
|
|
||||||
var hexDigits []byte
|
|
||||||
var valueTypes []core.ValueType
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
hexDigits = make([]byte, 256)
|
|
||||||
for i := 0; i < len(hexDigits); i++ {
|
|
||||||
hexDigits[i] = 255
|
|
||||||
}
|
|
||||||
for i := '0'; i <= '9'; i++ {
|
|
||||||
hexDigits[i] = byte(i - '0')
|
|
||||||
}
|
|
||||||
for i := 'a'; i <= 'f'; i++ {
|
|
||||||
hexDigits[i] = byte((i - 'a') + 10)
|
|
||||||
}
|
|
||||||
for i := 'A'; i <= 'F'; i++ {
|
|
||||||
hexDigits[i] = byte((i - 'A') + 10)
|
|
||||||
}
|
|
||||||
valueTypes = make([]core.ValueType, 256)
|
|
||||||
for i := 0; i < len(valueTypes); i++ {
|
|
||||||
valueTypes[i] = core.InvalidValue
|
|
||||||
}
|
|
||||||
valueTypes['"'] = core.StringValue
|
|
||||||
valueTypes['-'] = core.NumberValue
|
|
||||||
valueTypes['0'] = core.NumberValue
|
|
||||||
valueTypes['1'] = core.NumberValue
|
|
||||||
valueTypes['2'] = core.NumberValue
|
|
||||||
valueTypes['3'] = core.NumberValue
|
|
||||||
valueTypes['4'] = core.NumberValue
|
|
||||||
valueTypes['5'] = core.NumberValue
|
|
||||||
valueTypes['6'] = core.NumberValue
|
|
||||||
valueTypes['7'] = core.NumberValue
|
|
||||||
valueTypes['8'] = core.NumberValue
|
|
||||||
valueTypes['9'] = core.NumberValue
|
|
||||||
valueTypes['t'] = core.BoolValue
|
|
||||||
valueTypes['f'] = core.BoolValue
|
|
||||||
valueTypes['n'] = core.NilValue
|
|
||||||
valueTypes['['] = core.ArrayValue
|
|
||||||
valueTypes['{'] = core.ObjectValue
|
|
||||||
}
|
|
||||||
func NewExtractor(codec core.ICodec) *JsonExtractor {
|
func NewExtractor(codec core.ICodec) *JsonExtractor {
|
||||||
return &JsonExtractor{
|
return &JsonExtractor{
|
||||||
codec: codec,
|
codec: codec,
|
||||||
@ -145,7 +104,6 @@ func (this *JsonExtractor) ReadNil() (ret bool) {
|
|||||||
this.unreadByte()
|
this.unreadByte()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *JsonExtractor) ReadArrayStart() (ret bool) {
|
func (this *JsonExtractor) ReadArrayStart() (ret bool) {
|
||||||
c := this.nextToken()
|
c := this.nextToken()
|
||||||
if c == '[' {
|
if c == '[' {
|
||||||
@ -226,7 +184,6 @@ func (this *JsonExtractor) ReadKeyEnd() (ret bool) {
|
|||||||
this.reportError("ReadKeyEnd", `expect " but found `+string([]byte{c}))
|
this.reportError("ReadKeyEnd", `expect " but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *JsonExtractor) Skip() {
|
func (this *JsonExtractor) Skip() {
|
||||||
c := this.nextToken()
|
c := this.nextToken()
|
||||||
switch c {
|
switch c {
|
@ -1,4 +1,4 @@
|
|||||||
package stream
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go_dreamfactory/lego/sys/codec/core"
|
"go_dreamfactory/lego/sys/codec/core"
|
||||||
@ -143,7 +143,6 @@ func (this *JsonStream) WriteString(val string) {
|
|||||||
}
|
}
|
||||||
utils.WriteStringSlowPath(&this.buf, i, val, valLen)
|
utils.WriteStringSlowPath(&this.buf, i, val, valLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *JsonStream) WriteBytes(val []byte) {
|
func (this *JsonStream) WriteBytes(val []byte) {
|
||||||
this.buf = append(this.buf, val...)
|
this.buf = append(this.buf, val...)
|
||||||
}
|
}
|
@ -27,7 +27,7 @@ func Test_sys(t *testing.T) {
|
|||||||
if sys, err := codec.NewSys(); err != nil {
|
if sys, err := codec.NewSys(); err != nil {
|
||||||
fmt.Printf("gin init err:%v", err)
|
fmt.Printf("gin init err:%v", err)
|
||||||
} else {
|
} else {
|
||||||
d, err := sys.MarshalJson(&TestData{Name: "liwe1idao", Value: 10, Array: []interface{}{1, "dajiahao", &Test1Data{Name: "liwe1dao", Value: 123}}, Data: map[string]interface{}{"hah": 1, "asd": 999}})
|
d, err := sys.MarshalJson(&TestData{Name: "http://liwei1dao.com?asd=1&dd=1", Value: 10, Array: []interface{}{1, "dajiahao", &Test1Data{Name: "liwe1dao", Value: 123}}, Data: map[string]interface{}{"hah": 1, "asd": 999}})
|
||||||
fmt.Printf("codec Marshal d:%s err:%v", d, err)
|
fmt.Printf("codec Marshal d:%s err:%v", d, err)
|
||||||
data := &TestData{}
|
data := &TestData{}
|
||||||
err = sys.UnmarshalJson(d, data)
|
err = sys.UnmarshalJson(d, data)
|
||||||
|
Loading…
Reference in New Issue
Block a user