How to solve this?

How to solve this ?

By "dbg subcommand" do you mean the dbg! macro? If so, that is designed for quick debugging during development, not for normal output of a program, and the format of its output shouldn't be relied on. Things like print! (also println!, write! etc.) should be used for most cases where you want to produce output.

You mentioned outputting JSON. Where are you obtaining the JSON from? Are you printing a particular type? Extra details like that may be helpful for guiding you on how to obtain the desired output and avoiding X-Y problems.

Also, please try to avoid putting text in images. It makes it harder to read on some devices, and it can't be selected or quoted.

As @jameseb7 mentions, using Debug implementations is not to be relied on for machine-based parsing. It's only meant to be used, as it's name indicates, for debugging.

That being said, we can perfectly envision a dbg! macro equivalent that uses the Serialize trait, and a Serializer / data format flavor, such as (serde_)json, and outputs it:

fn json_dbg<T> (value: T)
  -> T
where
    T : ::serde::Serialize,
{
    if let Ok(json) = ::serde_json::to_string_pretty(&value) {
        eprintln!("{}", json);
    } /* else { … } */
    value
}
  • Demo: let _: Point = json_dbg(Point { x: 42., y: 27. }); outputs:

    {
      "x": 42.0,
      "y": 27.0
    }
    

macro version (it being a macro is unnecessary and thus suboptimal)
macro_rules! json_dbg {( $value:expr ) => (
    match $value { value => {
        if let ::core::result::Result::Ok(json) =
            ::serde_json::to_string_pretty(&value)
        {
            ::std::eprintln!("{}", json);
        } /* else { … } */
        value
    }}
)}

2 Likes