You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
4.5 KiB
115 lines
4.5 KiB
// ------------------------------------------------------------------------
|
|
// Project atila
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name builderMap.go
|
|
// Created by DEV
|
|
// Modified 06/02/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package pkjson
|
|
|
|
import (
|
|
"apacks"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
func (p *JSONPacker) KeyValueAny (key any, value any, flags apacks.Flags) {
|
|
p.addKeyValue(key, func(){ p.PackAny(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueRaw (key any,value []byte, flags apacks.Flags) {
|
|
p.addKeyValue(key, func(){ p.PackRaw(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueBinary (key any, value []byte, flags apacks.Flags) {
|
|
p.addKeyValue(key, func(){ p.PackBinary(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueString (key any, value string, flags apacks.Flags) {
|
|
p.addKeyValue(key, func(){ p.PackString(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueBool (key any, value bool, flags apacks.Flags) {
|
|
p.addKeyValue(key, func(){ p.PackBool(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueInt8 (key any, value int8 , flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueInt16 (key any, value int16, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueInt32 (key any, value int32, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueInt64 (key any, value int64, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackInt64(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueUInt8 (key any, value uint8 , flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueUInt16 (key any, value uint16, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueUInt32 (key any, value uint32, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
|
|
func (p *JSONPacker) KeyValueUInt64 (key any, value uint64, flags apacks.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(value,flags)}) }
|
|
|
|
func (p *JSONPacker) KeyValueFloat32(key any, value float32, flags apacks.Flags){
|
|
p.addKeyValue(key,func(){ p.PackFloat32(value, flags) }) }
|
|
|
|
func (p *JSONPacker) KeyValueFloat64(key any, value float64, flags apacks.Flags){
|
|
p.addKeyValue(key,func(){ p.PackFloat64(value, flags) }) }
|
|
|
|
func (p *JSONPacker) KeyValueDate (key any, value time.Time, flags apacks.Flags){
|
|
p.addKeyValue(key,func(){ p.PackDate(value, flags) }) }
|
|
|
|
|
|
|
|
func (p *JSONPacker) KeyValueList (key any, builder func(apacks.ListPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
p.addKeyValue(key,func(){ p.PackList(builder, flags) }) }
|
|
|
|
|
|
func (p *JSONPacker) KeyValueMap (key any, builder func(apacks.MapPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
p.addKeyValue(key,func(){ p.PackMap(builder, flags) }) }
|
|
|
|
|
|
func (p *JSONPacker) KeyValueObject (key any, builder func(apacks.ObjectPackBuilder, apacks.Flags), flags apacks.Flags) {
|
|
p.addKeyValue(key,func(){ p.PackObject(builder, flags) }) }
|
|
|
|
|
|
func (p *JSONPacker) addKeyValue(key any, caller func()) {
|
|
if p.state.Count() > 0 {
|
|
p.state.WriteByte(',')
|
|
}
|
|
p.packStringifyKey(key)
|
|
if p.state.Error != nil {
|
|
return
|
|
}
|
|
p.state.WriteByte(':')
|
|
caller()
|
|
p.state.Inc()
|
|
|
|
}
|
|
|
|
|
|
func (p *JSONPacker) packStringifyKey(key any) {
|
|
switch v := key.(type) {
|
|
case string : p.PackString(v, apacks.FlagScapeHTML)
|
|
case float32: p.PackFloat32(v, apacks.FlagScapeHTML|apacks.FlagQuote)
|
|
case float64: p.PackFloat64(v, apacks.FlagScapeHTML|apacks.FlagQuote)
|
|
default:
|
|
me := &rMapEntry{
|
|
v : reflect.ValueOf(key),
|
|
}
|
|
fn := stringifyRKey(me.v.Type())
|
|
if fn == nil {
|
|
p.state.SetError(apacks.G.ErrInvalidKey)
|
|
return
|
|
}
|
|
if er := fn(me,me.v); er != nil {
|
|
p.state.SetError(er)
|
|
return
|
|
}
|
|
p.PackString(me.k,me.f)
|
|
}
|
|
} |