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

Row to serde_json::Value #174

Open
amokio opened this issue Jan 6, 2024 · 12 comments
Open

Row to serde_json::Value #174

amokio opened this issue Jan 6, 2024 · 12 comments
Labels
wontfix This will not be worked on

Comments

@amokio
Copy link

amokio commented Jan 6, 2024

for row in rows {
        for col in &cols.metadata.col_specs {
            let value = match col.clone().col_type.id {
                ColType::Varchar | ColType::Ascii => {
                    let val: String = row.get_by_name(col.name.as_str()).unwrap().expect("");
                    println!("{:#?}", val)
                }
                ColType::Map => {
                    let map_value: Map = row.get_by_name(col.name.as_str()).unwrap().expect("");
                }
                _ => {}
            };
        }
    }

is anyone have idea how to convert the Map/List to serde_json::Value? assume the struct is unknown.

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

You can convert them into Vec/Map and then to Value.

@amokio
Copy link
Author

amokio commented Jan 6, 2024

You can convert them into Vec/Map and then to Value.

Not really understanding, any code snippet?

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

Here's an example how to convert a Cassandra list to a Vec: https://github.com/krojew/cdrs-tokio/blob/master/cdrs-tokio/tests/collection_types.rs#L76
You can do similar thing to a Map.

@amokio
Copy link
Author

amokio commented Jan 6, 2024

Here's an example how to convert a Cassandra list to a Vec: https://github.com/krojew/cdrs-tokio/blob/master/cdrs-tokio/tests/collection_types.rs#L76

You can do similar thing to a Map.

But the problem is the row structure is unknown. I need to use ColSpec to get correct type

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

Is there anything preventing you from calling as_r_rust() on the column data?

@amokio
Copy link
Author

amokio commented Jan 6, 2024

Is there anything preventing you from calling as_r_rust() on the column data?

No, but when I declare the new variable like HashMap<String, String (assume I don't know it's String)>, I have to provide type. But assume I don't know the map structure

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

ColTypeOptionValue contains the information about the underlying types for collections. You can use that to deserialize appropriate values and move them into serde_json::Value.

@amokio
Copy link
Author

amokio commented Jan 6, 2024

ColTypeOptionValue contains the information about the underlying types for collections. You can use that to deserialize appropriate values and move them into serde_json::Value.

Yes, currently using this, but not sure how to deal with nested map and triple

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

The same way you'd do it with other programming languages - recursion.

@amokio
Copy link
Author

amokio commented Jan 6, 2024

@krojew , any idea ?

let v: ColTypeOptionValue = col.clone().col_type.value.expect("");
            let v = match v {
                ColTypeOptionValue::CMap(key, v) => {
                    println!("{:#?} {:#?}", key.id, v);

                    // how to use key.id and v.id here?
                    let map: HashMap<?, ?> = row
                        .r_by_name::<Map>(col.name.as_str())
                        .expect("")
                        .as_r_rust()
                        .expect("");
                }
                _ => todo!(),
            };

@krojew
Copy link
Owner

krojew commented Jan 6, 2024

You are asking a basic programming question now, which is out of scope of rust crates.

Copy link

stale bot commented Apr 22, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants