6 stable releases

3.2.1 Oct 28, 2024
3.2.0 Oct 16, 2024
3.1.1 Sep 2, 2024
3.1.0 Jul 22, 2024
3.0.0 Mar 16, 2023

#815 in Parser implementations

Download history 35/week @ 2024-07-24 142/week @ 2024-08-28 29/week @ 2024-09-04 20/week @ 2024-09-11 18/week @ 2024-09-18 16/week @ 2024-09-25 3/week @ 2024-10-02 149/week @ 2024-10-16 110/week @ 2024-10-23 32/week @ 2024-10-30 6/week @ 2024-11-06

297 downloads per month

Apache-2.0

49KB
1K SLoC

Momoa JSON

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

About

Momoa is a general purpose JSON utility toolkit, containing:

  • A tokenizer that allows you to separate a JSON string into its component parts.
  • A ECMA-404 compliant parser that produces an abstract syntax tree (AST) representing everything inside of a JSON string.

Background

A tool like Momoa comes in handy when you want to know not just the result of JSON parsing, but exactly what is contained in the original JSON string.

Usage

Parsing

There are two parsing methods: one for JSON and one for JSONC.

To parse a JSON string into an AST, use the json::parse() function:

use momoa::ast::*;
use momoa::json;

fn do_parse(code) -> Node {
    let ast = json::parse(code).unwrap();

    // do something with ast

    ast
}

To allow trailing commas in JSON, use the json::parse_with_trailing_commas() function:

use momoa::ast::*;
use momoa::json;

fn do_parse(code) -> Node {
    let ast = json::parse_with_trailing_commas(code).unwrap();

    // do something with ast

    ast
}

To parse a JSONC string into an AST, use the jsonc::parse() function:

use momoa::ast::*;
use momoa::jsonc;

fn do_parse(code) -> Node {
    let ast = jsonc::parse(code).unwrap();

    // do something with ast

    ast
}

To allow trailing commas in JSONC, use the jsonc::parse_with_trailing_commas() function:

use momoa::ast::*;
use momoa::jsonc;

fn do_parse(code) -> Node {
    let ast = jsonc::parse_with_trailing_commas(code).unwrap();

    // do something with ast

    ast
}

Tokenizing

To produce JSON tokens from a string, use the json::tokenize() function:

use momoa::*;

fn do_parse(code) -> Vec<Token> {
    let result = json::tokenize(code).unwrap();


    // do something with ast

    result
}

To produce JSON tokens from a string, use the jsonc::tokenize() function:

use momoa::*;

fn do_parse(code) -> Vec<Token> {
    let result = jsonc::tokenize(code).unwrap();


    // do something with ast

    result
}

Development

To work on Momoa, you'll need:

Make sure both are installed by visiting the links and following the instructions to install.

Now you're ready to clone the repository:

git clone https://github.com/humanwhocodes/momoa.git

Then, enter the directory and install the dependencies:

cd momoa/rust
cargo build

After that, you can run the tests via:

cargo test

License

Apache 2.0

Frequently Asked Questions

What does "Momoa" even mean?

Momoa is the last name of American actor Jason Momoa. Because "JSON" is pronounced "Jason", I wanted a name that played off of this fact. The most obvious choice would have been something related to Jason and the Argonauts, as this movie is referenced in the JSON specification directly. However, both "Argo" and "Argonaut" were already used for open source projects. When I did a search for "Jason" online, Jason Momoa was the first result that came up. He always plays badass characters so it seemed to fit.

Why support comments in JSON?

There are a number of programs that allow C-style comments in JSON files, most notably, configuration files for Visual Studio Code. As there seems to be a need for this functionality, I decided to add it out-of-the-box.

Dependencies

~1.4–2.6MB
~49K SLoC