forma_json 0.1.0

JSON serialization and deserialization for forma_core.
Documentation
use forma_derive::{Deserialize, Serialize};
use forma_json::{from_str, to_string};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Color {
    Red,
    Green,
    Blue,
    #[forma(other)]
    Unknown,
}

#[test]
fn test_other_known_variant() {
    assert_eq!(from_str::<Color>("\"Red\"").unwrap(), Color::Red);
    assert_eq!(from_str::<Color>("\"Green\"").unwrap(), Color::Green);
    assert_eq!(from_str::<Color>("\"Blue\"").unwrap(), Color::Blue);
}

#[test]
fn test_other_unknown_variant() {
    // Unknown variant names should map to the #[forma(other)] variant
    assert_eq!(from_str::<Color>("\"Yellow\"").unwrap(), Color::Unknown);
    assert_eq!(from_str::<Color>("\"Purple\"").unwrap(), Color::Unknown);
    assert_eq!(from_str::<Color>("\"\"").unwrap(), Color::Unknown);
}

#[test]
fn test_other_serialize() {
    // The other variant serializes by its actual name
    assert_eq!(to_string(&Color::Unknown).unwrap(), "\"Unknown\"");
    assert_eq!(to_string(&Color::Red).unwrap(), "\"Red\"");
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Status {
    Active,
    Inactive,
    #[forma(other)]
    #[forma(rename = "other")]
    Other,
}

#[test]
fn test_other_with_rename() {
    assert_eq!(from_str::<Status>("\"Active\"").unwrap(), Status::Active);
    assert_eq!(from_str::<Status>("\"SomeNewStatus\"").unwrap(), Status::Other);
    assert_eq!(to_string(&Status::Other).unwrap(), "\"other\"");
}