Documentation
¶
Overview ¶
Package dynamic provides support for unmarshal dynamic JSON objects.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Data ¶
type Data struct { Type string Data interface{} }
Data represents JSON objects that have a `Type' and `Data' property for underlining data structure. For example:
{ "Type": "Point", "Data": { "X": 21, "Y": 3 } }
is the JSON representation of:
type Point struct { X int Y int }
func (*Data) UnmarshalJSON ¶
UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON to the instance created by respective factory method.
type Type ¶
type Type [1]interface{}
Type represents objects that have their properties at top level along with `Type' property. The `Type' property is not compulsory when unmarshalling but needed when marshalling to JSON. For example:
{ "Type": "Point", "X": 21, "Y": 3 }
is the JSON representation of:
type Point struct { X int Y int }
Example (Marshal) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) type Gopher struct { G string } func main() { var test dynamic.Type test.SetValue(&Gopher{"gopher"}) data, err := json.Marshal(&test) if err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("%s\n", data) }
Output: {"G":"gopher"}
Example (MarshalListInStruct) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) type Gopher struct { G string } type Breaver struct { B string } func main() { var test struct { X []dynamic.Type } test.X = []dynamic.Type{ dynamic.Type{&Gopher{"gopher"}}, dynamic.Type{&Breaver{"breaver"}}, } data, err := json.Marshal(&test) if err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("%s\n", data) }
Output: {"X":[{"G":"gopher"},{"B":"breaver"}]}
Example (MarshalStruct) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) type Gopher struct { G string } type Breaver struct { B string } func main() { var test struct { X dynamic.Type Y dynamic.Type } test.X.SetValue(&Gopher{"gopher"}) test.Y.SetValue(&Breaver{"breaver"}) data, err := json.Marshal(&test) if err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("%s\n", data) }
Output: {"X":{"G":"gopher"},"Y":{"B":"breaver"}}
Example (Unmarshal) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) func main() { var test dynamic.Type data := ` { "type": "go", "G": "gopher" }` if err := json.Unmarshal([]byte(data), &test); err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("%#v\n", test.Value()) }
Output: &dynamic_test.Gopher{G:"gopher"}
Example (UnmarshalList) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) func main() { var test []dynamic.Type data := ` [ { "type": "go", "G": "gopher" }, { "Type": "br", "B": "breaver" } ]` if err := json.Unmarshal([]byte(data), &test); err != nil { fmt.Printf("%v\n", err) return } for _, t := range test { fmt.Printf("%#v\n", t.Value()) } }
Output: &dynamic_test.Gopher{G:"gopher"} &dynamic_test.Breaver{B:"breaver"}
Example (UnmarshalListInStruct) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) func main() { var test struct { X []dynamic.Type } data := ` { "X": [ { "Type": "go", "G": "gopher" }, { "type": "br", "B": "breaver" } ] }` if err := json.Unmarshal([]byte(data), &test); err != nil { fmt.Printf("%v\n", err) return } for _, t := range test.X { fmt.Printf("%#v\n", t.Value()) } }
Output: &dynamic_test.Gopher{G:"gopher"} &dynamic_test.Breaver{B:"breaver"}
Example (UnmarshalStruct) ¶
package main import ( "encoding/json" "fmt" "github.com/goburrow/dynamic" ) func main() { var test struct { X dynamic.Type } data := ` { "X": { "type": "go", "G": "gopher" } }` if err := json.Unmarshal([]byte(data), &test); err != nil { fmt.Printf("%v\n", err) return } fmt.Printf("%#v\n", test.X.Value()) }
Output: &dynamic_test.Gopher{G:"gopher"}
func (*Type) MarshalJSON ¶
MarshalJSON marshals the t.Value.
func (*Type) UnmarshalJSON ¶
UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON to the instance created by respective factory method.