-
Notifications
You must be signed in to change notification settings - Fork 34
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
Convert serde_json::Value to Ion #803
Comments
I'm assuming you mean "without serde-annotated structs". You would need to define a mapping from use ion_rs::{Element, IonType, List, Sequence, Struct};
use serde_json::{Value};
fn json_to_ion(value: Value) -> Element {
match value {
Value::Null => Element::null(IonType::Null),
Value::Bool(b) => Element::boolean(b),
Value::Number(n) => {
todo!("Choose a number conversion strategy")
// == Convert Number to Ion float and Ion int ==
// if let Some(i) = n.as_i64() {
// Element::int(i)
// } else if let Some(i) = n.as_u64() {
// Element::int(i)
// } else if let Some(f) = n.as_f64() {
// Element::float(f)
// } else {
// unreachable!()
// }
// == Convert all Number to Ion decimal ==
// if let Some(i) = n.as_i64() {
// Element::decimal(i.into())
// } else if let Some(i) = n.as_u64() {
// Element::decimal(i.into())
// } else if let Some(f) = n.as_f64() {
// Element::decimal(f.try_into().expect("Infinite or NaN values are not JSON numbers."))
// } else {
// unreachable!()
// }
// One could also convert float Numbers to Ion decimal and integer Numbers to Ion int
}
Value::String(s) => Element::string(s),
Value::Array(values) => List(
values
.into_iter()
.map(|v| json_to_ion(v))
.collect::<Sequence>(),
)
.into(),
Value::Object(values) => values
.into_iter()
.map(|(k, v)| (k, json_to_ion(v)))
.collect::<Struct>()
.into(),
}
} (Constructing the List is more awkward than it needs to be; I've created #811 to address that.) Then, assuming you are using Does this help? (Have I understood your question correctly?) |
Yes, correct. No serde annotated structs. This gives me a good starting point. Thanks! |
Follow up question, is there anyway to go from a Vec representing a JSON byte string directly into Ion or do I need to convert to a serde_json::Value first? |
By "Vec representing a JSON byte string" do you just mean serialized JSON? All JSON is valid text-encoded Ion, so you could skip reading as JSON and read it as if it were Ion using The disadvantage to this approach, however, is that you have no specific control over how numbers are converted. JSON has only one "number" type, whereas Ion has three. When up-converting from JSON, the Ion reader will follow the same rules as parsing text-encoded Ion.
|
Yes serialized JSON. The read_one method sounds like it will work for my use case. I’ll try it out. Thanks again! |
My API allows people to pass arbitrary JSON payloads in a request body. Through Axum I’m converting the payload into a serde_json::Value. I’d like to convert this into an Ion binary value and store it (in DynamoDB).
What’s the best way to do the conversion if I don’t care about the schema?
The text was updated successfully, but these errors were encountered: