mirror of
https://github.com/therootcompany/golib.git
synced 2025-10-07 01:28:19 +00:00
ref(colorjson): modernize with any, b.Loop, and other minor enhancements
This commit is contained in:
parent
df73764148
commit
24df99adfa
@ -39,7 +39,7 @@ str := `{
|
|||||||
}`
|
}`
|
||||||
|
|
||||||
// Create an intersting JSON object to marshal in a pretty format
|
// Create an intersting JSON object to marshal in a pretty format
|
||||||
var obj map[string]interface{}
|
var obj map[string]any
|
||||||
json.Unmarshal([]byte(str), &obj)
|
json.Unmarshal([]byte(str), &obj)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -3,37 +3,30 @@ package colorjson_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/therootcompany/golib/colorjson"
|
|
||||||
"github.com/hokaccha/go-prettyjson"
|
"github.com/hokaccha/go-prettyjson"
|
||||||
|
"github.com/therootcompany/golib/colorjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func benchmarkMarshall(i int, b *testing.B) {
|
func BenchmarkMarshall(b *testing.B) {
|
||||||
simpleMap := make(map[string]interface{})
|
simpleMap := make(map[string]any)
|
||||||
simpleMap["a"] = 1
|
simpleMap["a"] = 1
|
||||||
simpleMap["b"] = "bee"
|
simpleMap["b"] = "bee"
|
||||||
simpleMap["c"] = [3]float64{1, 2, 3}
|
simpleMap["c"] = [3]float64{1, 2, 3}
|
||||||
simpleMap["d"] = [3]string{"one", "two", "three"}
|
simpleMap["d"] = [3]string{"one", "two", "three"}
|
||||||
|
|
||||||
// run the Fib function b.N times
|
for b.Loop() {
|
||||||
for n := 0; n < b.N; n++ {
|
_, _ = colorjson.Marshal(simpleMap)
|
||||||
colorjson.Marshal(simpleMap)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkPrettyJSON(i int, b *testing.B) {
|
func BenchmarkPrettyJSON(b *testing.B) {
|
||||||
simpleMap := make(map[string]interface{})
|
simpleMap := make(map[string]any)
|
||||||
simpleMap["a"] = 1
|
simpleMap["a"] = 1
|
||||||
simpleMap["b"] = "bee"
|
simpleMap["b"] = "bee"
|
||||||
simpleMap["c"] = [3]float64{1, 2, 3}
|
simpleMap["c"] = [3]float64{1, 2, 3}
|
||||||
simpleMap["d"] = [3]string{"one", "two", "three"}
|
simpleMap["d"] = [3]string{"one", "two", "three"}
|
||||||
|
|
||||||
// run the Fib function b.N times
|
for b.Loop() {
|
||||||
for n := 0; n < b.N; n++ {
|
_, _ = prettyjson.Marshal(simpleMap)
|
||||||
prettyjson.Marshal(simpleMap)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkMarshall(b *testing.B) { benchmarkMarshall(100, b) }
|
|
||||||
func BenchmarkMarshall1k(b *testing.B) { benchmarkMarshall(1000, b) }
|
|
||||||
func BenchmarkPrettyJSON(b *testing.B) { benchmarkPrettyJSON(100, b) }
|
|
||||||
func BenchmarkPrettyJSON1k(b *testing.B) { benchmarkPrettyJSON(1000, b) }
|
|
||||||
|
@ -67,13 +67,13 @@ func (f *Formatter) writeObjSep(buf *bytes.Buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) Marshal(jsonObj interface{}) ([]byte, error) {
|
func (f *Formatter) Marshal(jsonObj any) ([]byte, error) {
|
||||||
buffer := bytes.Buffer{}
|
buffer := bytes.Buffer{}
|
||||||
f.marshalValue(jsonObj, &buffer, initialDepth)
|
f.marshalValue(jsonObj, &buffer, initialDepth)
|
||||||
return buffer.Bytes(), nil
|
return buffer.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) marshalMap(m map[string]interface{}, buf *bytes.Buffer, depth int) {
|
func (f *Formatter) marshalMap(m map[string]any, buf *bytes.Buffer, depth int) {
|
||||||
remaining := len(m)
|
remaining := len(m)
|
||||||
|
|
||||||
if remaining == 0 {
|
if remaining == 0 {
|
||||||
@ -109,7 +109,7 @@ func (f *Formatter) marshalMap(m map[string]interface{}, buf *bytes.Buffer, dept
|
|||||||
buf.WriteString(endMap)
|
buf.WriteString(endMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) marshalArray(a []interface{}, buf *bytes.Buffer, depth int) {
|
func (f *Formatter) marshalArray(a []any, buf *bytes.Buffer, depth int) {
|
||||||
if len(a) == 0 {
|
if len(a) == 0 {
|
||||||
buf.WriteString(emptyArray)
|
buf.WriteString(emptyArray)
|
||||||
return
|
return
|
||||||
@ -130,11 +130,11 @@ func (f *Formatter) marshalArray(a []interface{}, buf *bytes.Buffer, depth int)
|
|||||||
buf.WriteString(endArray)
|
buf.WriteString(endArray)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Formatter) marshalValue(val interface{}, buf *bytes.Buffer, depth int) {
|
func (f *Formatter) marshalValue(val any, buf *bytes.Buffer, depth int) {
|
||||||
switch v := val.(type) {
|
switch v := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
f.marshalMap(v, buf, depth)
|
f.marshalMap(v, buf, depth)
|
||||||
case []interface{}:
|
case []any:
|
||||||
f.marshalArray(v, buf, depth)
|
f.marshalArray(v, buf, depth)
|
||||||
case string:
|
case string:
|
||||||
f.marshalString(v, buf)
|
f.marshalString(v, buf)
|
||||||
@ -163,6 +163,6 @@ func (f *Formatter) marshalString(str string, buf *bytes.Buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Marshal JSON data with default options
|
// Marshal JSON data with default options
|
||||||
func Marshal(jsonObj interface{}) ([]byte, error) {
|
func Marshal(jsonObj any) ([]byte, error) {
|
||||||
return NewFormatter().Marshal(jsonObj)
|
return NewFormatter().Marshal(jsonObj)
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,8 @@ func main() {
|
|||||||
"obj": { "a": 1, "b": 2 }
|
"obj": { "a": 1, "b": 2 }
|
||||||
}`
|
}`
|
||||||
|
|
||||||
var obj map[string]interface{}
|
var obj map[string]any
|
||||||
json.Unmarshal([]byte(str), &obj)
|
_ = json.Unmarshal([]byte(str), &obj)
|
||||||
|
|
||||||
// Make a custom formatter with indent set
|
// Make a custom formatter with indent set
|
||||||
f := colorjson.NewFormatter()
|
f := colorjson.NewFormatter()
|
||||||
|
@ -17,8 +17,8 @@ func main() {
|
|||||||
"obj": { "a": 1, "b": 2 }
|
"obj": { "a": 1, "b": 2 }
|
||||||
}`
|
}`
|
||||||
|
|
||||||
var obj map[string]interface{}
|
var obj map[string]any
|
||||||
json.Unmarshal([]byte(str), &obj)
|
_ = json.Unmarshal([]byte(str), &obj)
|
||||||
|
|
||||||
// Marshall the Colorized JSON
|
// Marshall the Colorized JSON
|
||||||
s, _ := colorjson.Marshal(obj)
|
s, _ := colorjson.Marshal(obj)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user