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 ErrType
- 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 ErrType ¶
type ErrType int
ErrType is an enum type of error
const ( // EInternal is an error indicating an internal error occurred. EInternal ErrType = iota // EEncoding is an error indicating encoding failed. EEncoding // EEmptyString is an error indicating the string is empty. EEmptyString // EUnmatchedPair is an error indicating characters such as parentheses are not paired. EUnmatchedPair // EMissingCharacter is an error indicating necessary characters are missing. EMissingCharacter // EMissingCharacterAfterEscape is an error indicating there is no character after the escape character. EMissingCharacterAfterEscape // EExtraCharacter is an error indicating extra characters. EExtraCharacter // EExtraCharacterAfterRison is an error indicating there are extra characters after valid Rison. EExtraCharacterAfterRison // EInvalidLiteral is an error indicating an invalid literal was found. EInvalidLiteral // EInvalidCharacter is an error indicating an invalid character was found. EInvalidCharacter // EInvalidTypeOfObjectKey is an error indicating an invalid type object key was found. EInvalidTypeOfObjectKey // EInvalidStringEscape is an error indicating an invalid string escape was found. EInvalidStringEscape // EInvalidNumber is an error indicating an invalid number was found. EInvalidNumber // EInvalidLargeExp is an error indicating an upper case "E" is used as an exponent. EInvalidLargeExp )
type Mode ¶
type Mode int
Mode is an enum type to specify which Rison variation to use to encode/decode.
type ParseError ¶
type ParseError struct { Child error Type ErrType Args []interface{} Src []byte Pos int // contains filtered or unexported fields }
ParseError is an error type to be raised by parser
func (*ParseError) Error ¶
func (e *ParseError) Error() string
func (*ParseError) ErrorInLang ¶
func (e *ParseError) ErrorInLang(lang string) string
ErrorInLang returns the error message in specified language.
Example ¶
r := "!(" _, err := rison.ToJSON([]byte(r), rison.Rison) e, _ := err.(interface { ErrorInLang(lang string) string Langs() []string }) fmt.Println(strings.Join(e.Langs(), ", ")) fmt.Println(e.ErrorInLang("en")) fmt.Println(e.ErrorInLang("ja"))
Output: en, ja unmatched "!(" (at the end of string "!(" -> EOS) "!(" が閉じていません (場所: 文字列終端: "!(" → EOS)
func (*ParseError) Translate ¶
func (e *ParseError) Translate(lang string)
Translate sets the language of the error message to be retrieved by Error().