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.
111 lines
2.5 KiB
111 lines
2.5 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name active.go
|
|
// Created by DEV
|
|
// Modified 21/04/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 cjson
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
var (
|
|
|
|
G = struct {
|
|
Hex string
|
|
JSONChars [utf8.RuneSelf]CharAtt
|
|
|
|
NullValue []byte
|
|
EmptyValue []byte
|
|
ZeroValue []byte
|
|
TrueValue []byte
|
|
FalseValue []byte
|
|
|
|
FloatNaN []byte
|
|
FloatInfPos []byte
|
|
FloatInfNeg []byte
|
|
} {
|
|
|
|
Hex: "0123456789abcdef",
|
|
JSONChars: initJSONChars(),
|
|
|
|
NullValue: []byte{'n', 'u', 'l', 'l'},
|
|
ZeroValue: []byte{'0'},
|
|
EmptyValue: []byte{'"','"'},
|
|
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', '"'},
|
|
}
|
|
)
|
|
|
|
|
|
func initJSONChars() [utf8.RuneSelf]CharAtt {
|
|
var tb = [utf8.RuneSelf]CharAtt{
|
|
'"': CharAttAllUnsafe,
|
|
'&': CharAttHtmlUnsafe,
|
|
'<': CharAttHtmlUnsafe,
|
|
'>': CharAttHtmlUnsafe,
|
|
'\\': CharAttAllUnsafe,
|
|
}
|
|
for i := 0; i <= 31; i++ {
|
|
tb[i] = CharAttAllUnsafe
|
|
}
|
|
return tb
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func JSONFloat (f float64, bits int) string {
|
|
var t [64]byte
|
|
if math.IsInf(f, 0) || math.IsNaN(f) {
|
|
return string(G.FloatNaN)
|
|
}
|
|
|
|
abs := math.Abs(f)
|
|
fm := byte('f')
|
|
|
|
if abs != 0 {
|
|
if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
|
|
fm = 'e'
|
|
}
|
|
}
|
|
bf := t[:0]
|
|
bf = strconv.AppendFloat(bf, f, fm, -1, int(bits))
|
|
if fm == 'e' {
|
|
// clean up e-09 to e-9
|
|
n := len(bf)
|
|
if n >= 4 && bf[n-4] == 'e' && bf[n-3] == '-' && bf[n-2] == '0' {
|
|
bf[n-2] = bf[n-1]
|
|
bf = bf[:n-1]
|
|
}
|
|
}
|
|
return string(bf)
|
|
}
|
|
|