All Projects → sakura-internet → go-rison

sakura-internet / go-rison

Licence: MIT License
Go port of Rison, a data serialization format optimized for compactness in URIs.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-rison

readtext
an R package for reading text files
Stars: ✭ 102 (+343.48%)
Mutual labels:  encoding
Lingo
Text encoding for modern C++
Stars: ✭ 28 (+21.74%)
Mutual labels:  encoding
gb-convert
Gameboy tile conversion and map editor tool
Stars: ✭ 26 (+13.04%)
Mutual labels:  encoding
7uring
An advanced cryptography tool for hashing, encrypting, encoding, steganography and more.
Stars: ✭ 15 (-34.78%)
Mutual labels:  encoding
avro ex
An Avro Library that emphasizes testability and ease of use.
Stars: ✭ 47 (+104.35%)
Mutual labels:  encoding
vcdiff
Heavily optimized .NET Core vcdiff library
Stars: ✭ 16 (-30.43%)
Mutual labels:  encoding
h264-roi
H.264 video Region of Interest encoding tool, using x264
Stars: ✭ 44 (+91.3%)
Mutual labels:  encoding
sia
Sia - Binary serialisation and deserialisation
Stars: ✭ 52 (+126.09%)
Mutual labels:  encoding
iconv
Fast encoding conversion library for Erlang / Elixir
Stars: ✭ 45 (+95.65%)
Mutual labels:  encoding
scram-cli
A command-line utility to encode messages. Python 3.
Stars: ✭ 13 (-43.48%)
Mutual labels:  encoding
deen
Generic data DEcoding/ENcoding application built with PyQt5.
Stars: ✭ 45 (+95.65%)
Mutual labels:  encoding
dark-lord-obama
AV-evading Pythonic Reverse Shell with Dynamic Adaption Capabilities
Stars: ✭ 61 (+165.22%)
Mutual labels:  encoding
posprinterdriver
Android ESC/POS Printer Driver (https://play.google.com/store/apps/details?id=com.fidelier.posprinterdriver) usage notes
Stars: ✭ 61 (+165.22%)
Mutual labels:  encoding
urdu-characters
📄 Complete collection of Urdu language characters & unicode code points.
Stars: ✭ 24 (+4.35%)
Mutual labels:  encoding
Base62
PHP Base62 encoder and decoder for integers and big integers with Laravel 5 support.
Stars: ✭ 16 (-30.43%)
Mutual labels:  encoding
harsh
Hashids implementation in Rust
Stars: ✭ 48 (+108.7%)
Mutual labels:  encoding
velvet-video
Java library for encoding / decoding / muxing / demuxing video and audio in various formats
Stars: ✭ 32 (+39.13%)
Mutual labels:  encoding
scure-base
Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
Stars: ✭ 27 (+17.39%)
Mutual labels:  encoding
babelfish
Universal translater for encodings
Stars: ✭ 13 (-43.48%)
Mutual labels:  encoding
staxrip
StaxRip is a video encoding app for Windows with a unrivaled feature set and usability.
Stars: ✭ 132 (+473.91%)
Mutual labels:  encoding

go-rison

CircleCI Go Report Card Godoc MIT License

Go port of Rison.

This page describes Rison, 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.

Examples

import "github.com/sakura-internet/go-rison/v4"

func ExampleDecode() {
	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]
}

// The object keys corresponding the struct fields can be
// specified in struct tag (not "rison" but) "json".
type exampleStruct struct {
	I int64       `json:"i"`
	F float64     `json:"f"`
	S string      `json:"s"`
	B bool        `json:"b"`
	P *bool       `json:"p"`
	A []int64     `json:"a"`
	X interface{} `json:"x"`
}

func ExampleUnmarshal() {
	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]}
}

func ExampleMarshal() {
	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 ExampleToJSON() {
	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 ExampleQuote() {
	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 ExampleParseError_ErrorInLang() {
	type multilingualError interface {
		error
		Langs() []string
		Translate(lang string)
	}

	r := "!("
	_, err := rison.ToJSON([]byte(r), rison.Rison)
	merr := err.(multilingualError)

	langs := merr.Langs()
	fmt.Println(strings.Join(langs, ", "))

	fmt.Println(merr.Error())
	merr.Translate("ja")
	fmt.Println(merr.Error())

	// Output:
	// en, ja
	// unmatched "!(" (at the end of string "!(" -> EOS)
	// "!(" が閉じていません (場所: 文字列終端: "!(" → EOS)
}

Descriptions

The following descriptions are some excerpts from the original README and the article:

Differences from JSON syntax

  • no whitespace is permitted except inside quoted strings.

  • almost all character escaping is left to the uri encoder.

  • single-quotes are used for quoting, but quotes can and should be left off strings when the strings are simple identifiers.

  • the e+ exponent format is forbidden, since + is not safe in form values and the plain e format is equivalent.

  • the E, E+, and E exponent formats are removed.

  • object keys should be lexically sorted when encoding. the intent is to improve url cacheability.

  • uri-safe tokens are used in place of the standard json tokens:

    rison token json token meaning
    ' " string quote
    ! \ string escape
    (...) {...} object
    !(...) [...] array
  • the JSON literals that look like identifiers (true, false and null) are represented as ! sequences:

    rison token json token
    !t true
    !f false
    !n null

The ! character plays two similar but different roles, as an escape character within strings, and as a marker for special values. This may be confusing.

Notice that services can distinguish Rison-encoded strings from JSON-encoded strings by checking the first character. Rison structures start with ( or !(. JSON structures start with [ or {. This means that a service which expects a JSON encoded object or array can accept Rison-encoded objects without loss of compatibility.

Interaction with URI %-encoding

Rison syntax is designed to produce strings that be legible after being form- encoded for the query section of a URI. None of the characters in the Rison syntax need to be URI encoded in that context, though the data itself may require URI encoding. Rison tries to be orthogonal to the %-encoding process - it just defines a string format that should survive %-encoding with very little bloat. Rison quoting is only applied when necessary to quote characters that might otherwise be interpreted as special syntax.

Note that most URI encoding libraries are very conservative, percent-encoding many characters that are legal according to RFC 3986. For example, Javascript's builtin encodeURIComponent() function will still make Rison strings difficult to read. The rison.js library includes a more tolerant URI encoder.

Rison uses its own quoting for strings, using the single quote (**'**) as a string delimiter and the exclamation point (**!**) as the string escape character. Both of these characters are legal in uris. Rison quoting is largely inspired by Unix shell command line parsing.

All Unicode characters other than **'** and **!** are legal inside quoted strings. This includes newlines and control characters. Quoting all such characters is left to the %-encoding process.

Grammar

modified from the json.org grammar.

  • object
    • ()
    • ( members )
  • members
    • pair
    • pair , members
  • pair
    • key : value
  • array
    • !()
    • !( elements )
  • elements
    • value
    • value , elements
  • key
    • id
    • string
  • value
    • id
    • string
    • number
    • object
    • array
    • !t
    • !f
    • !n
          ────────────
  • id
    • idstart
    • idstart idchars
  • idchars
    • idchar
    • idchar idchars
  • idchar
    • any alphanumeric ASCII character
    • any ASCII character from the set - _ . / ~
    • any non-ASCII Unicode character
  • idstart
    • any idchar not in -, digit
          ────────────
  • string
    • ''
    • ' strchars '
  • strchars
    • strchar
    • strchar strchars
  • strchar
    • any Unicode character except ASCII ' and !
    • !!
    • !'
          ────────────
  • number
    • int
    • int frac
    • int exp
    • int frac exp
  • int
    • digit
    • digit1-9 digits
    • - digit
    • - digit1-9 digits
  • frac
    • . digits
  • exp
    • e digits
  • digits
    • digit
    • digit digits
  • e
    • e
    • e-
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].