Browse Source

first commit

master
Christoph Haug 7 months ago
commit
5080347761
  1. 1
      .gitignore
  2. 17
      .vscode/launch.json
  3. 57
      equals.go
  4. 112
      equals_test.go

1
.gitignore

@ -0,0 +1 @@
go.mod

17
.vscode/launch.json

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}

57
equals.go

@ -0,0 +1,57 @@
package jsonutil
import (
"encoding/json"
"fmt"
)
func dumpMap(space string, m map[string]interface{}) {
for k, v := range m {
if mv, ok := v.(map[string]interface{}); ok {
fmt.Printf("{ \"%v\": \n", k)
dumpMap(space+"\t", mv)
fmt.Printf("}\n")
} else {
fmt.Printf("%v %v : %v\n", space, k, v)
}
}
}
func equalsMap(object1 *map[string]interface{}, object2 *map[string]interface{}) bool {
if len(*object1) != len(*object2) {
return false
}
for k, v := range *object1 {
if v2, ok := (*object2)[k]; ok {
if mv, ok := v.(map[string]interface{}); ok {
if mv2, ok := v2.(map[string]interface{}); ok {
return equalsMap(&mv, &mv2)
} else {
return false
}
} else if v == v2 {
return true
}
} else {
return false
}
}
return true
}
func Equals(json1 *string, json2 *string) bool {
json1Object := make(map[string]interface{})
err := json.Unmarshal([]byte(*json1), &json1Object)
if err != nil {
panic(err)
}
json2Object := make(map[string]interface{})
err = json.Unmarshal([]byte(*json2), &json2Object)
if err != nil {
panic(err)
}
//dumpMap("", json1Object)
//fmt.Println(json1Object["sub"].(map[string]interface{})["msg"])
return equalsMap(&json1Object, &json2Object)
}

112
equals_test.go

@ -0,0 +1,112 @@
package jsonutil
import "testing"
var json1 = `
{
"hallo":"hallo1",
"sub":{
"msg":"sub1",
"msg1":"sub2",
"msg3":"sub3",
"array":[
"test",
"test1",
"test2"
]
}
}
`
var json2 = `
{
"hallo":"hallo1",
"sub":{
"msg1":"sub2",
"msg":"sub1",
"msg3":"sub3",
"array":[
"test",
"test2",
"test1"
]
}
}
`
var json3 = `
{
"hallo":"hallo1",
"sub":{
"msg1":"sub2",
"msg":"sub5",
"msg3":"sub3",
"array":[
"test",
"test2",
"test1"
]
}
}
`
var json4 = `
{
"hallo":"hallo1",
"sub":{
"msg1":"sub2",
"msg":"sub5",
"msg3":"sub3",
"array":[
"test",
"test5",
"test1"
]
},
"hallo1":"test"
}
`
var json5 = `
{
"hallo":"hallo1",
"sub":{
"msg1":"sub2",
"msg":"sub5",
"msg3":"sub3",
"array":[
"test",
"test5",
"test1"
]
}
}
`
func TestEqualsSame(t *testing.T) {
if Equals(&json1, &json1) {
t.Logf("ok: json1 is the same as json1")
} else {
t.Errorf("error: Equals not work. json1 is the same as json1")
}
}
func TestEqualsJson1AndJson2(t *testing.T) {
if Equals(&json1, &json2) {
t.Logf("ok: json1 is the same as json2")
} else {
t.Errorf("error: Equals not work. json1 is the same as json2")
}
}
func TestEqualsJson1AndJson3(t *testing.T) {
if Equals(&json1, &json3) {
t.Logf("ok: json1 is the same as json3")
} else {
t.Errorf("error: Equals not work. json1 is the same as json3")
}
}
func TestEqualsJson1AndJson4(t *testing.T) {
if Equals(&json1, &json4) == false {
t.Logf("ok: json1 is not the same as json4")
} else {
t.Errorf("error: Equals not work. json1 is not the same as json4")
}
}
Loading…
Cancel
Save