use indented json in ONM.String()

This commit is contained in:
ionutboangiu
2026-02-19 18:20:07 +02:00
committed by Dan Christian Bogos
parent 34ab081b33
commit 51943cb387
4 changed files with 70 additions and 5 deletions

View File

@@ -603,7 +603,19 @@ func TestExportRequestSetAsSliceDefaultOK(t *testing.T) {
val := &utils.DataLeaf{
Data: "cgrates.org",
}
exp := `{"Map":{"Tenant":{"Slice":[{"Value":{"Data":"cgrates.org"}}]}}}`
exp := `{
"Map": {
"Tenant": {
"Slice": [
{
"Value": {
"Data": "cgrates.org"
}
}
]
}
}
}`
if err := eeR.SetAsSlice(fullPath, val); err != nil {
t.Error(err)
@@ -734,7 +746,19 @@ func TestExportRequestAppendDefaultOK(t *testing.T) {
val := &utils.DataLeaf{
Data: "cgrates.org",
}
exp := `{"Map":{"Tenant":{"Slice":[{"Value":{"Data":"cgrates.org"}}]}}}`
exp := `{
"Map": {
"Tenant": {
"Slice": [
{
"Value": {
"Data": "cgrates.org"
}
}
]
}
}
}`
if err := eeR.Append(fullPath, val); err != nil {
t.Error(err)
@@ -919,7 +943,19 @@ func TestExportRequestComposeDefaultOK(t *testing.T) {
val := &utils.DataLeaf{
Data: "cgrates.org",
}
exp := `{"Map":{"Tenant":{"Slice":[{"Value":{"Data":"cgrates.org"}}]}}}`
exp := `{
"Map": {
"Tenant": {
"Slice": [
{
"Value": {
"Data": "cgrates.org"
}
}
]
}
}
}`
if err := eeR.Compose(fullPath, val); err != nil {
t.Error(err)

View File

@@ -1698,3 +1698,24 @@ func TestToUnescapedJSON(t *testing.T) {
t.Errorf("Expected %v, received %v", ToJSON(exp), ToJSON(rcv))
}
}
// go test ./utils/ -bench BenchmarkToJSON -benchmem
func BenchmarkToJSON(b *testing.B) {
v := map[string]any{
"Result-Code": "2001",
"Granted-Service-Unit": map[string]any{
"CC-Time": 300,
},
"Session-Id": "cgrates;1052943642;938",
}
b.Run("ToJSON", func(b *testing.B) {
for b.Loop() {
ToJSON(v)
}
})
b.Run("ToIJSON", func(b *testing.B) {
for b.Loop() {
ToIJSON(v)
}
})
}

View File

@@ -42,7 +42,7 @@ type OrderedNavigableMap struct {
// String returns the map as json string
func (onm *OrderedNavigableMap) String() string {
return ToJSON(onm.nm)
return ToIJSON(onm.nm)
}
// GetFirstElement returns the first element from the order

View File

@@ -876,7 +876,15 @@ func TestOrderedNavigableMapString(t *testing.T) {
},
},
}
exp := `{"Map":{"Node1":{"Value":{"Data":"value"}}}}`
exp := `{
"Map": {
"Node1": {
"Value": {
"Data": "value"
}
}
}
}`
rcv := onm.String()
if rcv != exp {
t.Errorf("Expected %v but received %v", exp, rcv)