Bump github.com/gookit/config/v2 from 2.2.3 to 2.2.4
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.3 to 2.2.4. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.3...v2.2.4) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
30784affc4
commit
0df009eae0
+64
@@ -0,0 +1,64 @@
|
||||
package jsonutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
+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
|
||||
}
|
||||
*/
|
||||
+37
-48
@@ -4,7 +4,6 @@ package jsonutil
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -13,7 +12,7 @@ import (
|
||||
|
||||
// WriteFile write data to JSON file
|
||||
func WriteFile(filePath string, data any) error {
|
||||
jsonBytes, err := Encode(data)
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -46,71 +45,35 @@ func Pretty(v any) (string, error) {
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// Encode data to json bytes.
|
||||
func Encode(v any) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
// EncodePretty encode pretty JSON data to json bytes.
|
||||
func EncodePretty(v any) ([]byte, error) {
|
||||
return json.MarshalIndent(v, "", " ")
|
||||
}
|
||||
|
||||
// EncodeToWriter encode data 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
|
||||
// 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 buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// Decode json bytes to data ptr.
|
||||
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)
|
||||
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 := Encode(src)
|
||||
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.Unmarshal)
|
||||
// IsJSON check if the string is valid JSON. (Note: uses json.Valid)
|
||||
func IsJSON(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
var js json.RawMessage
|
||||
return json.Unmarshal([]byte(s), &js) == nil
|
||||
return json.Valid([]byte(s))
|
||||
}
|
||||
|
||||
// IsJSONFast simple and fast check input string is valid JSON.
|
||||
// IsJSONFast simple and fast check input is valid JSON array or object.
|
||||
func IsJSONFast(s string) bool {
|
||||
ln := len(s)
|
||||
if ln < 2 {
|
||||
@@ -129,6 +92,32 @@ func IsJSONFast(s string) bool {
|
||||
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*)`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user