Unmarshalling json lists  with multiple types  Also called "Fun with json & types"                  or     "Help me find a...
{    "things": [       {          "name": "Alice",          "age": 37       },       {          "city": "Ipoh",          "...
To get us startedtype Person struct { Name string Age int}type Place struct { City string Country string}func solution(jso...
{    "things": [       {          "name": "Alice",          "age": 37       },       {          "city": "Ipoh",          "...
Map & type assertfunc solutionA(jsonStr []byte) ([]Person, []Place) {      func addPerson(persons []Person, item map[strin...
{    "things": [       {          "name": "Alice",          "age": 37       },       {          "city": "Ipoh",          "...
Mixed Type Structfunc solutionB(jsonStr []byte) ([]Person, []Place) {       type Mixed struct {      persons := []Person{}...
{    "things": [       {          "name": "Alice",          "age": 37       },       {          "city": "Ipoh",          "...
json.RawMessagefunc solutionC(jsonStr []byte) ([]Person, []Place) {   func addPersonC(thing json.RawMessage, people []Pers...
https://gist.github.com/mattyw/4556603http://bit.ly/10Os7Tp <- Original blog post
Upcoming SlideShare
Loading in...5
×

Unmarshalling json lists with multiple types

484
-1

Published on

My talk from the London go user group on 27 March 2013

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
484
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unmarshalling json lists with multiple types

  1. 1. Unmarshalling json lists with multiple types Also called "Fun with json & types" or "Help me find another way"
  2. 2. { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Place
  3. 3. To get us startedtype Person struct { Name string Age int}type Place struct { City string Country string}func solution(jsonString []byte) ([]Person []Place) {}Error handling is allowed to be good -> non existent and inconsistent
  4. 4. { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Placemap[string][]map[string]interface{}
  5. 5. Map & type assertfunc solutionA(jsonStr []byte) ([]Person, []Place) { func addPerson(persons []Person, item map[string]interface persons := []Person{} {}) []Person { places := []Place{} name, _ := item["name"].(string) var data map[string][]map[string]interface{} age, _ := item["age"].(int) err := json.Unmarshal(jsonStr, &data) person := Person{name, age} if err != nil { persons = append(persons, person) fmt.Println(err) return persons return persons, places } } for i := range data["things"] { func addPlace(places []Place, item map[string]interface{}) []Place { item := data["things"][i] city, _ := item["city"].(string) if item["name"] != nil { country, _ := item["city"].(string) persons = addPerson(persons, item) place := Place{city, country} } else { places = append(places, place) places = addPlace(places, item) return places } } } return persons, places}
  6. 6. { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Placemap[string][]Person&Place??
  7. 7. Mixed Type Structfunc solutionB(jsonStr []byte) ([]Person, []Place) { type Mixed struct { persons := []Person{} Name string `json:"name"` places := []Place{} Age int `json:"age"` var data map[string][]Mixed City string `json:"city"` err := json.Unmarshal(jsonStr, &data) Country string `json:"country"` if err != nil { } fmt.Println(err) return persons, places } for i := range data["things"] { item := data["things"][i] if item.Name != "" { persons = append(persons, Person{item.Name, item.Age}) } else { places = append(places, Place{item.City, item.Country}) } } return persons, places}
  8. 8. { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Placemap[string][]PleaseLeaveAsJson
  9. 9. json.RawMessagefunc solutionC(jsonStr []byte) ([]Person, []Place) { func addPersonC(thing json.RawMessage, people []Person) [] people := []Person{} Person { places := []Place{} person := Person{} var data map[string][]json.RawMessage if err := json.Unmarshal(thing, &person); err != nil { err := json.Unmarshal(jsonStr, &data) fmt.Println(err) if err != nil { } else { fmt.Println(err) if person != *new(Person) { return people, places people = append(people, person) } } for _, thing := range data["things"] { } people = addPersonC(thing, people) return people places = addPlaceC(thing, places) } } func addPlaceC(thing json.RawMessage, places []Place) []Place { return people, places place := Place{}} if err := json.Unmarshal(thing, &place); err != nil { fmt.Println(err) } else { if place != *new(Place) { places = append(places, place) } } return places }
  10. 10. https://gist.github.com/mattyw/4556603http://bit.ly/10Os7Tp <- Original blog post
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×