Guest User

Untitled

a guest
Mar 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. )
  6.  
  7. type Business struct {
  8. ID string `json:"id"`
  9. Name string `json:"name"`
  10. }
  11.  
  12. type BusinessRef struct {
  13. Business
  14. IDOnly bool
  15. }
  16.  
  17. func (bj *BusinessRef) UnmarshalJSON(data []byte) error {
  18. var id string
  19. err := json.Unmarshal(data, &id)
  20. if err != nil {
  21. // if the err was because the value found is an object,
  22. // try again with the Business struct
  23. typeErr, ok := err.(*json.UnmarshalTypeError)
  24. if ok && typeErr.Value == "object" {
  25. return json.Unmarshal(data, &bj.Business)
  26. }
  27. // otherwise, return the original error
  28. return err
  29. }
  30. bj.ID = id
  31. bj.IDOnly = true // indicate that business was just a string
  32. return nil
  33. }
  34.  
  35. type JobOffer struct {
  36. Business *BusinessRef `json:"business"`
  37. Position string `json:"position"`
  38. }
Add Comment
Please, Sign In to add comment