forked from readium/readium-lcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canon.go
45 lines (39 loc) · 1.12 KB
/
canon.go
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
43
44
45
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
package sign
import (
"bytes"
"encoding/json"
"io"
"strings"
)
func Canon(in interface{}) ([]byte, error) {
// the easiest way to canonicalize is to marshal it and reify it as a map
// which will sort stuff correctly
b, err1 := json.Marshal(in)
if err1 != nil {
return b, err1
}
var jsonObj interface{} // map[string]interface{} ==> auto sorting
dec := json.NewDecoder(strings.NewReader(string(b)))
dec.UseNumber()
for {
if err2 := dec.Decode(&jsonObj); err2 == io.EOF {
break
} else if err2 != nil {
return nil, err2
}
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
// do not escape characters
enc.SetEscapeHTML(false)
err := enc.Encode(jsonObj)
if err != nil {
return nil, err
}
// remove the trailing newline, added by encode
return bytes.TrimRight(buf.Bytes(), "\n"), nil
}