// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name gogo.go // Created by DEV // Modified 23/01/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 pjson import ( "activething.com/go/gogo/core/opts" "activething.com/go/gogo/core/results" "activething.com/go/gogo/packs" "encoding" "encoding/json" "reflect" "sync" "unicode/utf8" ) const ( jsonTag = "json" maxCyclesAfter = 100 defaultFlags = packs.Flags(0) ) var ( G = struct { hex string jsonChars [utf8.RuneSelf]jsonCharAtt nullValue []byte zeroValue []byte trueValue []byte falseValue []byte floatNaN []byte floatInfPos []byte floatInfNeg []byte packersCache sync.Map keyersCache sync.Map fieldsCache sync.Map jsonMarshalerType reflect.Type textMarshalerType reflect.Type packMarshalerType reflect.Type }{ hex: "0123456789abcdef", jsonChars: initJSONChars(), nullValue: []byte{'n', 'u', 'l', 'l'}, zeroValue: []byte{'0'}, trueValue: []byte{'"','t', 'r', 'u', 'e','"'}, falseValue: []byte{'"','f', 'a', 'l', 's', 'e','"'}, floatNaN: []byte{'"', 'N', 'a', 'N', '"'}, floatInfPos: []byte{'"', 'I', 'n', 'f', '"'}, floatInfNeg: []byte{'"', '-', 'I', 'n', 'f', '"'}, jsonMarshalerType: reflect.TypeOf((*json.Marshaler)(nil)).Elem(), textMarshalerType: reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem(), packMarshalerType: reflect.TypeOf((*packs.Packer)(nil)).Elem(), } ) func initJSONChars() [utf8.RuneSelf]jsonCharAtt { var tb = [utf8.RuneSelf]jsonCharAtt{ '"': jsonCharAttAllUnsafe, '&': jsonCharAttHtmlUnsafe, '<': jsonCharAttHtmlUnsafe, '>': jsonCharAttHtmlUnsafe, '\\': jsonCharAttAllUnsafe, } for i := 0; i <= 31; i++ { tb[i] = jsonCharAttAllUnsafe } return tb } func Pack (value any, options ... opts.OptionFnc[Packer]) (res results.Bytes) { p := NewPacker(options ...) p.Pack(value,defaultFlags) if p.Error() != nil { res = results.DoBytesError(p.Error()) } else { res = results.DoBytesSuccess(p.Packet()) } p.Release() return res }