3 releases

0.0.3 Nov 2, 2019
0.0.2 Nov 2, 2019
0.0.1 Oct 15, 2019

#2497 in Parser implementations

Download history 4/week @ 2024-02-19 26/week @ 2024-02-26 4/week @ 2024-03-11 57/week @ 2024-04-01

61 downloads per month
Used in jrf

MIT license

57KB
1.5K SLoC

Jsonist

A JSON formatter

How does it work?

Jsonist tokenizes the input string you give it and then builds an Abstract Syntax Tree (AST).

It then takes the AST and produces a String of formatted JSON from the AST and the optional configuration parameter.

Example Usage

Add to your Cargo.toml:

jsonist = '0.0.3'

Then in your code you can use it like this:

extern crate jsonist;
use jsonist:: { format, FormatConfig, Delimiter, DelimiterCount, FormatterError };

fn example() {
    let json = r#"
        {
            "name": "Peter",
            "leg_count": 2,
            "languages": ["rust", "javascript", "lisp"],
            "address": {
                "street_name": "lets not put this online",
                "city": "a large one"
            },
            "winner": true
        }
    "#.to_owned();

    // let config = FormatConfig::new(Delimiter::Tabs);
    let config = FormatConfig::new(Delimiter::Spaces(DelimiterCount::Two));

    match format(json, config) {
        Ok(formatted_json) => {do what you want with the 'formatted_json'
        }
        Err(e) => panic!("{}", e)
    }
}

Error types

(in case you want to handle, ignore or print them out)

  // General Tokeniser
  ExpectedMoreCharacters, InvalidTokenStartCharacter, WrongCharacter

  // Tokenising Numbers 
  InvalidNumberCharacter, ExtraDotInNumber, ExtraEInNumber, NumberLiteralEndingInE,

  // Parser
  ExpectedMoreTokens, ExpectedColonInKeyValuePair, ExpectedStringLiteral, DuplicateKeyEntry

No runtime deps