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.
193 lines
3.9 KiB
193 lines
3.9 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name writer.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 (
|
|
"cdcs/base32"
|
|
"cdcs/base64"
|
|
"math"
|
|
"mems"
|
|
"strconv"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
|
|
func WriteString(b *mems.ABuffer, value string, escapeHTML bool ) {
|
|
if len(value) == 0 {
|
|
b.Write2Byte('"', '"')
|
|
return
|
|
}
|
|
|
|
b.WriteByte('"')
|
|
start := 0
|
|
for i := 0; i < len(value); {
|
|
if ch := value[i]; ch < utf8.RuneSelf {
|
|
|
|
if !G.JSONChars[ch].IsHtmlUnsafe() || (!escapeHTML && !G.JSONChars[ch].IsUnsafe()) {
|
|
i++
|
|
continue
|
|
}
|
|
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteByte('\\')
|
|
switch ch {
|
|
case '\\', '"':
|
|
b.WriteByte(ch)
|
|
case '\n':
|
|
b.WriteByte('n')
|
|
case '\r':
|
|
b.WriteByte('r')
|
|
case '\t':
|
|
b.WriteByte('t')
|
|
default:
|
|
b.WriteString(`u00`)
|
|
b.Write2Byte(G.Hex[ch>>4], G.Hex[ch&0xF])
|
|
}
|
|
i++
|
|
start = i
|
|
continue
|
|
}
|
|
c, size := utf8.DecodeRuneInString(value[i:])
|
|
if c == utf8.RuneError && size == 1 {
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteString(`\ufffd`)
|
|
i += size
|
|
start = i
|
|
continue
|
|
}
|
|
|
|
if c == '\u2028' || c == '\u2029' {
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteString(`\u202`)
|
|
b.WriteByte(G.Hex[c&0xF])
|
|
i += size
|
|
start = i
|
|
continue
|
|
}
|
|
i += size
|
|
}
|
|
if start < len(value) {
|
|
b.WriteString(value[start:])
|
|
}
|
|
b.WriteByte('"')
|
|
}
|
|
|
|
|
|
|
|
func WriteUint (b *mems.ABuffer, value uint64 , quoted bool) {
|
|
if quoted {
|
|
b.WriteByte('"')
|
|
}
|
|
if value == 0 {
|
|
b.WriteByte('0')
|
|
} else {
|
|
b.WriteUIntStr(value, 10)
|
|
}
|
|
if quoted {
|
|
b.WriteByte('"')
|
|
}
|
|
}
|
|
|
|
|
|
func WriteBool (b *mems.ABuffer, value bool , quoted bool) {
|
|
if value {
|
|
b.WOQ(quoted,'"', G.TrueValue)
|
|
} else {
|
|
b.WOQ(quoted,'"', G.FalseValue)
|
|
}
|
|
}
|
|
|
|
|
|
func WriteInt (b *mems.ABuffer, value int64 , quoted bool) {
|
|
if quoted {
|
|
b.WriteByte('"')
|
|
}
|
|
if value == 0 {
|
|
b.WriteByte('0')
|
|
} else {
|
|
b.WriteIntStr(value, 10)
|
|
}
|
|
if quoted {
|
|
b.WriteByte('"')
|
|
}
|
|
}
|
|
|
|
|
|
func WriteFloat (b *mems.ABuffer, f float64, bits int, quoted bool) {
|
|
var t [64]byte
|
|
if math.IsInf(f, 0) || math.IsNaN(f) {
|
|
_,_ = b.Write(G.FloatNaN)
|
|
return
|
|
}
|
|
|
|
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]
|
|
}
|
|
}
|
|
|
|
b.WOQ(quoted,'"',bf)
|
|
}
|
|
|
|
|
|
func WriteDate(b *mems.ABuffer, value time.Time, layout string, quote bool) {
|
|
if layout == "" {
|
|
layout = time.RFC3339
|
|
}
|
|
if quote {
|
|
b.WriteByte('"')
|
|
}
|
|
b.WriteDateStr(value,layout)
|
|
if quote {
|
|
b.WriteByte('"')
|
|
}
|
|
}
|
|
|
|
|
|
func WriteBase64(b *mems.ABuffer, value []byte) (er error) {
|
|
return base64.Write(b,value,G.NullValue,G.EmptyValue,true,'"') }
|
|
|
|
|
|
func WriteBase32(b *mems.ABuffer, value []byte) (er error) {
|
|
return base32.Write(b,value,G.NullValue,G.EmptyValue,true,'"') } |