Skip to content

std/encoding/protojson

import "github.com/eugenioenko/goalchemy/std/encoding/protojson"

Package protojson encodes and decodes proto3 JSON for messages generated by protoc-gen-goalchemy, matching google.golang.org/protobuf/encoding/protojson. Marshal output equals protojson.Marshal’s after removing insignificant whitespace: fields in declaration order, zero values of implicit-presence fields omitted, 64-bit integers as strings, enums by name and map keys sorted. Unmarshal follows protojson.UnmarshalOptions{DiscardUnknown: true}, the options Connect clients use: unknown fields and unknown enum names are ignored, and either the JSON name or the proto name selects a field.

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

Marshal returns the proto3 JSON encoding of m.

func Register(names []string, newMessage func(name string) Message)

Register makes generated messages known to Any by full name. Generated packages call it from init.

func SortInt64s(keys []int64) []int64

SortInt64s sorts signed integer map keys in protojson order.

func SortUint64s(keys []uint64) []uint64

SortUint64s sorts unsigned integer map keys in protojson order.

func SortedKeys(keys []string) []string

SortedKeys returns the keys of a string-keyed map in protojson order.

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

Unmarshal decodes proto3 JSON into m, which it does not reset first.

func UnmarshalValue(v jsonvalue.Value, m Message) error

UnmarshalValue decodes an already parsed proto3 JSON value into m.

type Any struct {
TypeURL string
Fields jsonvalue.Value
}

Any is google.protobuf.Any. Fields holds the JSON members that follow “@type”: the embedded message’s fields, or the single member “value” for a well-known type. Payloads of registered generated messages and of the well-known types are kept in canonical form; other payloads are kept as received. Use NewAny and UnmarshalTo to convert typed messages.

func NewAny(typeURL string, m Message) (*Any, error)

NewAny packs m under typeURL, for example “type.googleapis.com/policy.Attribute”.

func (x *Any) DecodeProtoJSON(d *Decoder, v jsonvalue.Value)

DecodeProtoJSON implements Message.

func (x *Any) EncodeProtoJSON(e *Encoder)

EncodeProtoJSON implements Message.

func (x *Any) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (x *Any) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (x *Any) UnmarshalTo(m Message) error

UnmarshalTo decodes the embedded message into m.

type Decoder struct {
}

Decoder walks parsed JSON for generated code. The first error is kept and later reads return zero values.

func (d *Decoder) Array(v jsonvalue.Value, field string) bool

Array reports whether v is an array, failing otherwise.

func (d *Decoder) Bool(v jsonvalue.Value, field string) bool

Bool decodes a bool field.

func (d *Decoder) Bytes(v jsonvalue.Value, field string) []byte

Bytes decodes a bytes field from standard or URL-safe base64, with or without padding.

func (d *Decoder) Duplicate(name string)

Duplicate fails because a field appeared under both of its names.

func (d *Decoder) Enum(v jsonvalue.Value, field string, values map[string]int32) (n int32, ok bool)

Enum decodes an enum field from its value name or number. ok is false for an unknown name, which proto3 JSON with DiscardUnknown ignores.

func (d *Decoder) Err() error

Err returns the first decoding error.

func (d *Decoder) Fail(msg string)

Fail records msg as the decoding error unless one is already set.

func (d *Decoder) Float32(v jsonvalue.Value, field string) float32

Float32 decodes a float field.

func (d *Decoder) Float64(v jsonvalue.Value, field string) float64

Float64 decodes a double field.

func (d *Decoder) Int32(v jsonvalue.Value, field string) int32

Int32 decodes an int32, sint32 or sfixed32 field from a number or string.

func (d *Decoder) Int64(v jsonvalue.Value, field string) int64

Int64 decodes an int64, sint64 or sfixed64 field from a number or string.

func (d *Decoder) ListValue(v jsonvalue.Value, field string) jsonvalue.Value

ListValue decodes a google.protobuf.ListValue field.

func (d *Decoder) MapKeyBool(key string, field string) bool

MapKeyBool decodes a bool map key.

func (d *Decoder) MapKeyInt64(key string, bitSize int, field string) int64

MapKeyInt64 decodes a signed integer map key.

func (d *Decoder) MapKeyUint64(key string, bitSize int, field string) uint64

MapKeyUint64 decodes an unsigned integer map key.

func (d *Decoder) NullElement(field string)

NullElement fails on a null list element or map value of a non-Value type.

func (d *Decoder) Object(v jsonvalue.Value, message string) bool

Object reports whether v is an object, failing otherwise.

func (d *Decoder) OneofSet(name, oneof string)

OneofSet fails because a second member of the same oneof appeared.

func (d *Decoder) String(v jsonvalue.Value, field string) string

String decodes a string field.

func (d *Decoder) Struct(v jsonvalue.Value, field string) jsonvalue.Value

Struct decodes a google.protobuf.Struct field.

func (d *Decoder) Uint32(v jsonvalue.Value, field string) uint32

Uint32 decodes a uint32 or fixed32 field from a number or string.

func (d *Decoder) Uint64(v jsonvalue.Value, field string) uint64

Uint64 decodes a uint64 or fixed64 field from a number or string.

func (d *Decoder) Value(v jsonvalue.Value, field string) jsonvalue.Value

Value decodes a google.protobuf.Value field; JSON null is a set value.

type Duration struct {
Seconds int64
Nanos int32
}

Duration is google.protobuf.Duration, encoded as decimal seconds with an “s” suffix.

func DurationOf(d time.Duration) *Duration

DurationOf returns the Duration for d.

func (x *Duration) AsDuration() time.Duration

AsDuration returns the span as a time.Duration, saturating on overflow.

func (x *Duration) DecodeProtoJSON(d *Decoder, v jsonvalue.Value)

DecodeProtoJSON implements Message.

func (x *Duration) EncodeProtoJSON(e *Encoder)

EncodeProtoJSON implements Message.

func (x *Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (x *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Empty struct {
}

Empty is google.protobuf.Empty, encoded as {}.

func (x *Empty) DecodeProtoJSON(d *Decoder, v jsonvalue.Value)

DecodeProtoJSON implements Message.

func (x *Empty) EncodeProtoJSON(e *Encoder)

EncodeProtoJSON implements Message.

func (x *Empty) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (x *Empty) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Encoder struct {
}

Encoder accumulates one JSON document. Generated code drives it; the first error is kept and later writes are ignored.

func (e *Encoder) BeginArray()

BeginArray starts a JSON array value.

func (e *Encoder) BeginObject()

BeginObject starts a JSON object value.

func (e *Encoder) Bool(b bool)

Bool writes b.

func (e *Encoder) Bytes(b []byte)

Bytes writes b as padded standard base64.

func (e *Encoder) EndArray()

EndArray closes the innermost array.

func (e *Encoder) EndObject()

EndObject closes the innermost object.

func (e *Encoder) Enum(n int32, name string)

Enum writes name, or n when the value has no name.

func (e *Encoder) Err() error

Err returns the first encoding error.

func (e *Encoder) Fail(msg string)

Fail records msg as the encoding error unless one is already set.

func (e *Encoder) Float32(f float32)

Float32 writes f as a JSON number, or “NaN”, “Infinity” or “-Infinity”.

func (e *Encoder) Float64(f float64)

Float64 writes f as a JSON number, or “NaN”, “Infinity” or “-Infinity”.

func (e *Encoder) Int32(n int32)

Int32 writes n as a JSON number.

func (e *Encoder) Int64(n int64)

Int64 writes n as a JSON string.

func (e *Encoder) Key(name string)

Key writes an object member name; the member’s value follows.

func (e *Encoder) ListValue(v jsonvalue.Value)

ListValue writes v, which must be an array, as google.protobuf.ListValue.

func (e *Encoder) Message(m Message)

Message writes m, or {} when m is nil.

func (e *Encoder) Null()

Null writes null.

func (e *Encoder) String(s string)

String writes s, which must be valid UTF-8.

func (e *Encoder) Struct(v jsonvalue.Value)

Struct writes v, which must be an object, as google.protobuf.Struct.

func (e *Encoder) Uint32(n uint32)

Uint32 writes n as a JSON number.

func (e *Encoder) Uint64(n uint64)

Uint64 writes n as a JSON string.

func (e *Encoder) Value(v jsonvalue.Value)

Value writes v as google.protobuf.Value: objects as Structs with sorted keys and numbers as doubles.

type Error struct {
Msg string
}

Error describes an invalid value found while encoding or decoding.

func (e *Error) Error() string
type FieldMask struct {
Paths []string
}

FieldMask is google.protobuf.FieldMask, encoded as comma-separated lowerCamelCase paths.

func (x *FieldMask) DecodeProtoJSON(d *Decoder, v jsonvalue.Value)

DecodeProtoJSON implements Message.

func (x *FieldMask) EncodeProtoJSON(e *Encoder)

EncodeProtoJSON implements Message.

func (x *FieldMask) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (x *FieldMask) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Message interface {
EncodeProtoJSON(e *Encoder)
DecodeProtoJSON(d *Decoder, v jsonvalue.Value)
}

Message is implemented by generated messages and the well-known types.

type NullValue int32

NullValue is google.protobuf.NullValue, encoded as null.

const NullValue_NULL_VALUE NullValue = 0

NullValue_NULL_VALUE is the only NullValue.

type Timestamp struct {
Seconds int64
Nanos int32
}

Timestamp is google.protobuf.Timestamp, encoded as an RFC 3339 string.

func TimestampOf(t time.Time) *Timestamp

TimestampOf returns the Timestamp for t.

func (t *Timestamp) AsTime() time.Time

AsTime returns the instant as a UTC time.Time.

func (t *Timestamp) DecodeProtoJSON(d *Decoder, v jsonvalue.Value)

DecodeProtoJSON implements Message.

func (t *Timestamp) EncodeProtoJSON(e *Encoder)

EncodeProtoJSON implements Message.

func (t *Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (t *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.