/
type.go
49 lines (40 loc) · 857 Bytes
/
type.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
46
47
48
49
package jstn
import (
"encoding/json"
)
// A Kind represents a primitive JSON type.
type Kind int
const (
String Kind = iota
Number
Boolean
Null
Object
Array
)
type Type struct {
Kind Kind
Optional bool
Properties map[string]*Type // Only for Objects
Items *Type // Only for Arrays
}
func (t Type) String() string {
out, _ := Generate(t)
return string(out)
}
// MarshalJSON implements json.Unmarshaler by converting t to its concise JSTN
// text representation.
func (t Type) MarshalJSON() ([]byte, error) {
out, _ := Generate(t)
return json.Marshal(string(out))
}
// UnmarshalJSON implements json.Unmarshaler by parsing a JSTN text string.
func (t *Type) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
tt, err := Parse(s)
*t = tt
return err
}