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§

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

Functions§

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