(m proto.Message)
| 84 | } |
| 85 | |
| 86 | func (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 | |
| 122 | type jsonWriter struct { |
| 123 | *Marshaler |
no test coverage detected