json

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// KoinosFieldOverrides implements overriding byte fields based on the Btype option enum
	KoinosFieldOverrides = map[protoreflect.Kind]protojson.FieldOverride{
		protoreflect.BytesKind: protojson.FieldOverride{
			MarshalField: func(val protoreflect.Value, fd protoreflect.FieldDescriptor, opt protojson.MarshalOptions) ([]byte, error) {
				var encodedStr string
				opts := fd.Options()

				if opts != nil {
					fieldOpts := opts.(*descriptorpb.FieldOptions)
					ext := koinos.E_Btype.TypeDescriptor()
					enum := fieldOpts.ProtoReflect().Get(ext).Enum()

					switch koinos.BytesType(enum) {
					case koinos.BytesType_HEX:
						fallthrough
					case koinos.BytesType_BLOCK_ID:
						fallthrough
					case koinos.BytesType_TRANSACTION_ID:
						encodedStr = "0x" + hex.EncodeToString(val.Bytes())
					case koinos.BytesType_BASE58:
						fallthrough
					case koinos.BytesType_CONTRACT_ID:
						fallthrough
					case koinos.BytesType_ADDRESS:
						encodedStr = base58.Encode(val.Bytes())
					case koinos.BytesType_BASE64:
						fallthrough
					default:
						encodedStr = base64.URLEncoding.EncodeToString(val.Bytes())
					}
				} else {
					encodedStr = base64.URLEncoding.EncodeToString(val.Bytes())
				}

				return gojson.Marshal(encodedStr)
			},
			UnmarshalField: func(b []byte, fd protoreflect.FieldDescriptor, opt protojson.UnmarshalOptions) (protoreflect.Value, error) {
				val := protoreflect.Value{}

				var decodedBytes []byte
				var encodedStr string
				err := gojson.Unmarshal(b, &encodedStr)
				if err != nil {
					return val, err
				}

				opts := fd.Options()
				if opts != nil {
					fieldOpts := opts.(*descriptorpb.FieldOptions)
					ext := koinos.E_Btype.TypeDescriptor()
					enum := fieldOpts.ProtoReflect().Get(ext).Enum()

					switch koinos.BytesType(enum) {
					case koinos.BytesType_HEX:
						fallthrough
					case koinos.BytesType_BLOCK_ID:
						fallthrough
					case koinos.BytesType_TRANSACTION_ID:
						{
							if len(encodedStr) < 2 || encodedStr[:2] != "0x" {
								return val, errors.New("hex string not prepended with '0x'")
							}

							decodedBytes, err = hex.DecodeString(encodedStr[2:])
						}
					case koinos.BytesType_BASE58:
						fallthrough
					case koinos.BytesType_CONTRACT_ID:
						fallthrough
					case koinos.BytesType_ADDRESS:
						{
							decodedBytes = base58.Decode(encodedStr)
							if len(decodedBytes) == 0 && len(encodedStr) != 0 {
								err = errors.New("error decoding base58")
							}
						}
					case koinos.BytesType_BASE64:
						fallthrough
					default:
						{
							decodedBytes, err = base64.URLEncoding.DecodeString(encodedStr)
						}
					}
				} else {
					decodedBytes, err = base64.URLEncoding.DecodeString(encodedStr)
				}

				return protoreflect.ValueOfBytes(decodedBytes), err
			},
		},
	}

	// KoinosMarshalOptions are the default Koinos JSON Marshal Options
	KoinosMarshalOptions = protojson.MarshalOptions{
		Multiline:      false,
		Indent:         "",
		UseProtoNames:  true,
		FieldOverrides: KoinosFieldOverrides,
	}

	// KoinosUnmarshalOptions are the default Koinos JSON Unmarshal Options
	KoinosUnmarshalOptions = protojson.UnmarshalOptions{
		DiscardUnknown: true,
		FieldOverrides: KoinosFieldOverrides,
	}
)
View Source
var File_koinos_json_json_test_objects_proto protoreflect.FileDescriptor

Functions

func Marshal

func Marshal(m proto.Message) ([]byte, error)

Marshal encodes to JSON using KoinosMarshalOptions

func Unmarshal

func Unmarshal(b []byte, m proto.Message) error

Unmarshal decods from JSON using KoinosUnmarshalOptions

Types

type BytesSerializationTestObject

type BytesSerializationTestObject struct {
	Base_64       []byte `protobuf:"bytes,1,opt,name=base_64,json=base64,proto3" json:"base_64,omitempty"`
	Base_58       []byte `protobuf:"bytes,2,opt,name=base_58,json=base58,proto3" json:"base_58,omitempty"`
	Hex           []byte `protobuf:"bytes,3,opt,name=hex,proto3" json:"hex,omitempty"`
	BlockId       []byte `protobuf:"bytes,4,opt,name=block_id,json=blockId,proto3" json:"block_id,omitempty"`
	TransactionId []byte `protobuf:"bytes,5,opt,name=transaction_id,json=transactionId,proto3" json:"transaction_id,omitempty"`
	ContractId    []byte `protobuf:"bytes,6,opt,name=contract_id,json=contractId,proto3" json:"contract_id,omitempty"`
	Address       []byte `protobuf:"bytes,7,opt,name=address,proto3" json:"address,omitempty"`
	Default       []byte `protobuf:"bytes,8,opt,name=default,proto3" json:"default,omitempty"`
	// contains filtered or unexported fields
}

func (*BytesSerializationTestObject) Descriptor deprecated

func (*BytesSerializationTestObject) Descriptor() ([]byte, []int)

Deprecated: Use BytesSerializationTestObject.ProtoReflect.Descriptor instead.

func (*BytesSerializationTestObject) GetAddress

func (x *BytesSerializationTestObject) GetAddress() []byte

func (*BytesSerializationTestObject) GetBase_58

func (x *BytesSerializationTestObject) GetBase_58() []byte

func (*BytesSerializationTestObject) GetBase_64

func (x *BytesSerializationTestObject) GetBase_64() []byte

func (*BytesSerializationTestObject) GetBlockId

func (x *BytesSerializationTestObject) GetBlockId() []byte

func (*BytesSerializationTestObject) GetContractId

func (x *BytesSerializationTestObject) GetContractId() []byte

func (*BytesSerializationTestObject) GetDefault

func (x *BytesSerializationTestObject) GetDefault() []byte

func (*BytesSerializationTestObject) GetHex

func (x *BytesSerializationTestObject) GetHex() []byte

func (*BytesSerializationTestObject) GetTransactionId

func (x *BytesSerializationTestObject) GetTransactionId() []byte

func (*BytesSerializationTestObject) ProtoMessage

func (*BytesSerializationTestObject) ProtoMessage()

func (*BytesSerializationTestObject) ProtoReflect

func (*BytesSerializationTestObject) Reset

func (x *BytesSerializationTestObject) Reset()

func (*BytesSerializationTestObject) String

Jump to

Keyboard shortcuts

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