1package jsoninfo 2 3import ( 4 "encoding/json" 5) 6 7func MarshalRef(value string, otherwise interface{}) ([]byte, error) { 8 if len(value) > 0 { 9 return json.Marshal(&refProps{ 10 Ref: value, 11 }) 12 } 13 return json.Marshal(otherwise) 14} 15 16func UnmarshalRef(data []byte, destRef *string, destOtherwise interface{}) error { 17 refProps := &refProps{} 18 if err := json.Unmarshal(data, refProps); err == nil { 19 ref := refProps.Ref 20 if len(ref) > 0 { 21 *destRef = ref 22 return nil 23 } 24 } 25 return json.Unmarshal(data, destOtherwise) 26} 27 28type refProps struct { 29 Ref string `json:"$ref,omitempty"` 30} 31