Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- )
- type Business struct {
- ID string `json:"id"`
- Name string `json:"name"`
- }
- type BusinessRef struct {
- Business
- IDOnly bool
- }
- func (bj *BusinessRef) UnmarshalJSON(data []byte) error {
- var id string
- err := json.Unmarshal(data, &id)
- if err != nil {
- // if the err was because the value found is an object,
- // try again with the Business struct
- typeErr, ok := err.(*json.UnmarshalTypeError)
- if ok && typeErr.Value == "object" {
- return json.Unmarshal(data, &bj.Business)
- }
- // otherwise, return the original error
- return err
- }
- bj.ID = id
- bj.IDOnly = true // indicate that business was just a string
- return nil
- }
- type JobOffer struct {
- Business *BusinessRef `json:"business"`
- Position string `json:"position"`
- }
Add Comment
Please, Sign In to add comment