Skip to content

regexp: support encoding.TextMarshaler #46159

Closed
@flimzy

Description

@flimzy

What did you do?

Marshaling a compiled regular expression, for example as part of a JSON object, results in an empty object. Example:

re := regexp.MustCompile("foo?")
data, _ := json.Marshal(map[string]interface{}{"foo":re})
fmt.Println(string(data))

Produces:

{"foo":{}}

(Playground)

What did you expect to see?

It would seem more intuitive for a compiled regexp to marshal to its string representation, as if .String() were called. Example:

{"foo":"foo?"}

I propose we implement the encoding.TextMarshaler interface to return the value produced by the String() method.

Strictly speaking, this would be a breaking change, so may need to be targeted to Go 2. On the other hand, in a practical sense, the current behavior is completely useless, so anyone depending on the current behavior probably already has a bug in their program. 🤷‍♂️

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions