use-json 0.1.0

Lightweight JSON inspection and formatting helpers for RustUse
Documentation
use use_json::{
    compact_json_basic, detect_json_kind, escape_json_string, looks_like_json,
    looks_like_json_array, looks_like_json_object, quote_json_string, unquote_json_string,
    JsonKind,
};

#[test]
fn detects_objects_and_arrays() {
    assert!(looks_like_json_object(" { \"name\": \"RustUse\" } "));
    assert!(looks_like_json_array("[1, 2, 3]"));
    assert!(looks_like_json("true"));
}

#[test]
fn detects_primitive_kinds() {
    assert_eq!(detect_json_kind("null"), JsonKind::Null);
    assert_eq!(detect_json_kind("false"), JsonKind::Bool);
    assert_eq!(detect_json_kind("3.14e2"), JsonKind::Number);
    assert_eq!(detect_json_kind("\"value\""), JsonKind::String);
}

#[test]
fn escapes_and_quotes_strings() {
    assert_eq!(
        escape_json_string("line\n\"quote\""),
        "line\\n\\\"quote\\\""
    );
    assert_eq!(quote_json_string("tab\tvalue"), "\"tab\\tvalue\"");
    assert_eq!(
        unquote_json_string("\"hello\\nworld\""),
        Some("hello\nworld".to_string())
    );
}

#[test]
fn compacts_json_without_touching_string_content() {
    assert_eq!(
        compact_json_basic("{ \"message\": \"two words\", \"ok\": true }"),
        "{\"message\":\"two words\",\"ok\":true}"
    );
}

#[test]
fn handles_malformed_input_gracefully() {
    assert_eq!(detect_json_kind("{"), JsonKind::Unknown);
    assert_eq!(detect_json_kind("01"), JsonKind::Unknown);
    assert_eq!(unquote_json_string("\"unterminated"), None);
}

#[test]
fn handles_empty_input() {
    assert!(!looks_like_json("   "));
    assert_eq!(detect_json_kind(""), JsonKind::Unknown);
}