-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
245 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package orderedjson | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
orderedmap "github.com/wk8/go-ordered-map/v2" | ||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
type OrderedData struct { | ||
orderedMap *orderedmap.OrderedMap[string, OrderedData] | ||
array *[]OrderedData | ||
fallback *interface{} | ||
} | ||
|
||
func (orderedData *OrderedData) UnmarshalJSON(data []byte) error { | ||
orderedData.orderedMap = orderedmap.New[string, OrderedData]() | ||
err := json.Unmarshal(data, &orderedData.orderedMap) | ||
if err != nil { | ||
orderedData.orderedMap = nil | ||
orderedData.array = new([]OrderedData) | ||
err = json.Unmarshal(data, orderedData.array) | ||
} | ||
if err != nil { | ||
orderedData.array = nil | ||
orderedData.fallback = new(interface{}) | ||
err = json.Unmarshal(data, &orderedData.fallback) | ||
} | ||
return err | ||
} | ||
|
||
// TODO: remove once hack in printYaml is not needed anymore | ||
func (orderedData *OrderedData) GetMapOrNil() *orderedmap.OrderedMap[string, OrderedData] { | ||
return orderedData.orderedMap | ||
} | ||
|
||
func (orderedData OrderedData) MarshalJSON() ([]byte, error) { | ||
if orderedData.orderedMap != nil { | ||
return json.Marshal(orderedData.orderedMap) | ||
} else if orderedData.array != nil { | ||
return json.Marshal(orderedData.array) | ||
} else if orderedData.fallback != nil { | ||
return json.Marshal(orderedData.fallback) | ||
} else { | ||
return json.Marshal(nil) | ||
} | ||
} | ||
|
||
func (orderedData OrderedData) MarshalYAML() (interface{}, error) { | ||
if orderedData.orderedMap != nil { | ||
return orderedData.orderedMap, nil | ||
} else if orderedData.array != nil { | ||
return orderedData.array, nil | ||
} | ||
return orderedData.fallback, nil | ||
} | ||
|
||
func (orderedData *OrderedData) UnmarshalYAML(value *yaml.Node) error { | ||
panic("Not supported") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package orderedjson | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestOrderedRecursiveMap(t *testing.T) { | ||
testForJson(t, `{"name":"John","age":30,"city":"New York","children":[{"name":"Alice","age":5},{"name":"Bob","age":7}],"parent":{"name":"Jane","age":60,"city":"New York"}}`) | ||
testForJson(t, `"yo"`) | ||
testForJson(t, `true`) | ||
testForJson(t, `false`) | ||
testForJson(t, `42`) | ||
testForJson(t, `42.2`) | ||
testForJson(t, `[]`) | ||
testForJson(t, `{}`) | ||
testForJson(t, `{"z":{"x":{"v":{}}},"y":{"u":{"t":"p"}}}`) | ||
testForJson(t, `[[[[]]]]`) | ||
testForJson(t, `[{"z":42},{"b":{},"y":41,"a":[[{"z":42},{"b":{},"y":41,"a":[[{"z":42},{"b":{},"y":41,"a":[[{"z":42},{"b":{},"y":41,"a":[]}]]}]]}]]}]`) | ||
} | ||
|
||
func testForJson(t *testing.T, originalJSON string) { | ||
// Unmarshal the JSON into an OrderedRecursiveMap | ||
var omap OrderedData | ||
err := json.Unmarshal([]byte(originalJSON), &omap) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal JSON: %+v", err) | ||
} | ||
|
||
fmt.Printf("%v\n", omap) | ||
// Marshal the OrderedRecursiveMap back into JSON | ||
marshaledJSON, err := json.Marshal(&omap) | ||
if err != nil { | ||
t.Fatalf("Failed to marshal OrderedRecursiveMap: %v", err) | ||
} | ||
|
||
// Check if the original JSON and the marshaled JSON are the same | ||
if originalJSON != string(marshaledJSON) { | ||
t.Errorf("Original JSON and marshaled JSON do not match. Original: %s, Marshaled: %s", originalJSON, string(marshaledJSON)) | ||
} | ||
} | ||
|
||
func TestYamlMarshallingKeepOrderTo(t *testing.T) { | ||
// Unmarshal the JSON into an OrderedRecursiveMap | ||
var omap OrderedData | ||
err := json.Unmarshal([]byte(`{"name":"John","age":30,"city":"New York","children":[{"name":"Alice","age":5},{"name":"Bob","age":7}],"parent":{"name":"Jane","age":60,"city":"New York"}}`), &omap) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal JSON: %+v", err) | ||
} | ||
|
||
fmt.Printf("%v\n", omap) | ||
// Marshal the OrderedRecursiveMap back into JSON | ||
marshaledYaml, err := yaml.Marshal(&omap) | ||
if err != nil { | ||
t.Fatalf("Failed to marshal OrderedRecursiveMap: %v", err) | ||
} | ||
|
||
expected := `name: John | ||
age: 30 | ||
city: New York | ||
children: | ||
- name: Alice | ||
age: 5 | ||
- name: Bob | ||
age: 7 | ||
parent: | ||
name: Jane | ||
age: 60 | ||
city: New York | ||
` | ||
|
||
// Check if the original JSON and the marshaled JSON are the same | ||
if expected != string(marshaledYaml) { | ||
t.Errorf("Marshalled yaml is not valid. Got:\n##\n%s\n##\n,\nMarshaled:\n##\n%s\n##", string(marshaledYaml), expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters