Giter Club home page Giter Club logo

tinyjson's Introduction

tinyjson

version CI

tinyjson is a library to parse/generate JSON format document.

Goals of this library are

  • Simplicity: This library uses standard containers like Vec or HashMap as its internal representation and exposes it to users. Users can operate JSON values via the standard APIs. And it keeps this crate as small as possible.
  • Explicit: This library does not hide memory allocation from users. You need to allocate memory like Vec, String, HashMap by yourself. It is good for readers of your source code to show where memory allocations happen. And you can have control of how memory is allocated (e.g. allocating memory in advance with with_capacity method).
  • No dependencies: This library is built on top of only standard libraries.
  • No unsafe code: This library is built with Safe Rust.
  • Well tested: This library is tested with famous test suites:

Documentation

Requirements

Rust stable toolchain.

Installation

Add this crate to dependencies section of your Cargo.toml

[dependencies]
tinyjson = "2"

Example

use tinyjson::JsonValue;
use std::collections::HashMap;
use std::convert::TryInto;

let s = r#"
    {
        "bool": true,
        "arr": [1, null, "test"],
        "nested": {
            "blah": false,
            "blahblah": 3.14
        },
        "unicode": "\u2764"
    }
"#;

// Parse from strings
let parsed: JsonValue = s.parse().unwrap();

// Access to inner value represented with standard containers
let object: &HashMap<_, _> = parsed.get().unwrap();
println!("Parsed HashMap: {:?}", object);

// Generate JSON string
println!("{}", parsed.stringify().unwrap());
// Generate formatted JSON string with indent
println!("{}", parsed.format().unwrap());

// Access nested elements by .query() or .query_mut() without panic
let elem = parsed.query().child("arr").child(1).find();
println!("Second element of \"arr\": {:?}", elem);

// Convert to inner value represented with standard containers
let object: HashMap<_, _> = parsed.try_into().unwrap();
println!("Converted into HashMap: {:?}", object);

// Create JSON values from standard containers
let mut m = HashMap::new();
m.insert("foo".to_string(), true.into());
let mut v = JsonValue::from(m);

// Access with `Index` and `IndexMut` operators quickly (panic when no element)
println!("{:?}", v["foo"]);
v["foo"] = JsonValue::from("hello".to_string());
println!("{:?}", v["foo"]);

See the document to know all APIs.

Repository

https://github.com/rhysd/tinyjson

License

the MIT License

tinyjson's People

Contributors

cherryblossom000 avatar philippludwig avatar rhysd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

tinyjson's Issues

Release outdated?

The README documents the .query method, which cannot be found in the latest release v2.5.1 doc.

I suppose there are some major changes in the git repo are not published yet?

Query API

Currently it is hard to access to nested elements. ([] operator panics when the key or index doesn't exist)

We need to consider some query API to

  • get nested values without panic
  • check if some (nested) keys or indices are existing

API currently I'm thinking about:

let v: JsonValue = r#"[{"foo": true}]"#.parse().unwrap();

// Access to nested element
let found = v.query().child(0).child("foo").find();
assert_eq!(found, Some(&JsonValue::Boolean(true)));

// Check if key exists
assert!(v.query().child(0).exists());
assert!(!v.query().child(1).exists());
assert!(!v.query().child("foo").exists());

// Mutably access to nested element
let Some(found) = v.query_mut().child(0).child("foo").find() else { panic() };
*found = JsonValue::Number(10.0);
assert_eq!(v.stringify().unwrap(), "[{\"foo\":10}]");

How to check if key exists?

My JSON file might not contain a certain key. How do I check if such key exists? I'd rather not resort to catching panics.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.