File: testtools.go

package info (click to toggle)
golang-github-flowstack-go-jsonschema 0.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: makefile: 15
file content (42 lines) | stat: -rw-r--r-- 704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package testtools

import (
	"bytes"
	"encoding/json"
)

func CompareJSON(j1, j2 []byte) (bool, error) {
	var err error

	j1, err = SortAndCompactJSON(j1)
	if err != nil {
		return false, err
	}

	j2, err = SortAndCompactJSON(j2)
	if err != nil {
		return false, err
	}

	return (string(j1) == string(j2)), nil
}

func SortAndCompactJSON(j []byte) ([]byte, error) {
	// Unmarshalling to an interface and marshalling back, will cause the fields to be sorted
	var err error

	var tmp interface{}
	if err = json.Unmarshal(j, &tmp); err != nil {
		return nil, err
	}

	j, err = json.Marshal(tmp)
	if err != nil {
		return nil, err
	}

	var out bytes.Buffer
	json.Compact(&out, j)

	return out.Bytes(), nil
}