• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Unmarshalling json lists with multiple types
 

Unmarshalling json lists with multiple types

on

  • 516 views

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

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

Statistics

Views

Total Views
516
Views on SlideShare
504
Embed Views
12

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 12

https://twitter.com 6
http://lanyrd.com 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Unmarshalling json lists with multiple types Unmarshalling json lists with multiple types Presentation Transcript

    • Unmarshalling json lists with multiple types Also called "Fun with json & types" or "Help me find another way"
    • { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Place
    • 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
    • { "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{}
    • 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}
    • { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Placemap[string][]Person&Place??
    • 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}
    • { "things": [ { "name": "Alice", "age": 37 }, { "city": "Ipoh", "country": "Malaysia" }, { "name": "Bob", "age": 36 }, { "city": "Northampton", "country": "England" } ]}map[string]Person|Placemap[string][]PleaseLeaveAsJson
    • 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 }
    • https://gist.github.com/mattyw/4556603http://bit.ly/10Os7Tp <- Original blog post