Documentation ¶
Overview ¶
Package rison implements encoding and decoding of Rison. https://github.com/Nanonid/rison
Rison is a data serialization format optimized for compactness in URIs. Rison is a slight variation of JSON that looks vastly superior after URI encoding. Rison still expresses exactly the same set of data structures as JSON, so data can be translated back and forth without loss or guesswork.
Index ¶
- func Decode(data []byte, m Mode) (interface{}, error)
- func Encode(v interface{}, m Mode) ([]byte, error)
- func FromJSON(data []byte, m Mode) ([]byte, error)
- func Marshal(v interface{}, m Mode) ([]byte, error)
- func Quote(s []byte) []byte
- func QuoteString(s string) string
- func ToJSON(data []byte, m Mode) ([]byte, error)
- func Unmarshal(data []byte, v interface{}, m Mode) error
- type Mode
- type ParseError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode parses the Rison-encoded data and returns the result as the tree of map[string]interface{} (or []interface{} or scalar value).
Example ¶
r := "(id:example,str:'string',num:100,yes:!t,nil:!n,arr:!(1,2,3))" v, _ := rison.Decode([]byte(r), rison.Rison) m := v.(map[string]interface{}) fmt.Printf( "id:%v, str:%v, num:%v, yes:%v, nil:%v, arr:%v", m["id"], m["str"], m["num"], m["yes"], m["nil"], m["arr"], )
Output: id:example, str:string, num:100, yes:true, nil:<nil>, arr:[1 2 3]
func FromJSON ¶
FromJSON parses the JSON-encoded data and returns the Rison-encoded data that expresses the equal value.
Example ¶
j := `[1,2.3,"str","-ing","true","nil",{"a":"b"},[7,8,9]]` r, err := rison.FromJSON([]byte(j), rison.Rison) if err != nil { panic(err) } fmt.Printf("%s\n", string(r))
Output: !(1,2.3,str,'-ing',true,nil,(a:b),!(7,8,9))
func Marshal ¶
Marshal returns the Rison encoding of v.
The object keys corresponding the struct fields can be specified in struct tag (not "rison" but) "json".
Example ¶
v := exampleStruct{ I: 1, F: 2.3, S: "str", B: true, P: nil, A: []int64{7, 8, 9}, X: map[string]interface{}{"y": "Y"}, } r, _ := rison.Marshal(&v, rison.Rison) fmt.Println(string(r))
Output: (a:!(7,8,9),b:!t,f:2.3,i:1,p:!n,s:str,x:(y:Y))
func Quote ¶
Quote is like "net/url".QueryEscape but quotes fewer characters.
Example ¶
s := "~!*()-_.,:@$'/ \"#%&+;<=>?[\\]^`{|}" fmt.Println(rison.QuoteString(s))
Output: ~!*()-_.,:@$'/+%22%23%25%26%2B%3B%3C%3D%3E%3F%5B%5C%5D%5E%60%7B%7C%7D
func QuoteString ¶
QuoteString is like "net/url".QueryEscape but quotes fewer characters.
func ToJSON ¶
ToJSON parses the Rison-encoded data and returns the JSON-encoded data that expresses the equal value.
Example ¶
r := "!(1,2.3,str,'ing',true,nil,(a:b),!(7,8,9))" j, _ := rison.ToJSON([]byte(r), rison.Rison) fmt.Printf("%s\n", string(j))
Output: [1,2.3,"str","ing","true","nil",{"a":"b"},[7,8,9]]
func Unmarshal ¶
Unmarshal parses the Rison-encoded data and stores the result in the value pointed to by v.
The object keys corresponding the struct fields can be specified in struct tag (not "rison" but) "json".
Example ¶
r := "(i:1,f:2.3,s:str,b:!t,p:!n,a:!(7,8,9),x:(y:Y))" var v exampleStruct _ = rison.Unmarshal([]byte(r), &v, rison.Rison) fmt.Printf("%+v\n", v)
Output: {I:1 F:2.3 S:str B:true P:<nil> A:[7 8 9] X:map[y:Y]}
Types ¶
type Mode ¶
type Mode int
Mode is an enum type to specify which Rison variation to use to encode/decode.
type ParseError ¶ added in v2.1.0
ParseError is an error type to be raised by parser
func (*ParseError) Error ¶ added in v2.1.0
func (e *ParseError) Error() string
func (*ParseError) ErrorInLang ¶ added in v2.2.0
func (e *ParseError) ErrorInLang(lang string) string
ErrorInLang returns the error message in specified language.
Example ¶
r := "!(" _, err := rison.ToJSON([]byte(r), rison.Rison) fmt.Println(err.(*rison.ParseError).ErrorInLang("en")) fmt.Println(err.(*rison.ParseError).ErrorInLang("ja"))
Output: unmatched "!(" (at the end of string "!(" -> EOS) "!(" が閉じていません (場所: 文字列終端: "!(" → EOS)
func (*ParseError) Langs ¶ added in v2.3.0
func (e *ParseError) Langs() []string
Langs returns supported languages.