JSON serialization string type data fails to serialize

JSON serialization string type data fails to serialize using serde_json library
Below is a data type

[DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 64, path: "/mnt/md0/data/cache/s11/sc-02-data-1.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 65, path: "/mnt/md0/data/cache/s11/sc-02-data-2.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 66, path: "/mnt/md0/data/cache/s11/sc-02-data-3.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 67, path: "/mnt/md0/data/cache/s11/sc-02-data-4.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 },
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 68, path: "/mnt/md0/data/cache/s11/sc-02-data-5.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 69, path: "/mnt/md0/data/cache/s11/sc-02-data-6.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 70, path: "/mnt/md0/data/cache/s11/sc-02-data-7.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 71, path: "/mnt/md0/data/cache/s11/sc-02-data-8.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 72, path: "/mnt/md0/data/cache/s11/sc-02-data-9.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 73, path: "/mnt/md0/data/cache/s11/sc-02-data-10.dat", read: true, write: false}, loaded_from_disk: true, store_size: 34359738368 }, 
DiskStore { len: 1073741824, elem_len: 32, _e: PhantomData, file: File { fd: 74, path: "/mnt/md0/data/cache/s11/sc-02-data-11.dat", read: true, write: false }, loaded_from_disk: true, store_size: 34359738368 }]

code method

The following code is my test case Failed to parse json format Looking for help and guidance

use std::fs;
use serde_json;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
pub struct File {  // file: File
    fd: usize,    // fd: 37,
    path: String, //path: \"、mnt/md0/data/cache/s11/sc-02-data-11.dat\"
    read: bool,   //read: true
    write: bool,  // write: false
}

#[derive(Deserialize, Serialize, Debug)]
pub struct DiskStore {    // DiskStore , },
    len: usize,             // len: 1073741824
    elem_len: usize,        // elem_len: 32
    _e: String,             //_e: PhantomData
    file: File,             // file: File
    loaded_from_disk: bool, // loaded_from_disk: true
    store_size: usize,      // store_size: 34359738368
}



fn main() {
    let  file = fs::read_to_string("11.json").unwrap();
    let  javlue  = serde_json::from_str(&file).unwrap();
    let jdata:Vec<DiskStore> = serde_json::from_value(javlue).unwrap();
    println!("{:?}", jdata);
}

The fields of a json object need quotes around them

[
    "DiskStore": { 
        "len": 1073741824, 
        "elem_len": 32, 
        "_e": "PhantomData", 
        "file": File { 
            "fd": 64, 
            "path": "/mnt/md0/data/cache/s11/sc-02-data-1.dat", 
            "read": true, 
            "write": false 
        }, 
        "loaded_from_disk": true, 
        "store_size": 34359738368 
    },
    /* ... */
]

More of a json question than a Rust question, but oh well :slight_smile:

1 Like

The resulting data type is vec
I want to extract his data for a custom method. What method should be used to decompress into json format?

can you help me?
gentlemen

There's nothing wrong with your Rust code, the json itself is the issue

Can you help me what to do to use the correct json code?
sir

The first code block looks like Debug output, not JSON. If that's the contents of "11.json", then that is indeed the problem.

Alternatively, if your first code block was just for illustration of the data type and not the JSON you're trying to load, show us (at least some of) the contents of "11.json" and the error that you're getting. You can modify this playground, click share, and use the resulting link to share the JSON and errors that way.

(I also notice a mismatch between _e being PhantomData or a String. Maybe you want to use #[serde(skip)] there.)

1 Like

I gave an example in my first response, containing a corrected version of the first entry you gave.

full type data structure

Instead of having Debug output in your json files, you need to have JSON in there. You can use serde_json::to_string to get the JSON representation in Rust code. You might have to put #[serde(skip)] on the _e field to get something reasonable.

If you happen to be on Linux or similar, you can try to convert your existing files on the command line with something like this:

sed -i.backup -r \
  -e 's/DiskStore//g' \
  -e 's/_e: PhantomData,//g' \
  -e 's/File//g' \
  -e 's/([a-z_]+):/"\1":/g' \
  INPUT_FILE.json

(You should make your own backups first too, of course.) I used this on your example and here's the result. Note that

  • I also removed the _e field from DiskStore
  • The long output at the top is still Debug output, not JSON!
  • There's an example of serde_json::to_string at the bottom
    • That single line of output is JSON

This action may lead to a fatal vulnerability in the program

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.