Skip to content

Commit

Permalink
Preserve order of input json
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyu003 committed Dec 27, 2022
1 parent 99d282b commit ef78637
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/vmihailenco/msgpack/v5 v5.3.5
github.com/yuin/goldmark v1.5.3
gitlab.com/abhimanyusharma003/go-ordered-json v0.0.0-20200508150302-7ef32eef8ead
golang.org/x/crypto v0.4.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/abhimanyusharma003/go-ordered-json v0.0.0-20200508150302-7ef32eef8ead h1:JzI8XDh2gVmJqe5n+F2MHhxGpL9efc4ZgBjXpY6fDrs=
gitlab.com/abhimanyusharma003/go-ordered-json v0.0.0-20200508150302-7ef32eef8ead/go.mod h1:rzwdkxQuwcyF+elCTRk7kbXOI7Bj/UkmDDIhjPOrvdw=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
5 changes: 3 additions & 2 deletions processors/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ghodss/yaml"
"github.com/vmihailenco/msgpack/v5"
"gitlab.com/abhimanyusharma003/go-ordered-json"
)

// FormatJSON format given string to a JSON with Indent.
Expand All @@ -23,8 +24,8 @@ func (p FormatJSON) Alias() []string {
// unmarshalJson converts given bytes to json.RawMessage
// it checks if input is of type array or non array.
func unmarshalJSON(data []byte) (any, error) {
var nonArray map[string]*json.RawMessage
var arrayBased []map[string]*json.RawMessage
nonArray := ordered.NewOrderedMap()
arrayBased := make([]ordered.OrderedMap, 0)
err := json.Unmarshal(data, &nonArray)
if err == nil {
return nonArray, nil
Expand Down
66 changes: 66 additions & 0 deletions processors/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,41 @@ func TestJSON_Transform(t *testing.T) {
want: `{"name":"sttr"}`,
wantErr: false,
},
{
name: "Should preserver order of object input",
args: args{data: []byte(`{"c":"c","b":"b","a":"a"}`)},
want: `{"c":"c","b":"b","a":"a"}`,
wantErr: false,
},
{
name: "Should preserver order of array input",
args: args{data: []byte(`[{"c":"c","b":"b","a":"a"}]`)},
want: `[{"c":"c","b":"b","a":"a"}]`,
wantErr: false,
},
{
name: "Should preserver order of array input and indent on flag",
args: args{
data: []byte(`[{"c":"c","b":"b","a":"a"}]`),
f: []Flag{
{
Name: "indent",
Short: "i",
Desc: "Indent the output (prettyprint)",
Value: true,
Type: FlagBool,
},
},
},
want: `[
{
"c": "c",
"b": "b",
"a": "a"
}
]`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -381,6 +416,29 @@ func TestJSONUnescape_Transform(t *testing.T) {
want: ``,
wantErr: true,
},
{
name: "Should preserver order of array input and indent on flag",
args: args{
data: []byte(`[{\"c\":\"c\",\"b\":\"b\",\"a\":\"a\"}]`),
f: []Flag{
{
Name: "indent",
Short: "i",
Desc: "Indent the output (prettyprint)",
Value: true,
Type: FlagBool,
},
},
},
want: `[
{
"c": "c",
"b": "b",
"a": "a"
}
]`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -477,6 +535,14 @@ func TestJSONEscape_Transform(t *testing.T) {
want: ``,
wantErr: true,
},
{
name: "Should preserver order of array input and indent on flag",
args: args{
data: []byte(`[{"c":"c","b":"b","a":"a"}]`),
},
want: `[{\"c\":\"c\",\"b\":\"b\",\"a\":\"a\"}]`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ef78637

Please sign in to comment.