|
78 | 78 | //! ## Writing to [serde_json] JSON Objects
|
79 | 79 | //!
|
80 | 80 | //! To serialize [`RecordBatch`]es into an array of
|
81 |
| -//! [JSON](https://docs.serde.rs/serde_json/) objects, use the [RawValue] api |
82 |
| -//! |
83 |
| -//! [RawValue]: https://docs.rs/serde_json/latest/serde_json/value/struct.RawValue.html |
| 81 | +//! [JSON](https://docs.serde.rs/serde_json/) objects you can reparse the resulting JSON string. |
| 82 | +//! Note that this is less efficient than using the `Writer` API. |
84 | 83 | //!
|
85 | 84 | //! ```
|
86 | 85 | //! # use std::sync::Arc;
|
|
92 | 91 | //! let a = Int32Array::from(vec![1, 2, 3]);
|
93 | 92 | //! let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
|
94 | 93 | //!
|
95 |
| -//! let json_rows: Vec<Map<String, Value>> = todo!("How do we do this?"); |
96 |
| -//! // let json_rows = arrow_json::writer::record_batches_to_json_rows(&[&batch]).unwrap(); |
| 94 | +//! // Write the record batch out as a JSON array |
| 95 | +//! let json_string = { |
| 96 | +//! let buf = Vec::new(); |
| 97 | +//! let mut writer = arrow_json::ArrayWriter::new(buf); |
| 98 | +//! writer.write_batches(&vec![&batch]).unwrap(); |
| 99 | +//! writer.finish().unwrap(); |
| 100 | +//! String::from_utf8(writer.into_inner()).unwrap() |
| 101 | +//! }; |
| 102 | +//! |
| 103 | +//! // Parse the string using serde_json |
| 104 | +//! let json_rows: Vec<Map<String, Value>> = serde_json::from_str(&json_string).unwrap(); |
97 | 105 | //! assert_eq!(
|
98 | 106 | //! serde_json::Value::Object(json_rows[1].clone()),
|
99 | 107 | //! serde_json::json!({"a": 2}),
|
100 | 108 | //! );
|
101 | 109 | //! ```
|
102 |
| -//! |
103 |
| -//! |
104 |
| -//! |
105 |
| -//! |
106 | 110 | mod encoder;
|
107 | 111 |
|
108 | 112 | use std::iter;
|
|
0 commit comments