go.temporal.io/server/common/codec/jsonpb.go
147 LOC · 66 covered · 81 uncovered · 19 ranges · 787 concepts · 10 introducers · 412 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package codec
import (
"bytes"
"encoding/json"
"fmt"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/api/temporalproto"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type (
// JSONPBEncoder is JSON encoder/decoder for protobuf structs and slices of protobuf structs.
// This is an wrapper on top of jsonpb.Marshaler which supports not only single object serialization
// but also slices of concrete objects.
JSONPBEncoder struct {
marshaler protojson.MarshalOptions
unmarshaler temporalproto.CustomJSONUnmarshalOptions
}
)
// NewJSONPBEncoder creates a new JSONPBEncoder.
return JSONPBEncoder{}
}
// NewJSONPBIndentEncoder creates a new JSONPBEncoder with indent.
return JSONPBEncoder{
marshaler: protojson.MarshalOptions{
Indent: indent,
},
}
}
// Encode protobuf struct to bytes.
return e.marshaler.Marshal(pb)
}
// Decode bytes to protobuf struct.
return e.unmarshaler.Unmarshal(data, pb)
}
// Encode HistoryEvent slice to bytes.
func (e *JSONPBEncoder) EncodeHistoryEvents(historyEvents []*historypb.HistoryEvent) ([]byte, error) {
proto_decoder.go ×19
return e.encodeSlice(
len(historyEvents),
func(i int) proto.Message { return historyEvents[i] })
}
// Encode History slice to bytes.
func (e *JSONPBEncoder) EncodeHistories(histories []*historypb.History) ([]byte, error) {
jsonpb.go ×1
return e.encodeSlice(
len(histories),
func(i int) proto.Message { return histories[i] })
}
// Decode HistoryEvent slice from bytes.
func (e *JSONPBEncoder) DecodeHistoryEvents(data []byte) ([]*historypb.HistoryEvent, error) {
var historyEvents []*historypb.HistoryEvent
err := e.DecodeSlice(
data,
func() proto.Message {
historyEvent := &historypb.HistoryEvent{}
historyEvents = append(historyEvents, historyEvent)
return historyEvent
})
return historyEvents, err
}
// Decode History slice from bytes.
func (e *JSONPBEncoder) DecodeHistories(data []byte) ([]*historypb.History, error) {
jsonpb.go ×8
var histories []*historypb.History
err := e.DecodeSlice(
data,
func() proto.Message {
history := &historypb.History{}
histories = append(histories, history)
return history
})
}
// Due to the lack of generics in go
// this function accepts callback which should return particular item by it index.
func (e *JSONPBEncoder) encodeSlice(
len int,
item func(i int) proto.Message,
var buf bytes.Buffer
buf.WriteString("[")
for i := range len {
bs, err := e.marshaler.Marshal(pb)
if err != nil {
return nil, err
}
if i < len-1 {
}
}
return buf.Bytes(), nil
}
// constructor callback must create empty object, add it to result slice, and return it.
func (e *JSONPBEncoder) DecodeSlice(
data []byte,
dec := json.NewDecoder(bytes.NewReader(data))
tok, err := dec.Token()
if err != nil {
return err
}
return fmt.Errorf("invalid json: expected [ but found %v", tok)
}
// We need DiscardUnknown here as the history json may have been written by a
// different proto revision
DiscardUnknown: true,
}
var buf json.RawMessage
for dec.More() {
if err := dec.Decode(&buf); err != nil {
return err
}
if err := unmarshaller.Unmarshal([]byte(buf), pb); err != nil {
return err
}
}
}