dynamic

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2016 License: MIT Imports: 2 Imported by: 4

README

dynamic Build Status GoDoc

Unmarshal JSON with dynamic (multiple) types.

Examples

See godoc

go get github.com/goburrow/dynamic
package main

import (
	"encoding/json"
	"fmt"

	"github.com/goburrow/dynamic"
)

type Info struct {
	Type    string
	Message string
}

type Error struct {
	Errors []string
}

type Status struct {
	Code int
}

func init() {
	dynamic.Register("info", func() interface{} { return &Info{} })
	dynamic.Register("error", func() interface{} { return &Error{} })
	dynamic.Register("status", func() interface{} { return &Status{} })
}

func main() {
	json1 := `{"Type": "info", "Message": "hello"}`
	json2 := `{"Type": "error", "Errors": ["unknown"]}`
	json3 := `{"Type": "status", "Data": {"Code": 213}}`

	var obj1, obj2 dynamic.Type
	var obj3 dynamic.Data
	json.Unmarshal([]byte(json1), &obj1)
	json.Unmarshal([]byte(json2), &obj2)
	json.Unmarshal([]byte(json3), &obj3)

	fmt.Printf("1: %#v\n", obj1.Value())
	fmt.Printf("2: %#v\n", obj2.Value())
	fmt.Printf("3: %#v\n", obj3.Value())
}

Output:

1: &main.Info{Type:"info", Message:"hello"}
2: &main.Error{Errors:[]string{"unknown"}}
3: &main.Status{Code:213}

TODO

  • Support sub-types.

Documentation

Overview

Package dynamic provides support for unmarshal dynamic JSON objects.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, f func() interface{})

Register associates factory method for a type name. The factory method must return a pointer to a struct it's going to create.

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

func (d *Data) UnmarshalJSON(data []byte) error

UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON to the instance created by respective factory method.

func (*Data) Value

func (d *Data) Value() interface{}

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

func (t *Type) MarshalJSON() ([]byte, error)

MarshalJSON marshals the t.Value.

func (*Type) SetValue

func (t *Type) SetValue(v interface{})

SetValue sets value to t.

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(data []byte) error

UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON to the instance created by respective factory method.

func (*Type) Value

func (t *Type) Value() interface{}

Value returns the value marshalled in t.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL