Documentation ¶
Overview ¶
Package jcs implements JSON Canonicalization Scheme, formalized as RFC8785:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInf = errors.New("jcs: cannot c14n Inf")
ErrInf indicates that the inputted value is or contains infinity or negative infinity.
var ErrNaN = errors.New("jcs: cannot c14n NaN")
ErrNaN indicates that the inputted value is or contains NaN.
var ErrUnsupportedType = errors.New("jcs: value has unsupported type")
ErrUnsupportedType indicates the inputted value is or contains a type outside the set of types described in Append.
Functions ¶
func Append ¶
Append appends the JCS canonical representation of the value v to b and returns the extended buffer.
Append only supports the types used by encoding/json to unmarshal into an interface value, namely:
bool, for JSON booleans float64, for JSON numbers string, for JSON strings []interface{}, for JSON arrays map[string]interface{}, for JSON objects nil for JSON null
Any other types, including types embedded within maps or arrays in the value v, will result in Append returning ErrUnsupportedType.
Furthermore, if v contains float64 values that are NaN, Infinity, or negative Infinity, then Append will return either ErrNaN or ErrInf.
Append does not check if strings contain invalid surrogate pairs. Codepoints in strings will be appended to b as-is, with the appropriate escaping required by JCS.
func Format ¶
Format returns the JCS canonical representation of the given value.
See Append for the types of data supported, and possible error conditions.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/ucarion/jcs" ) func main() { input := `{"z": [1, 2, 3], "a": "<foo>" }` var v interface{} if err := json.Unmarshal([]byte(input), &v); err != nil { panic(err) } // This is not going to output JCS canonical JSON, because of quirks in // encoding/json's output format. outNaive, _ := json.Marshal(v) fmt.Println(string(outNaive)) out, _ := jcs.Format(v) fmt.Println(out) }
Output: {"a":"\u003cfoo\u003e","z":[1,2,3]} {"a":"<foo>","z":[1,2,3]}
Types ¶
This section is empty.