-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
example_interface_test.go
79 lines (66 loc) · 1.79 KB
/
example_interface_test.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
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
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package oj_test
import (
"fmt"
"github.com/ohler55/ojg"
"github.com/ohler55/ojg/alt"
"github.com/ohler55/ojg/oj"
)
// Encode and decode slice of interfaces.
type Animal interface {
Kind() string
}
type Dog struct {
Size string
}
func (d *Dog) Kind() string {
return fmt.Sprintf("%s dog", d.Size)
}
type Cat struct {
Color string
}
func (c *Cat) Kind() string {
return fmt.Sprintf("%s cat", c.Color)
}
func ExampleUnmarshal_interface() {
pets := []Animal{&Dog{Size: "big"}, &Cat{Color: "black"}}
// Encode and use a create key to identify the encoded type.
b, err := oj.Marshal(pets, &ojg.Options{CreateKey: "^", Sort: true})
if err != nil {
panic(err)
}
// Sort the object members in the output for repeatability.
fmt.Printf("as JSON: %s\n", b)
// Create a new Recomposer. This can be use over and over again. Register
// the types with a nil creation function to let reflection do the work
// since the types are exported.
var r *alt.Recomposer
if r, err = alt.NewRecomposer("^", map[any]alt.RecomposeFunc{&Dog{}: nil, &Cat{}: nil}); err != nil {
panic(err)
}
var result any
if err = oj.Unmarshal(b, &result, r); err != nil {
panic(err)
}
list, _ := result.([]any)
for _, item := range list {
animal, _ := item.(Animal)
fmt.Printf(" %s\n", animal.Kind())
}
// Unmarshal with a typed target.
var animals []Animal
if err = oj.Unmarshal(b, &animals, r); err != nil {
panic(err)
}
fmt.Println("Unmarshal into a target struct")
for _, animal := range animals {
fmt.Printf(" %T - %s\n", animal, animal.Kind())
}
// Output:
// as JSON: [{"^":"Dog","size":"big"},{"^":"Cat","color":"black"}]
// big dog
// black cat
// Unmarshal into a target struct
// *oj_test.Dog - big dog
// *oj_test.Cat - black cat
}