func (m *Macaroon) unmarshalJSONOldFormat(data []byte) error {
	var m1 *macaroon.Macaroon
	if err := json.Unmarshal(data, &m1); err != nil {
		return errgo.Mask(err)
	}
	m2, err := NewLegacyMacaroon(m1)
	if err != nil {
		return errgo.Mask(err)
	}
	*m = *m2
	return nil
}
unmarshalJSONOldFormat unmarshals the data from an old format
macaroon (without any external caveats or namespace).
package bakery
import (
	"bytes"
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"golang.org/x/net/context"
	"gopkg.in/errgo.v1"
	"gopkg.in/macaroon.v2"
	"gopkg.in/macaroon-bakery.v2/bakery/checkers"
)
func legacyNamespace() *checkers.Namespace {
	ns := checkers.NewNamespace(nil)
	ns.Register(checkers.StdNamespace, "")
	return ns
}
type Macaroon struct {
	m *macaroon.Macaroon
	version Version
	caveatData map[string][]byte
	namespace *checkers.Namespace
	caveatIdPrefix []byte
}
func NewLegacyMacaroon(m *macaroon.Macaroon) (*Macaroon, error) {
	v, err := bakeryVersion(m.Version())
	if err != nil {
		return nil, errgo.Mask(err)
	}
	return &Macaroon{
		m:         m,
		version:   v,
		namespace: legacyNamespace(),
	}, nil
}
type macaroonJSON struct {
	Macaroon *macaroon.Macaroon `json:"m"`
	Version  Version            `json:"v"`
	CaveatData map[string]string   `json:"cdata,omitempty"`
	Namespace  *checkers.Namespace `json:"ns"`
}
func (m *Macaroon) Clone() *Macaroon {
	m1 := *m
	m1.m = m1.m.Clone()
	m1.caveatData = make(map[string][]byte)
	for id, data := range m.caveatData {
		m1.caveatData[id] = data
	}
	return &m1
}
func (m *Macaroon) MarshalJSON() ([]byte, error) {
	if m.version < Version3 {
		if len(m.caveatData) > 0 {
			return nil, errgo.Newf("cannot marshal pre-version3 macaroon with external caveat data")
		}
		return m.m.MarshalJSON()
	}
	caveatData := make(map[string]string)
	for id, data := range m.caveatData {
		caveatData[base64.RawURLEncoding.EncodeToString([]byte(id))] = base64.RawURLEncoding.EncodeToString(data)
	}
	return json.Marshal(macaroonJSON{
		Macaroon:   m.m,
		Version:    m.version,
		CaveatData: caveatData,
		Namespace:  m.namespace,
	})
}
func (m *Macaroon) UnmarshalJSON(data []byte) error {
	var m1 macaroonJSON
	if err := json.Unmarshal(data, &m1); err != nil {
		return errgo.Mask(err)
	}
	if m1.Macaroon == nil {
		return m.unmarshalJSONOldFormat(data)
	}
	if m1.Version < Version3 || m1.Version > LatestVersion {
		return errgo.Newf("unexpected bakery macaroon version; got %d want %d", m1.Version, Version3)
	}
	if got, want := m1.Macaroon.Version(), MacaroonVersion(m1.Version); got != want {
		return errgo.Newf("underlying macaroon has inconsistent version; got %d want %d", got, want)
	}
	caveatData := make(map[string][]byte)
	for id64, data64 := range m1.CaveatData {
		id, err := macaroon.Base64Decode([]byte(id64))
		if err != nil {
			return errgo.Notef(err, "cannot decode caveat id")
		}
		data, err := macaroon.Base64Decode([]byte(data64))
		if err != nil {
			return errgo.Notef(err, "cannot decode caveat")
		}
		caveatData[string(id)] = data
	}
	m.caveatData = caveatData
	m.m = m1.Macaroon
	m.namespace = m1.Namespace
	m.version = m1.Version
	return nil
}