Initial QSfera import
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
package jsonutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// MustString encode data to json string, will panic on error
|
||||
func MustString(v any) string {
|
||||
bs, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
// Encode data to json bytes. alias of json.Marshal
|
||||
func Encode(v any) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
// EncodePretty encode data to pretty JSON bytes.
|
||||
func EncodePretty(v any) ([]byte, error) {
|
||||
return json.MarshalIndent(v, "", " ")
|
||||
}
|
||||
|
||||
// EncodeString encode data to JSON string.
|
||||
func EncodeString(v any) (string, error) {
|
||||
bs, err := json.MarshalIndent(v, "", " ")
|
||||
return string(bs), err
|
||||
}
|
||||
|
||||
// EncodeToWriter encode data to json and write to writer.
|
||||
func EncodeToWriter(v any, w io.Writer) error {
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
// EncodeUnescapeHTML data to json bytes. will close escape HTML
|
||||
func EncodeUnescapeHTML(v any) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
|
||||
if err := enc.Encode(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// Decode json bytes to data ptr. alias of json.Unmarshal
|
||||
func Decode(bts []byte, ptr any) error {
|
||||
return json.Unmarshal(bts, ptr)
|
||||
}
|
||||
|
||||
// DecodeString json string to data ptr.
|
||||
func DecodeString(str string, ptr any) error {
|
||||
return json.Unmarshal([]byte(str), ptr)
|
||||
}
|
||||
|
||||
// DecodeReader decode JSON from io reader.
|
||||
func DecodeReader(r io.Reader, ptr any) error {
|
||||
return json.NewDecoder(r).Decode(ptr)
|
||||
}
|
||||
|
||||
// DecodeFile decode JSON from file, bind data to ptr.
|
||||
func DecodeFile(file string, ptr any) error {
|
||||
bs, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(bs, ptr)
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package jsonutil
|
||||
|
||||
/*
|
||||
TODO json build
|
||||
type JsonBuilder struct {
|
||||
Indent string
|
||||
// mu sync.Mutex
|
||||
// cfg *CConfig
|
||||
buf bytes.Buffer
|
||||
out io.Writer
|
||||
}
|
||||
|
||||
// AddField add field to json
|
||||
func (b *JsonBuilder) AddField(key string, value any) *JsonBuilder {
|
||||
b.buf.WriteString(`,"`)
|
||||
b.buf.WriteString(key)
|
||||
b.buf.WriteString(`":`)
|
||||
b.encode(value)
|
||||
return b
|
||||
}
|
||||
*/
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
// Package jsonutil provide some util functions for quick operate JSON data
|
||||
package jsonutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/scanner"
|
||||
)
|
||||
|
||||
// WriteFile write data to JSON file
|
||||
func WriteFile(filePath string, data any) error {
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filePath, jsonBytes, 0664)
|
||||
}
|
||||
|
||||
// WritePretty write pretty data to JSON file
|
||||
func WritePretty(filePath string, data any) error {
|
||||
bs, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filePath, bs, 0664)
|
||||
}
|
||||
|
||||
// ReadFile Read JSON file data
|
||||
func ReadFile(filePath string, v any) error {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
return json.NewDecoder(file).Decode(v)
|
||||
}
|
||||
|
||||
// Pretty JSON string and return
|
||||
func Pretty(v any) (string, error) {
|
||||
out, err := json.MarshalIndent(v, "", " ")
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// MustPretty data to JSON string, will panic on error
|
||||
func MustPretty(v any) string {
|
||||
out, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// Mapping src data(map,struct) to dst struct use json tags.
|
||||
//
|
||||
// On src, dst both is struct, equivalent to merging two structures (src should be a subset of dsc)
|
||||
func Mapping(src, dst any) error {
|
||||
bts, err := json.Marshal(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Decode(bts, dst)
|
||||
}
|
||||
|
||||
// IsJSON check if the string is valid JSON. (Note: uses json.Valid)
|
||||
func IsJSON(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
return json.Valid([]byte(s))
|
||||
}
|
||||
|
||||
// IsJSONFast simple and fast check input is valid JSON array or object.
|
||||
func IsJSONFast(s string) bool {
|
||||
ln := len(s)
|
||||
if ln < 2 {
|
||||
return false
|
||||
}
|
||||
if ln == 2 {
|
||||
return s == "{}" || s == "[]"
|
||||
}
|
||||
|
||||
// object
|
||||
if s[0] == '{' {
|
||||
return s[ln-1] == '}' && s[1] == '"'
|
||||
}
|
||||
|
||||
// array
|
||||
return s[0] == '[' && s[ln-1] == ']'
|
||||
}
|
||||
|
||||
// IsArray check if the string is valid JSON array.
|
||||
func IsArray(s string) bool {
|
||||
ln := len(s)
|
||||
if ln < 2 {
|
||||
return false
|
||||
}
|
||||
return s[0] == '[' && s[ln-1] == ']'
|
||||
}
|
||||
|
||||
// IsObject check if the string is valid JSON object.
|
||||
func IsObject(s string) bool {
|
||||
ln := len(s)
|
||||
if ln < 2 {
|
||||
return false
|
||||
}
|
||||
if ln == 2 {
|
||||
return s == "{}"
|
||||
}
|
||||
|
||||
// object
|
||||
if s[0] == '{' {
|
||||
return s[ln-1] == '}' && s[1] == '"'
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// `(?s:` enable match multi line
|
||||
var jsonMLComments = regexp.MustCompile(`(?s:/\*.*?\*/\s*)`)
|
||||
|
||||
// StripComments strip comments for a JSON string
|
||||
func StripComments(src string) string {
|
||||
// multi line comments
|
||||
if strings.Contains(src, "/*") {
|
||||
src = jsonMLComments.ReplaceAllString(src, "")
|
||||
}
|
||||
|
||||
// single line comments
|
||||
if !strings.Contains(src, "//") {
|
||||
return strings.TrimSpace(src)
|
||||
}
|
||||
|
||||
// strip inline comments
|
||||
var s scanner.Scanner
|
||||
|
||||
s.Init(strings.NewReader(src))
|
||||
s.Filename = "comments"
|
||||
s.Mode ^= scanner.SkipComments // don't skip comments
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
|
||||
txt := s.TokenText()
|
||||
if !strings.HasPrefix(txt, "//") && !strings.HasPrefix(txt, "/*") {
|
||||
buf.WriteString(txt)
|
||||
// } else {
|
||||
// fmt.Printf("%s: %s\n", s.Position, txt)
|
||||
}
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
Reference in New Issue
Block a user