Skip to content
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

JSON: Field names should account for r# #3052

Open
fosskers opened this issue Aug 9, 2024 · 4 comments
Open

JSON: Field names should account for r# #3052

fosskers opened this issue Aug 9, 2024 · 4 comments

Comments

@fosskers
Copy link

fosskers commented Aug 9, 2024

Bug Report

Version

0.3.18

Platform

Linux caddi 6.10.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Tue, 06 Aug 2024 07:21:19 +0000 x86_64 GNU/Linux

Description

Rust allows us to give struct fields the same name as reserved keywords, such as type, if we prefix that name with r#, as shown here:

struct Thing {
    amount: u32,
    r#type: String,
}

When serializing this into JSON via serde, we see the r# ignored in the output:

#[derive(Debug, serde::Serialize)]
struct Thing {
    amount: u32,
    r#type: String,
}

fn main() {
    let c = Thing {
        amount: 1,
        r#type: "hi".to_string(),
    };

    let json = serde_json::to_string(&c).unwrap();
    println!("{json}");
}

{"amount":1,"type":"hi"}

However, the JSON support within tracing-subscriber does not do this, instead rendering the field name as r#type.

Suggestion: it should account for any field names that start with r# and avoid rendering that piece.

@mladedav
Copy link
Contributor

mladedav commented Aug 9, 2024

The JSON subscriber uses serde internally so the output should be the same.

Are you maybe using valuable? That would change how the serialization works and I've noticed you were using it in other recent issues.

@fosskers
Copy link
Author

fosskers commented Aug 9, 2024

Yes, using valuable in this case. Is there a way to not use valuable and have the Serialize impl just do everything?

@mladedav
Copy link
Contributor

mladedav commented Aug 9, 2024

I don't think so, you'd need a copy of the fmt subscriber without the valuable feature.

But in this case I think this should be fixed in valuable itself.

@fosskers
Copy link
Author

fosskers commented Aug 9, 2024

So it might not be how the tracing-subscriber JSON formatter is rendering the field name, but how valuable is reporting the field name to begin with?

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

No branches or pull requests

2 participants