-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializable_object.go
More file actions
122 lines (99 loc) · 2.84 KB
/
serializable_object.go
File metadata and controls
122 lines (99 loc) · 2.84 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
package gotio
import (
"bytes"
"io"
"os"
"github.com/Avalanche-io/gotio/internal/jsonenc"
)
// SerializableObject is the base interface for all serializable types.
type SerializableObject interface {
// SchemaName returns the schema name.
SchemaName() string
// SchemaVersion returns the schema version.
SchemaVersion() int
// Clone creates a deep copy.
Clone() SerializableObject
// IsEquivalentTo returns true if equivalent to another.
IsEquivalentTo(other SerializableObject) bool
}
// FromJSONString parses JSON into a SerializableObject.
func FromJSONString(jsonStr string) (SerializableObject, error) {
return FromJSONBytes([]byte(jsonStr))
}
// FromJSONBytes parses JSON bytes into a SerializableObject.
func FromJSONBytes(data []byte) (SerializableObject, error) {
return FromJSONBytesSonic(data)
}
// FromJSONFile reads a JSON file into a SerializableObject.
func FromJSONFile(filename string) (SerializableObject, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return FromJSONBytes(data)
}
// ToJSONString converts a SerializableObject to JSON string.
// If indent is provided, the output will be pretty-printed.
func ToJSONString(obj SerializableObject, indent string) (string, error) {
var data []byte
var err error
if indent != "" {
data, err = ToJSONBytesIndent(obj, indent)
} else {
data, err = ToJSONBytes(obj)
}
if err != nil {
return "", err
}
return string(data), nil
}
// ToJSONBytes converts a SerializableObject to JSON bytes.
func ToJSONBytes(obj SerializableObject) ([]byte, error) {
var buf bytes.Buffer
enc := jsonenc.NewEncoder(&buf)
defer enc.Release()
if err := jsonenc.EncodeValue(enc, obj); err != nil {
return nil, err
}
if err := enc.Flush(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ToJSONWriter writes a SerializableObject to an io.Writer.
func ToJSONWriter(obj SerializableObject, w io.Writer) error {
enc := jsonenc.NewEncoder(w)
defer enc.Release()
if err := jsonenc.EncodeValue(enc, obj); err != nil {
return err
}
return enc.Flush()
}
// ToJSONBytesIndent converts a SerializableObject to indented JSON bytes.
func ToJSONBytesIndent(obj SerializableObject, indent string) ([]byte, error) {
data, err := ToJSONBytes(obj)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := jsonIndent(&buf, data, "", indent); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ToJSONFile writes a SerializableObject to a JSON file.
func ToJSONFile(obj SerializableObject, filename string, indent string) error {
var data []byte
var err error
if indent != "" {
data, err = ToJSONBytesIndent(obj, indent)
} else {
data, err = ToJSONBytes(obj)
}
if err != nil {
return err
}
return os.WriteFile(filename, data, 0644)
}