+
Skip to content

feat: Handle json.RawMessage #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

daenney
Copy link
Contributor

@daenney daenney commented Mar 26, 2025

Without special handling, json.RawMessage is rendered as []uint8 which isn't very easy to decode. Since json.RawMessage always holds JSON text, this prints it instead so you can see the value instead.

Without special handling, json.RawMessage is rendered as []uint8 which
isn't very easy to decode. Since json.RawMessage always holds JSON text,
this prints it instead so you can see the value instead.
@daenney
Copy link
Contributor Author

daenney commented Mar 26, 2025

For example, without this patch:

  Value: []uint8{
    34,
    112,
    108,
    97,
    105,
    110,
    32,
    108,
    105,
    116,
    101,
    114,
    97,
    108,
    34,
  },

With the patch:

Value: "plain literal",

repr.go Outdated
@@ -193,6 +195,11 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent
}
t := v.Type()

if t == rawJSONType {
fmt.Fprintf(p.w, "%s", v.Bytes())
Copy link
Owner

@alecthomas alecthomas Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the idea of representing json.RawMessage differently, but I don't think this is what we want. This will make it impossible to copy+paste the output of repr and have it usable, which is one of the goals of this package.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want something like:

fmt.Fprintf(p.w, "json.RawMessage(%q)", v.Bytes())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good point. I forgot about the copy-paste part. I'll fix it up later today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for json.RawMessage(%s) instead, as it gives easier output.

If you have a string with json.RawMessage(%q) you get json.RawMessage(\"\\\"string\\\"\") since the quotes need to be escaped. With the backtick wrapped %s formatter you avoid that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, in my example, you now get:

Value: json.RawMessage(`"plain literal"`),

Whereas with %q you'd get:

Value: json.RawMessage("\"plain literal\""),

@@ -193,6 +195,11 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent
}
t := v.Type()

if t == rawJSONType {
fmt.Fprintf(p.w, "json.RawMessage(`%s`)", v.Bytes())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is not going to suffice - if the string contains a backtick, this will output invalid Go code.

@alecthomas alecthomas closed this Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载