diff --git a/jsonencoder/encoder.go b/jsonencoder/encoder.go index 744ddb2..4b912e1 100644 --- a/jsonencoder/encoder.go +++ b/jsonencoder/encoder.go @@ -11,26 +11,21 @@ import ( type Encoder struct { protoRegistry *protoregistry.Registry - marshallers []*json.Marshalers } func New(files *protoregistry.Registry) *Encoder { - e := &Encoder{ + return &Encoder{ protoRegistry: files, } - e.marshallers = []*json.Marshalers{ - json.MarshalFuncV2(e.anypb), - } - return e } func (e *Encoder) Marshal(in any) error { - return json.MarshalEncode(jsontext.NewEncoder(os.Stdout), in, json.WithMarshalers(json.NewMarshalers(e.marshallers...))) + return json.MarshalEncode(jsontext.NewEncoder(os.Stdout), in, json.WithMarshalers(e.getMarshallers(""))) } func (e *Encoder) MarshalToString(in any) (string, error) { buf := bytes.NewBuffer(nil) - if err := json.MarshalEncode(jsontext.NewEncoder(buf), in, json.WithMarshalers(json.NewMarshalers(e.marshallers...))); err != nil { + if err := json.MarshalEncode(jsontext.NewEncoder(buf), in, json.WithMarshalers(e.getMarshallers(""))); err != nil { return "", err } return buf.String(), nil diff --git a/jsonencoder/marshallers.go b/jsonencoder/marshallers.go index 2d504b6..d338c3a 100644 --- a/jsonencoder/marshallers.go +++ b/jsonencoder/marshallers.go @@ -19,8 +19,8 @@ func (e *Encoder) anypb(encoder *jsontext.Encoder, t *anypb.Any, options json.Op if err != nil { return fmt.Errorf("unmarshalling proto any: %w", err) } - setBytesEncoder(t.TypeUrl) - cnt, err := json.Marshal(msg, json.WithMarshalers(json.NewMarshalers(e.marshallers...))) + + cnt, err := json.Marshal(msg, json.WithMarshalers(e.getMarshallers(t.TypeUrl))) if err != nil { return fmt.Errorf("json marshalling proto any: %w", err) } @@ -35,10 +35,18 @@ func (e *Encoder) hexBytes(encoder *jsontext.Encoder, t []byte, options json.Opt return encoder.WriteToken(jsontext.String(hex.EncodeToString(t))) } -func setBytesEncoder(typeURL string) { +func (e *Encoder) getMarshallers(typeURL string) *json.Marshalers { + out := []*json.Marshalers{ + json.MarshalFuncV2(e.anypb), + } + if strings.Contains(typeURL, "solana") { dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsBase58) - return + out = append(out, json.MarshalFuncV2(e.base58Bytes)) + return json.NewMarshalers(out...) } + dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsHex) + out = append(out, json.MarshalFuncV2(e.hexBytes)) + return json.NewMarshalers(out...) }