MCPcopy
hub / github.com/golang/protobuf / marshal

Method marshal

jsonpb/encode.go:86–120  ·  jsonpb/encode.go::Marshaler.marshal
(m proto.Message)

Source from the content-addressed store, hash-verified

84}
85
86func (jm *Marshaler) marshal(m proto.Message) ([]byte, error) {
87 v := reflect.ValueOf(m)
88 if m == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
89 return nil, errors.New("Marshal called with nil")
90 }
91
92 // Check for custom marshalers first since they may not properly
93 // implement protobuf reflection that the logic below relies on.
94 if jsm, ok := m.(JSONPBMarshaler); ok {
95 return jsm.MarshalJSONPB(jm)
96 }
97
98 if wrapJSONMarshalV2 {
99 opts := protojson.MarshalOptions{
100 UseProtoNames: jm.OrigName,
101 UseEnumNumbers: jm.EnumsAsInts,
102 EmitUnpopulated: jm.EmitDefaults,
103 Indent: jm.Indent,
104 }
105 if jm.AnyResolver != nil {
106 opts.Resolver = anyResolver{jm.AnyResolver}
107 }
108 return opts.Marshal(proto.MessageReflect(m).Interface())
109 } else {
110 // Check for unpopulated required fields first.
111 m2 := proto.MessageReflect(m)
112 if err := protoV2.CheckInitialized(m2.Interface()); err != nil {
113 return nil, err
114 }
115
116 w := jsonWriter{Marshaler: jm}
117 err := w.marshalMessage(m2, "", "")
118 return w.buf, err
119 }
120}
121
122type jsonWriter struct {
123 *Marshaler

Callers 2

MarshalMethod · 0.95
MarshalToStringMethod · 0.95

Implementers 6

TextMarshalerproto/text_encode.go
Bufferproto/buffer.go
InternalMessageInfoproto/deprecated.go
fakeMarshalerproto/proto_test.go
nonptrMessageproto/proto_test.go
Marshalerjsonpb/encode.go

Calls 5

marshalMessageMethod · 0.95
InterfaceMethod · 0.80
MarshalJSONPBMethod · 0.65
MarshalMethod · 0.65
NewMethod · 0.45

Tested by

no test coverage detected