WilliamRagstad / qjson

A Quick and Simple JSON parser written in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The qJSON-parser

A quick and easy JSON parser.

WARNING: This library should not be used in production, it is purely for learning rust and how its library system works.

Introduction

This is a simple JSON parser written in Rust with full support for JSON5 serialization and deserialization.

Usage

The following code snippet shows an example of how to use the parser:

use qjson::{parse_file, parse_str};

fn main() {
    let json = r#"{
        name: "John Doe",
        'age': 42,
        "married": true,
        "children": ['Jane', "Jack"],
        "address": {
            "street": "Main St.",
            "city": "New York",
            "state": "NY"
        }
    }"#;

    let parsed = parse_str(json).unwrap();
    println!("{:?}", parsed);
}

The output is the parsed JSON object:

{
    "name": "John Doe",
    "age": 42,
    "married": true,
    "children": [
        "Jane",
        "Jack"
    ],
    "address": {
        "street": "Main St.",
        "city": "New York",
        "state": "NY"
    }
}

Features

The following features are provided by the library.

Definition

JSON values and numerical values are defined as following:

#[derive(Debug, PartialEq, Clone)]
pub enum JsonNumber {
    Int(i64),
    Float(f64),
}

#[derive(Debug, PartialEq, Clone)]
pub enum JsonValue {
 Null,
 Bool(bool),
 Number(JsonNumber),
 String(String),
 Array(Vec<Box<JsonValue>>),
 Object(HashMap<String, Box<JsonValue>>),
}

Single values

It is also possible to parse single JSON values:

use qjson::{parse_str};

fn main() {
    let json = r#"[1, 2, 3]"#;
    let parsed = parse_str(json).unwrap();
    println!("{:?}", parsed);
}

The output is the parsed JSON array:

[ 1, 2, 3 ]

About

A Quick and Simple JSON parser written in Rust


Languages

Language:Rust 100.0%