forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringInterfaces.go
More file actions
69 lines (58 loc) · 1.45 KB
/
stringInterfaces.go
File metadata and controls
69 lines (58 loc) · 1.45 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package common
import (
"bytes"
"encoding/json"
"github.com/davecgh/go-spew/spew"
)
//Interface for printing structures into JSON
type JSONable interface {
JSONByte() ([]byte, error)
JSONString() (string, error)
JSONBuffer(b *bytes.Buffer) error
}
//Interface for Spewing the structures for debugging
type Spewable interface {
Spew() string
}
//Interface for both JSON and Spew
type Printable interface {
JSONable
Spewable
}
//Interface for short, reoccuring data structures to interpret themselves into human-friendly form
type ShortInterpretable interface {
IsInterpretable() bool //Whether the structure can interpret itself
Interpret() string //Turns the data encoded int he structure into human-friendly string
}
func DecodeJSON(data []byte, v interface{}) error {
err := json.Unmarshal(data, &v)
return err
}
func EncodeJSON(data interface{}) ([]byte, error) {
encoded, err := json.Marshal(data)
if err != nil {
return nil, err
}
return encoded, nil
}
func EncodeJSONString(data interface{}) (string, error) {
encoded, err := EncodeJSON(data)
if err != nil {
return "", err
}
return string(encoded), err
}
func DecodeJSONString(data string, v interface{}) error {
return DecodeJSON([]byte(data), v)
}
func EncodeJSONToBuffer(data interface{}, b *bytes.Buffer) error {
encoded, err := EncodeJSON(data)
if err != nil {
return err
}
_, err = b.Write(encoded)
return err
}
func Spew(data interface{}) string {
return spew.Sdump(data)
}