12 // JSON converts the contained object to a JSON string
14 func (m Map) JSON() (string, error) {
16 result, err := json.Marshal(m)
19 err = errors.New("objx: JSON encode failed with: " + err.Error())
22 return string(result), err
26 // MustJSON converts the contained object to a JSON string
27 // representation and panics if there is an error
28 func (m Map) MustJSON() string {
29 result, err := m.JSON()
36 // Base64 converts the contained object to a Base64 string
37 // representation of the JSON string representation
38 func (m Map) Base64() (string, error) {
42 jsonData, err := m.JSON()
47 encoder := base64.NewEncoder(base64.StdEncoding, &buf)
48 encoder.Write([]byte(jsonData))
51 return buf.String(), nil
55 // MustBase64 converts the contained object to a Base64 string
56 // representation of the JSON string representation and panics
57 // if there is an error
58 func (m Map) MustBase64() string {
59 result, err := m.Base64()
66 // SignedBase64 converts the contained object to a Base64 string
67 // representation of the JSON string representation and signs it
68 // using the provided key.
69 func (m Map) SignedBase64(key string) (string, error) {
71 base64, err := m.Base64()
76 sig := HashWithKey(base64, key)
78 return base64 + SignatureSeparator + sig, nil
82 // MustSignedBase64 converts the contained object to a Base64 string
83 // representation of the JSON string representation and signs it
84 // using the provided key and panics if there is an error
85 func (m Map) MustSignedBase64(key string) string {
86 result, err := m.SignedBase64(key)
95 ------------------------------------------------
98 // URLValues creates a url.Values object from an Obj. This
99 // function requires that the wrapped object be a map[string]interface{}
100 func (m Map) URLValues() url.Values {
102 vals := make(url.Values)
104 for k, v := range m {
105 //TODO: can this be done without sprintf?
106 vals.Set(k, fmt.Sprintf("%v", v))
112 // URLQuery gets an encoded URL query representing the given
113 // Obj. This function requires that the wrapped object be a
114 // map[string]interface{}
115 func (m Map) URLQuery() (string, error) {
116 return m.URLValues().Encode(), nil