Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xujihui1985/learninggo in…
Browse files Browse the repository at this point in the history
…to master
  • Loading branch information
xujihui1985 committed Mar 28, 2021
2 parents 554003b + f075b72 commit 61c0047
Show file tree
Hide file tree
Showing 19 changed files with 1,193 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/learninggo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

370 changes: 370 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions json/customize_unmarshal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"encoding/json"
"errors"
"fmt"
)

// http://gregtrowbridge.com/golang-json-serialization-with-interfaces/
type ColoredThing interface {
Color() string
}

type ColorfulEcosystem struct {
Things []ColoredThing `json:"things"`
}

func (ce *ColorfulEcosystem) UnmarshalJSON(b []byte) error {
var objMap map[string]*json.RawMessage
err := json.Unmarshal(b, &objMap)
if err != nil {
return err
}
var rawMessages []*json.RawMessage
err = json.Unmarshal(*objMap["things"], &rawMessages)
if err != nil {
return err
}
ce.Things = make([]ColoredThing, len(rawMessages))
var m map[string]string

for idx, rawMessage := range rawMessages {
err = json.Unmarshal(*rawMessage, &m)
if err != nil {
return err
}

if m["type"] == "plant" {
var p Plant
err := json.Unmarshal(*rawMessage, &p)
if err != nil {
return err
}
ce.Things[idx] = &p
} else if m["type"] == "animal" {
var a Animal
err := json.Unmarshal(*rawMessage, &a)
if err != nil {
return err
}
ce.Things[idx] = &a
} else {
return errors.New("unsupported type " + m["type"])
}
}
return nil
}

type Plant struct {
MyColor string `json:"color"`
}

type Animal struct {
MyColor string `json:"color"`
}

func (p *Plant) Color() string {
return p.MyColor
}

func (a *Plant) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"type": "plant",
"color": a.Color(),
})
}

func (a *Animal) Color() string {
return a.MyColor
}

func (a *Animal) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"type": "animal",
"color": a.Color(),
})
}

func main() {
fern := &Plant{MyColor: "green"}
flower := &Plant{MyColor: "purple"}

panther := &Animal{MyColor: "black"}
lizard := &Animal{MyColor: "green"}

// Then let's create a ColorfulEcosystem
colorfulEcosystem := ColorfulEcosystem{
Things: []ColoredThing{
fern,
flower,
panther,
lizard,
},
}

byteSlice, _ := json.Marshal(colorfulEcosystem)
fmt.Println(string(byteSlice))

ce := ColorfulEcosystem{}

err := json.Unmarshal(byteSlice, &ce)
if err != nil {
panic(err)
}
for _, c := range ce.Things {
fmt.Println(c.Color())
}
}
33 changes: 33 additions & 0 deletions json/marshal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"encoding/json"
"fmt"
)

type Plant struct {
MyColor string
}

func (p Plant) MarshalJSON() ([]byte, error) {
m := make(map[string]string)
m["type"] = "plant"
m["color"] = p.MyColor
return json.Marshal(m)
}

type Animal struct {
MyColor string
}

func main() {
p := Plant{MyColor: "green"}
a := Animal{MyColor: "red"}

b, _ := json.Marshal(p)
ba, _ := json.Marshal(a)

fmt.Println(string(b))
fmt.Println(string(ba))

}
6 changes: 6 additions & 0 deletions net/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions net/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions net/.idea/net.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 61c0047

Please sign in to comment.