serde_json::Value depict Strings incorrect

Why does serde_json::Value puts a String into a depiction like String("Some string") and not like "Some string" or a string literal like Some string? Is there a way to prevent this except using structs?

Object {
        "apktool_out/lib/arm64-v8a/libqml_QtQuick_Controls_Fusion_impl_qtquickcontrols2fusionstyleimplplugin_arm64-v8a.so": Array [
            String("QColor"),
            String("QML.AddedInVersion"),
            String("QJSManagedValue"),
            String("highlight"),
[...]

You seem to be using the debug representation, and serde_json:: Value is an enum, which means that the enum variant will be printed out.

2 Likes

Ah ok, so I just have to change the representation

Yes, you can use serde_json::to_string for that, or one of the other functions here.

serde_json::Value implements Display, so there's no need to serialize it.

4 Likes

Presentation of the debug format is not guaranteed. If you want data printed it a particular way, you have to implement it yourself.

3 Likes

or just use Display? or use an existing crate that implements the printing logic?

generally, telling someone to implement json serialization manually is bad advice.

2 Likes

I'm quite certain this is not what kornel suggested. Rather, changing the implementation of Debug in a way that changes the output is generally not considered a breaking change, whereas changing Display in that way is (at least as far as I remember, that is the policy of the standard library, I don't think this was part of the API evolution RFC). So you shouldn't use the Debug output for any data processing beyond debugging.

Changing Display in std is sometimes considered breaking, but sometimes isn't. There are some practical limits depending on how hard the ecosystem relies on a certain output, of course.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.