Crate fjson

source ·
Expand description

fjson

A library for parsing and formatting JSON with C-style comments and trailing commas.

Format as JSONC

Format to pretty JSONC, intended for human viewing:

const INPUT: &str = r#"
// This is a JSON value with comments and trailing commas
{
    /* The project name is fjson */
    "project": "fjson",
    "language": "Rust",
    "license": [
        "MIT",
    ],


    // This project is public.
    "public": true,
}"#;

fn main() -> Result<(), fjson::Error> {
    let output = fjson::to_jsonc(INPUT)?;
    println!("{}", output);
    Ok(())
}

// Outputs:
//
// // This is a JSON value with comments and trailing commas
// {
//   /* The project name is fjson */
//   "project": "fjson",
//   "language": "Rust",
//   "license": ["MIT"],
//
//   // This project is public.
//   "public": true
// }

Format as JSON

Format to pretty JSON, intended for human viewing:

const INPUT: &str = r#"
// This is a JSON value with comments and trailing commas
{
    /* The project name is fjson */
    "project": "fjson",
    "language": "Rust",
    "license": [
        "MIT",
    ],


    // This project is public.
    "public": true,
}"#;

fn main() -> Result<(), fjson::Error> {
    let output = fjson::to_json(INPUT)?;
    println!("{}", output);
    Ok(())
}

// Outputs:
//
// {
//   "project": "fjson",
//   "language": "Rust",
//   "license": ["MIT"],
//   "public": true
// }

Format as valid, compact JSON

Format to compact JSON, intended for computer consumption:

const INPUT: &str = r#"
// This is a JSON value with comments and trailing commas
{
    /* The project name is fjson */
    "project": "fjson",
    "language": "Rust",
    "license": [
        "MIT",
    ],


    // This project is public.
    "public": true,
}"#;

fn main() -> Result<(), fjson::Error> {
    let output = fjson::to_json_compact(INPUT)?;
    println!("{}", output);
    Ok(())
}

// Outputs:
//
// {"project":"fjson","language":"Rust","license":["MIT"],"public":true}

Deserialize with Serde

To parse JSON with C-style comments and trailing commas, but deserialize via serde, the following can be done:

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Project {
    project: String,
    language: String,
    license: Vec<String>,
    public: bool,
}

const INPUT: &str = r#"
// This is a JSON value with comments and trailing commas
{
    /* The project name is fjson */
    "project": "fjson",
    "language": "Rust",
    "license": [
        "MIT",
    ],


    // This project is public.
    "public": true,
}"#;

fn main() {
    let output = fjson::to_json_compact(INPUT).unwrap();
    let project: Project = serde_json::from_str(&output).unwrap();
    println!("{:#?}", project);
}

Re-exports

pub use error::Error;

Modules

Parse JSONC into an abstract syntax tree.
Error handling for the fjson crate.
Format Root values to JSONC or pretty/compact JSON.
Scanner that provides an iterator over JSONC tokens.
Validate JSON(C) without creating an AST.

Functions

Parses JSONC and formats the output into “pretty” printed JSON.
Parses JSONC and formats the output into valid, compact JSON.
Parses JSONC and formats the output into “pretty” printed JSON to the provided writer.
Parses JSONC and formats the output into valid, compact JSON to the provided writer.
Parses and formats JSON with C-style comments and trailing commas.
Parses and formats JSON with C-style comments and trailing commas to the provided writer.