1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
use crate::ser::to_feature_writer;
use crate::{Error, Feature, Result};
use serde::Serialize;
use std::io::Write;
#[derive(PartialEq)]
enum State {
New,
Started,
Finished,
}
/// Write Features to a FeatureCollection
pub struct FeatureWriter<W: Write> {
writer: W,
state: State,
}
impl<W: Write> FeatureWriter<W> {
/// Create a FeatureWriter from the given `writer`.
///
/// To append features from your custom structs, use [`FeatureWriter::serialize`].
///
/// To append features from [`Feature`] use [`FeatureWriter::write_feature`].
pub fn from_writer(writer: W) -> Self {
Self {
writer,
state: State::New,
}
}
/// Write a [`crate::Feature`] struct to the output stream. If you'd like to
/// serialize your own custom structs, see [`FeatureWriter::serialize`] instead.
pub fn write_feature(&mut self, feature: &Feature) -> Result<()> {
match self.state {
State::Finished => {
return Err(Error::InvalidWriterState(
"cannot write another Feature when writer has already finished",
))
}
State::New => {
self.write_prefix()?;
self.state = State::Started;
}
State::Started => {
self.write_str(",")?;
}
}
serde_json::to_writer(&mut self.writer, feature)?;
Ok(())
}
/// Serialize your own custom struct to the features of a FeatureCollection using the
/// [`serde`] crate.
///
/// # Examples
///
/// Your struct must implement or derive [`serde::Serialize`].
///
/// If you have enabled the `geo-types` feature, which is enabled by default, you can
/// serialize directly from a useful geometry type.
///
/// ```rust,ignore
/// use geojson::{FeatureWriter, ser::serialize_geometry};
///
/// #[derive(serde::Serialize)]
/// struct MyStruct {
/// #[serde(serialize_with = "serialize_geometry")]
/// geometry: geo_types::Point<f64>,
/// name: String,
/// age: u64,
/// }
/// ```
///
/// Then you can serialize the FeatureCollection directly from your type.
#[cfg_attr(feature = "geo-types", doc = "```")]
#[cfg_attr(not(feature = "geo-types"), doc = "```ignore")]
/// #
/// # use geojson::{FeatureWriter, ser::serialize_geometry};
/// #
/// # #[derive(serde::Serialize)]
/// # struct MyStruct {
/// # #[serde(serialize_with = "serialize_geometry")]
/// # geometry: geo_types::Point<f64>,
/// # name: String,
/// # age: u64,
/// # }
///
/// let dinagat = MyStruct {
/// geometry: geo_types::point!(x: 125.6, y: 10.1),
/// name: "Dinagat Islands".to_string(),
/// age: 123
/// };
///
/// let neverland = MyStruct {
/// geometry: geo_types::point!(x: 2.3, y: 4.5),
/// name: "Neverland".to_string(),
/// age: 456
/// };
///
/// let mut output: Vec<u8> = vec![];
/// {
/// let io_writer = std::io::BufWriter::new(&mut output);
/// let mut feature_writer = FeatureWriter::from_writer(io_writer);
/// feature_writer.serialize(&dinagat).unwrap();
/// feature_writer.serialize(&neverland).unwrap();
/// }
///
/// let expected_output = r#"{
/// "type": "FeatureCollection",
/// "features": [
/// {
/// "type": "Feature",
/// "geometry": { "type": "Point", "coordinates": [125.6, 10.1] },
/// "properties": {
/// "name": "Dinagat Islands",
/// "age": 123
/// }
/// },
/// {
/// "type": "Feature",
/// "geometry": { "type": "Point", "coordinates": [2.3, 4.5] },
/// "properties": {
/// "name": "Neverland",
/// "age": 456
/// }
/// }
/// ]
/// }"#.as_bytes();
///
/// fn assert_eq_json(bytes_1: &[u8], bytes_2: &[u8]) {
/// // check for semantic equality, discarding any formatting/whitespace differences
/// let json_1: serde_json::Value = serde_json::from_slice(bytes_1).unwrap();
/// let json_2: serde_json::Value = serde_json::from_slice(bytes_2).unwrap();
/// assert_eq!(json_1, json_2);
/// }
///
/// assert_eq_json(expected_output, &output);
/// ```
///
/// If you're not using [`geo-types`](geo_types), you can deserialize to a `geojson::Geometry` instead.
/// ```rust,ignore
/// use serde::Deserialize;
/// #[derive(Deserialize)]
/// struct MyStruct {
/// geometry: geojson::Geometry,
/// name: String,
/// age: u64,
/// }
/// ```
pub fn serialize<S: Serialize>(&mut self, value: &S) -> Result<()> {
match self.state {
State::Finished => {
return Err(Error::InvalidWriterState(
"cannot serialize another record when writer has already finished",
))
}
State::New => {
self.write_prefix()?;
self.state = State::Started;
}
State::Started => {
self.write_str(",")?;
}
}
to_feature_writer(&mut self.writer, value)
}
/// Writes the closing syntax for the FeatureCollection.
///
/// You shouldn't normally need to call this manually, as the writer will close itself upon
/// being dropped.
pub fn finish(&mut self) -> Result<()> {
match self.state {
State::Finished => {
return Err(Error::InvalidWriterState(
"cannot finish writer - it's already finished",
))
}
State::New => {
self.state = State::Finished;
self.write_prefix()?;
self.write_suffix()?;
}
State::Started => {
self.state = State::Finished;
self.write_suffix()?;
}
}
Ok(())
}
/// Flush the underlying writer buffer.
///
/// You shouldn't normally need to call this manually, as the writer will flush itself upon
/// being dropped.
pub fn flush(&mut self) -> Result<()> {
Ok(self.writer.flush()?)
}
fn write_prefix(&mut self) -> Result<()> {
self.write_str(r#"{ "type": "FeatureCollection", "features": ["#)
}
fn write_suffix(&mut self) -> Result<()> {
self.write_str("]}")
}
fn write_str(&mut self, text: &str) -> Result<()> {
self.writer.write_all(text.as_bytes())?;
Ok(())
}
}
impl<W: Write> Drop for FeatureWriter<W> {
fn drop(&mut self) {
if self.state != State::Finished {
_ = self.finish().map_err(|e| {
log::error!("FeatureWriter errored while finishing in Drop impl. To handle errors like this, explicitly call `FeatureWriter::finish`. Error: {}", e);
});
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::JsonValue;
use serde_json::json;
// an example struct that we want to serialize
#[derive(Serialize)]
struct MyRecord {
geometry: crate::Geometry,
name: String,
age: u64,
}
#[test]
fn write_empty() {
let mut buffer: Vec<u8> = vec![];
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
writer.finish().unwrap();
}
let expected = json!({
"type": "FeatureCollection",
"features": []
});
let actual_json: JsonValue = serde_json::from_slice(&buffer).unwrap();
assert_eq!(actual_json, expected);
}
#[test]
fn finish_on_drop() {
let mut buffer: Vec<u8> = vec![];
{
_ = FeatureWriter::from_writer(&mut buffer);
}
let expected = json!({
"type": "FeatureCollection",
"features": []
});
let actual_json: JsonValue = serde_json::from_slice(&buffer).unwrap();
assert_eq!(actual_json, expected);
}
#[test]
fn write_feature() {
let mut buffer: Vec<u8> = vec![];
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
let record_1 = {
let mut props = serde_json::Map::new();
props.insert("name".to_string(), "Mishka".into());
props.insert("age".to_string(), 12.into());
Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2]))),
id: None,
properties: Some(props),
foreign_members: None,
}
};
let record_2 = {
let mut props = serde_json::Map::new();
props.insert("name".to_string(), "Jane".into());
props.insert("age".to_string(), 22.into());
Feature {
bbox: None,
geometry: Some(crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2]))),
id: None,
properties: Some(props),
foreign_members: None,
}
};
writer.write_feature(&record_1).unwrap();
writer.write_feature(&record_2).unwrap();
writer.flush().unwrap();
}
let expected = json!({
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [1.1, 1.2] },
"properties": { "name": "Mishka", "age": 12
}
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [2.1, 2.2] },
"properties": {
"name": "Jane",
"age": 22
}
}
]
});
let actual_json: JsonValue = serde_json::from_slice(&buffer).expect("valid json");
assert_eq!(actual_json, expected)
}
#[test]
fn serialize() {
let mut buffer: Vec<u8> = vec![];
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
let record_1 = MyRecord {
geometry: crate::Geometry::from(crate::Value::Point(vec![1.1, 1.2])),
name: "Mishka".to_string(),
age: 12,
};
let record_2 = MyRecord {
geometry: crate::Geometry::from(crate::Value::Point(vec![2.1, 2.2])),
name: "Jane".to_string(),
age: 22,
};
writer.serialize(&record_1).unwrap();
writer.serialize(&record_2).unwrap();
writer.flush().unwrap();
}
let expected = json!({
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [1.1, 1.2] },
"properties": { "name": "Mishka", "age": 12
}
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [2.1, 2.2] },
"properties": {
"name": "Jane",
"age": 22
}
}
]
});
let actual_json: JsonValue = serde_json::from_slice(&buffer).expect("valid json");
assert_eq!(actual_json, expected)
}
#[cfg(feature = "geo-types")]
mod test_geo_types {
use super::*;
use crate::ser::serialize_geometry;
// an example struct that we want to serialize
#[derive(Serialize)]
struct MyGeoRecord {
#[serde(serialize_with = "serialize_geometry")]
geometry: geo_types::Point,
name: String,
age: u64,
}
#[test]
fn serialize_geo_types() {
let mut buffer: Vec<u8> = vec![];
{
let mut writer = FeatureWriter::from_writer(&mut buffer);
let record_1 = MyGeoRecord {
geometry: geo_types::point!(x: 1.1, y: 1.2),
name: "Mishka".to_string(),
age: 12,
};
let record_2 = MyGeoRecord {
geometry: geo_types::point!(x: 2.1, y: 2.2),
name: "Jane".to_string(),
age: 22,
};
writer.serialize(&record_1).unwrap();
writer.serialize(&record_2).unwrap();
writer.flush().unwrap();
}
let expected = json!({
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [1.1, 1.2] },
"properties": {
"name": "Mishka",
"age": 12
}
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [2.1, 2.2] },
"properties": {
"name": "Jane",
"age": 22
}
}
]
});
let actual_json: JsonValue = serde_json::from_slice(&buffer).expect("valid json");
assert_eq!(actual_json, expected)
}
}
}