|
|
|
|
@ -28,7 +28,7 @@ import (
|
|
|
|
|
"crypts"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errs"
|
|
|
|
|
"proteus"
|
|
|
|
|
"apacks"
|
|
|
|
|
"reflect"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
@ -38,7 +38,7 @@ type (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONPacker struct {
|
|
|
|
|
state *proteus.State
|
|
|
|
|
state *apacks.State
|
|
|
|
|
dirty bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -49,7 +49,7 @@ type (
|
|
|
|
|
|
|
|
|
|
func NewJSONPacker(options ...JSONPackerFnc) *JSONPacker {
|
|
|
|
|
pk := &JSONPacker{
|
|
|
|
|
state : proteus.NewState(),
|
|
|
|
|
state : apacks.NewState(),
|
|
|
|
|
}
|
|
|
|
|
for _,o := range options {
|
|
|
|
|
o(pk)
|
|
|
|
|
@ -59,12 +59,12 @@ func NewJSONPacker(options ...JSONPackerFnc) *JSONPacker {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) Pack (value any, flags proteus.Flags) {
|
|
|
|
|
func (p *JSONPacker) Pack (value any, flags apacks.Flags) {
|
|
|
|
|
if p.state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
}
|
|
|
|
|
if p.dirty {
|
|
|
|
|
panic(proteus.G.ErrInvalidState)
|
|
|
|
|
panic(apacks.G.ErrInvalidState)
|
|
|
|
|
}
|
|
|
|
|
p.dirty = true
|
|
|
|
|
p.PackAny(value,flags)
|
|
|
|
|
@ -86,14 +86,14 @@ func (p *JSONPacker) Reset () {
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) Release () {
|
|
|
|
|
p.dirty = false
|
|
|
|
|
proteus.ReleaseState(p.state)
|
|
|
|
|
apacks.ReleaseState(p.state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackAny (value any, flags proteus.Flags) {
|
|
|
|
|
func (p *JSONPacker) PackAny (value any, flags apacks.Flags) {
|
|
|
|
|
if p.state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
}
|
|
|
|
|
if value == nil {
|
|
|
|
|
p.state.W(cjson.G.NullValue)
|
|
|
|
|
@ -101,7 +101,7 @@ func (p *JSONPacker) PackAny (value any, flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch v := value.(type) {
|
|
|
|
|
case proteus.PackBuilder:
|
|
|
|
|
case apacks.PackBuilder:
|
|
|
|
|
p.state.SetError(v.BuildPack(p))
|
|
|
|
|
|
|
|
|
|
case json.Marshaler:
|
|
|
|
|
@ -166,45 +166,45 @@ func (p *JSONPacker) PackAny (value any, flags proteus.Flags) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackRaw (value []byte, flags proteus.Flags) { packRaw(p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackRaw (value []byte, flags apacks.Flags) { packRaw(p.state,value,flags) }
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackBinary (value []byte, flags proteus.Flags) { packBinary(p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackString (value string, flags proteus.Flags) { packString (p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackBinary (value []byte, flags apacks.Flags) { packBinary(p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackString (value string, flags apacks.Flags) { packString (p.state,value,flags) }
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackBool (value bool, flags proteus.Flags) { packBool(p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackBool (value bool, flags apacks.Flags) { packBool(p.state,value,flags) }
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackInt8 (value int8 , flags proteus.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt16 (value int16, flags proteus.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt32 (value int32, flags proteus.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt64 (value int64, flags proteus.Flags) { packInt(p.state,value,flags)}
|
|
|
|
|
func (p *JSONPacker) PackInt8 (value int8 , flags apacks.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt16 (value int16, flags apacks.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt32 (value int32, flags apacks.Flags) { packInt(p.state,int64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackInt64 (value int64, flags apacks.Flags) { packInt(p.state,value,flags)}
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackUInt8 (value uint8 , flags proteus.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt16 (value uint16, flags proteus.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt32 (value uint32, flags proteus.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt64 (value uint64, flags proteus.Flags) { packUInt(p.state, value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt8 (value uint8 , flags apacks.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt16 (value uint16, flags apacks.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt32 (value uint32, flags apacks.Flags) { packUInt(p.state,uint64(value),flags) }
|
|
|
|
|
func (p *JSONPacker) PackUInt64 (value uint64, flags apacks.Flags) { packUInt(p.state, value,flags) }
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackFloat32 (value float32, flags proteus.Flags) { packFloatString(p.state, float64(value), flags, 32) }
|
|
|
|
|
func (p *JSONPacker) PackFloat64 (value float64, flags proteus.Flags) { packFloatString(p.state, value, flags, 64) }
|
|
|
|
|
func (p *JSONPacker) PackFloat32 (value float32, flags apacks.Flags) { packFloatString(p.state, float64(value), flags, 32) }
|
|
|
|
|
func (p *JSONPacker) PackFloat64 (value float64, flags apacks.Flags) { packFloatString(p.state, value, flags, 64) }
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackDate (value time.Time, flags proteus.Flags) { packDate(p.state,value,flags) }
|
|
|
|
|
func (p *JSONPacker) PackDate (value time.Time, flags apacks.Flags) { packDate(p.state,value,flags) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackList (builder func(proteus.ListPackBuilder, proteus.Flags), flags proteus.Flags) {
|
|
|
|
|
p.packCompound('[',']',builder==nil,func(pk *JSONPacker, fl proteus.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
func (p *JSONPacker) PackList (builder func(apacks.ListPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
|
|
|
p.packCompound('[',']',builder==nil,func(pk *JSONPacker, fl apacks.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackMap (builder func(proteus.MapPackBuilder, proteus.Flags), flags proteus.Flags) {
|
|
|
|
|
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl proteus.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
func (p *JSONPacker) PackMap (builder func(apacks.MapPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
|
|
|
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl apacks.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) PackObject(builder func(proteus.ObjectPackBuilder, proteus.Flags), flags proteus.Flags) {
|
|
|
|
|
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl proteus.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
func (p *JSONPacker) PackObject(builder func(apacks.ObjectPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
|
|
|
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl apacks.Flags){ builder(pk,fl) },flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *JSONPacker) packCompound (opener, closer byte, isnil bool, buildFn func(*JSONPacker, proteus.Flags), flags proteus.Flags) {
|
|
|
|
|
func (p *JSONPacker) packCompound (opener, closer byte, isnil bool, buildFn func(*JSONPacker, apacks.Flags), flags apacks.Flags) {
|
|
|
|
|
if p.state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(p.state.Error))
|
|
|
|
|
}
|
|
|
|
|
if isnil {
|
|
|
|
|
p.state.W(cjson.G.NullValue)
|
|
|
|
|
@ -229,9 +229,9 @@ func (p *JSONPacker) packCompound (opener, closer byte, isnil bool, buildFn func
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packRaw (state *proteus.State, value []byte, flags proteus.Flags) {
|
|
|
|
|
func packRaw (state *apacks.State, value []byte, flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
if value == nil {
|
|
|
|
|
state.W(cjson.G.NullValue)
|
|
|
|
|
@ -244,9 +244,9 @@ func packRaw (state *proteus.State, value []byte, flags proteus.Flags) {
|
|
|
|
|
state.WOQ(flags.Quote(),'"',value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func packString (state *proteus.State, value string , flags proteus.Flags) {
|
|
|
|
|
func packString (state *apacks.State, value string , flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(value) == 0 {
|
|
|
|
|
@ -255,7 +255,7 @@ func packString (state *proteus.State, value string , flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if flags.Encrypted() {
|
|
|
|
|
packEncrypted(state, []byte(value), flags|proteus.FlagBinary64)
|
|
|
|
|
packEncrypted(state, []byte(value), flags|apacks.FlagBinary64)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -268,7 +268,7 @@ func packString (state *proteus.State, value string , flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packEncrypted (state *proteus.State, value []byte, flags proteus.Flags) {
|
|
|
|
|
func packEncrypted (state *apacks.State, value []byte, flags apacks.Flags) {
|
|
|
|
|
r := state.Encrypter.Encrypt(value)
|
|
|
|
|
if !r.Ok() {
|
|
|
|
|
state.SetError(r.Fail())
|
|
|
|
|
@ -276,16 +276,16 @@ func packEncrypted (state *proteus.State, value []byte, flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !flags.Binary() {
|
|
|
|
|
flags|= proteus.FlagBinary64
|
|
|
|
|
flags|= apacks.FlagBinary64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packBinary(state,value,flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packBinary (state *proteus.State, value []byte , flags proteus.Flags) {
|
|
|
|
|
func packBinary (state *apacks.State, value []byte , flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if flags.Bin64() {
|
|
|
|
|
@ -296,48 +296,48 @@ func packBinary (state *proteus.State, value []byte , flags proteus.Flags) {
|
|
|
|
|
state.SetError(cjson.WriteBase32(state.ABuffer,value))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
state.SetError(proteus.G.ErrUnsupportedType)
|
|
|
|
|
state.SetError(apacks.G.ErrUnsupportedType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packBool (state *proteus.State, value bool , flags proteus.Flags) {
|
|
|
|
|
func packBool (state *apacks.State, value bool , flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
cjson.WriteBool(state.ABuffer,value,flags.Quote())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packInt (state *proteus.State, value int64 , flags proteus.Flags) {
|
|
|
|
|
func packInt (state *apacks.State, value int64 , flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
cjson.WriteInt(state.ABuffer,value,flags.Quote())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packUInt (state *proteus.State, value uint64 , flags proteus.Flags) {
|
|
|
|
|
func packUInt (state *apacks.State, value uint64 , flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
cjson.WriteUint(state.ABuffer,value,flags.Quote())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func packFloat (state *proteus.State, value float64, flags proteus.Flags) {
|
|
|
|
|
func packFloat (state *apacks.State, value float64, flags apacks.Flags) {
|
|
|
|
|
packFloatString(state, value, flags, 64) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packFloatString (state *proteus.State, f float64, flags proteus.Flags, bits int) {
|
|
|
|
|
func packFloatString (state *apacks.State, f float64, flags apacks.Flags, bits int) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
cjson.WriteFloat(state.ABuffer,f,bits,flags.Quote())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packDate(state *proteus.State, value time.Time, flags proteus.Flags) {
|
|
|
|
|
func packDate(state *apacks.State, value time.Time, flags apacks.Flags) {
|
|
|
|
|
if state.Error != nil {
|
|
|
|
|
panic(proteus.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
panic(apacks.G.ErrInvalidState.WrapWithCause(state.Error))
|
|
|
|
|
}
|
|
|
|
|
cjson.WriteDate(state.ABuffer,value,flags.DateFormat(),true)
|
|
|
|
|
}
|
|
|
|
|
@ -347,7 +347,7 @@ func packDate(state *proteus.State, value time.Time, flags proteus.Flags) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packListString (state *proteus.State, list []string, flags proteus.Flags) {
|
|
|
|
|
func packListString (state *apacks.State, list []string, flags apacks.Flags) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -365,7 +365,7 @@ func packListString (state *proteus.State, list []string, flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packListBool (state *proteus.State, list []bool, flags proteus.Flags) {
|
|
|
|
|
func packListBool (state *apacks.State, list []bool, flags apacks.Flags) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -383,7 +383,7 @@ func packListBool (state *proteus.State, list []bool, flags proteus.Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packListInt [T int | int8 | int16 | int32 | int64](state *proteus.State, list []T, flags proteus.Flags) {
|
|
|
|
|
func packListInt [T int | int8 | int16 | int32 | int64](state *apacks.State, list []T, flags apacks.Flags) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -401,7 +401,7 @@ func packListInt [T int | int8 | int16 | int32 | int64](state *proteus.State, l
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *proteus.State, list []T, flags proteus.Flags) {
|
|
|
|
|
func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *apacks.State, list []T, flags apacks.Flags) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -419,7 +419,7 @@ func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *proteus.Sta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func packListFloat[T float32 | float64](state *proteus.State, list []T, flags proteus.Flags, bits int) {
|
|
|
|
|
func packListFloat[T float32 | float64](state *apacks.State, list []T, flags apacks.Flags, bits int) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -436,7 +436,7 @@ func packListFloat[T float32 | float64](state *proteus.State, list []T, flags pr
|
|
|
|
|
state.WriteByte(']')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func packListDate (state *proteus.State, list []time.Time, flags proteus.Flags) {
|
|
|
|
|
func packListDate (state *apacks.State, list []time.Time, flags apacks.Flags) {
|
|
|
|
|
if !checkList(state,list == nil, len(list) ==0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -454,7 +454,7 @@ func packListDate (state *proteus.State, list []time.Time, flags proteus.Flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkList (state *proteus.State, isNull, isEmpty bool) bool {
|
|
|
|
|
func checkList (state *apacks.State, isNull, isEmpty bool) bool {
|
|
|
|
|
if isNull {
|
|
|
|
|
state.Write2Byte('"','"')
|
|
|
|
|
return false
|