func (m *Macaroon) UnmarshalJSON(data []byte) error {
	if data[0] == '"' {
		var s string
		if err := json.Unmarshal(data, &s); err != nil {
			return err
		}
		data, err := Base64Decode([]byte(s))
		if err != nil {
			return err
		}
		if err := m.UnmarshalBinary(data); err != nil {
			return err
		}
		return nil
	}
	type MacaroonJSONV1 macaroonJSONV1
	type MacaroonJSONV2 macaroonJSONV2
	var both struct {
		*MacaroonJSONV1
		*MacaroonJSONV2
	}
	if err := json.Unmarshal(data, &both); err != nil {
		return err
	}
	switch {
	case both.MacaroonJSONV1 != nil && both.MacaroonJSONV2 != nil:
		return fmt.Errorf("cannot determine macaroon encoding version")
	case both.MacaroonJSONV1 != nil:
		if err := m.initJSONV1((*macaroonJSONV1)(both.MacaroonJSONV1)); err != nil {
			return err
		}
		m.version = V1
	case both.MacaroonJSONV2 != nil:
		if err := m.initJSONV2((*macaroonJSONV2)(both.MacaroonJSONV2)); err != nil {
			return err
		}
		m.version = V2
	default:
		return fmt.Errorf("invalid JSON macaroon encoding")
	}
	return nil
}
UnmarshalJSON implements json.Unmarshaller by unmarshaling
the given macaroon in JSON format. It accepts both V1 and V2
forms encoded forms, and also a base64-encoded JSON string
containing the binary-marshaled macaroon.

After unmarshaling, the macaroon's version will reflect
the version that it was unmarshaled as.
package macaroon
import (
	"encoding/base64"
	"encoding/json"
	"fmt"
)
type Version uint16
const (
	V1 Version = 1
	V2 Version = 2
	LatestVersion = V2
)
func (v Version) String() string {
	return fmt.Sprintf("v%d", v)
}
func (m *Macaroon) Version() Version {
	return m.version
}
func (m *Macaroon) MarshalJSON() ([]byte, error) {
	switch m.version {
	case V1:
		return m.marshalJSONV1()
	case V2:
		return m.marshalJSONV2()
	default:
		return nil, fmt.Errorf("unknown version %v", m.version)
	}
}