I/O Overview

The io module provides import and export functionality for narratives in various formats.

Supported Formats

FormatTypeBest For
JSONNativeFull fidelity, all metadata preserved
GeoJSONStandardWeb mapping, GIS tools, Leaflet/Mapbox
CSVTabularSpreadsheets, data analysis, pandas

The Format Trait

All formats implement the Format trait:

use spatial_narrative::io::Format;

pub trait Format {
    fn import<R: Read>(&self, reader: &mut R) -> Result<Narrative>;
    fn export<W: Write>(&self, narrative: &Narrative, writer: &mut W) -> Result<()>;
    fn import_str(&self, s: &str) -> Result<Narrative>;
    fn export_str(&self, narrative: &Narrative) -> Result<String>;
}

Quick Examples

Export to Multiple Formats

use spatial_narrative::io::{GeoJsonFormat, CsvFormat, JsonFormat, Format};
use spatial_narrative::core::{Narrative, NarrativeBuilder, Event, Location, Timestamp};

let narrative = NarrativeBuilder::new()
    .title("My Story")
    .event(Event::new(
        Location::new(40.7128, -74.0060),
        Timestamp::now(),
        "Something happened"
    ))
    .build();

// Export to GeoJSON (for web maps)
let geojson = GeoJsonFormat::new().export_str(&narrative)?;

// Export to CSV (for spreadsheets)
let csv = CsvFormat::new().export_str(&narrative)?;

// Export to JSON (for full fidelity)
let json = JsonFormat::new().export_str(&narrative)?;

Import from File

use std::fs::File;
use std::io::BufReader;

// Import from GeoJSON file
let file = File::open("data.geojson")?;
let mut reader = BufReader::new(file);
let narrative = GeoJsonFormat::new().import(&mut reader)?;

println!("Loaded {} events", narrative.events.len());

Export to File

use std::fs::File;
use std::io::BufWriter;

// Export to CSV file
let file = File::create("output.csv")?;
let mut writer = BufWriter::new(file);
CsvFormat::new().export(&narrative, &mut writer)?;

Format Comparison

FeatureJSONGeoJSONCSV
All metadata⚠️ Partial⚠️ Limited
Tags
Sources⚠️ Optional
Custom metadata⚠️ Properties
Human readable⚠️
GIS compatible⚠️
Spreadsheet ready
File sizeMediumLargeSmall

Round-Trip Fidelity

For lossless round-trips, use JsonFormat:

let json = JsonFormat::new();

// Export
let exported = json.export_str(&narrative)?;

// Import
let imported = json.import_str(&exported)?;

// Verify
assert_eq!(narrative.events.len(), imported.events.len());

Next Steps