json2msgp

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2020 License: Apache-2.0 Imports: 11 Imported by: 4

README

json2msgp: heuristic recursive self-describing message transformation

JSON is a well-known human-readable self-describing message format. MsgPack is a well-known binary self-describing message format.

It is reasonably straightforward to transform arbitrary MsgPack data into JSON; there are library methods available to perform this conversion.

However, the reverse operation is not well-supported. This is because the MSGP type system is a superset of JSON's. In particular, MSGP supports byte arrays, and JSON does not. Typical conversions from MSGP to JSON, such as the one linked above, will transform byte arrays into base64-encoded strings. This works well, but when moving from JSON to MSGP, there is no way to know with certainty whether a given string is intended to be a string or a byte array.

This library uses a very simple heuristic to make that decision:

  • if the JSON string is not valid utf-8, it is passed through as a byte array without modification.
  • if the JSON string is valid padded base64 in the standard encoding, it is decoded and represented in the MSGP as a byte array.
  • otherwise, it is assumed to be a string, and represented as a string.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(in interface{}, typeHints map[string][]string) ([]byte, error)

Convert recursively converts the input object into a MSGP representation.

Strings are converted using the following heuristic:

- if the string is not valid utf-8, it is passed through as a byte array without modification. - if the string is a valid ndau address, it's represented as a string. - if the string is valid padded base64 in the standard encoding, it is decoded and represented in the MSGP as a byte array. - otherwise, it is assumed to be a string, and represented as a string.

This is primarily intended to assist conversion from JSON to MSGP, so certain conversions such as structs are intentionally excluded. If you have a struct, use `msgp.Marshal` directly.

func ConvertStream

func ConvertStream(in io.Reader, out io.Writer, typeHints map[string][]string) error

ConvertStream reads JSON from `in` and copies it as MSGP to `out` until EOF.

Strings are converted using the following heuristic:

- if the string is not valid utf-8, it is passed through as a byte array without modification. - if the string is a valid ndau address, it's represented as a string. - if the string is valid padded base64 in the standard encoding, it is decoded and represented in the MSGP as a byte array. - otherwise, it is assumed to be a string, and represented as a string.

Numeric valuse need extra help to know their types. Use the typeHints map for that.

  • if all "Fee" variables are to be encoded as int64, and "ChangeOn" as uint64, then use: typeHints = {"Fee": []string{"int64"}, "ChangeOn": []string{"uint64"}}
  • if there are blobs of json without names, yet there are arrays of differing numeric types, such as: [[0,1],[-2,3],[4,5]], then use: typeHints = {"": []string{"int64", "uint64"}}

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

Converter manages state during the converstion process.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL