serializer

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer an implementation of TransactionSerializer for handling conversion of to and from JSON string formats into usable values for chaincode. It is the default serializer for chaincode created by NewChaincode

func (*JSONSerializer) FromString

func (js *JSONSerializer) FromString(param string, fieldType reflect.Type, paramMetadata *metadata.ParameterMetadata, components *metadata.ComponentMetadata) (reflect.Value, error)

FromString takes a parameter and converts it to a reflect value representing the goal data type. If a parameter metadata is passed it will validate that the converted value meets the rules specified by that parameter's compiled schema. For complex data structures e.g. structs, arrays etc. the string value passed should be in JSON format. The default Go JSON unmarshaller is used for converting complex types and as such it does not respect private properties or properties using the api metadata tag by default. If you use either of these in your struct and expect data passed in to use these then you should write your own unmarshall function to handle this for your struct. Docs on how the Go JSON Unmarshaller works: https://golang.org/pkg/encoding/json/ For date-time types strings should be passed in RFC3339 format.

func (*JSONSerializer) ToString

func (js *JSONSerializer) ToString(result reflect.Value, resultType reflect.Type, returns *metadata.ReturnMetadata, components *metadata.ComponentMetadata) (string, error)

ToString takes a reflect value, the type of what the value originally was. If a non nil return metadata is supplied it will compare the value against the compiled schema and validate it matches the rules set out. Since it uses the compiled schema components is not used. It returns a string representation of the original value, complex types such as structs, arrays etc are returned in a JSON format. Structs, Arrays, Slices and Maps use the default JSON marshaller for creating the string. For structs this will therefore not include private properties (even if tagged with metadata) in the string or use the metadata tag value for the property name in the produced string by default. To include these within the string whilst using this serializer you should write a custom Marshall function on your struct Docs on how the Go JSON Marshaller works: https://golang.org/pkg/encoding/json/ For date-time types the resulting string will meet the RFC3339 format

type TransactionSerializer

type TransactionSerializer interface {
	// FromString receives the value in its original string form, the reflected type that the
	// new value should be of, the schema defining the rules that the converted value should
	// adhere to and components which the schema may point to as a reference. The schema and
	// component metadata may be nil. The function should produce a reflect value which matches
	// the goal type.
	FromString(string, reflect.Type, *metadata.ParameterMetadata, *metadata.ComponentMetadata) (reflect.Value, error)

	// ToString receives a reflected value of a value, the reflected type of that that value was
	// originally, the schema defining the rules of what that value should meet and components
	// which the schema may point to as a reference. The schema and component metadata may be nil
	// The function should produce a string which represents the original value
	ToString(reflect.Value, reflect.Type, *metadata.ReturnMetadata, *metadata.ComponentMetadata) (string, error)
}

TransactionSerializer defines the functions a valid transaction serializer should have. Serializers to be used by a chaincode must implement this interface. Serializers are called on calls to chaincode with FromString used to format arguments going in to a call and ToString to format the success response from a call

Jump to

Keyboard shortcuts

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