GithubHelp home page GithubHelp logo

lite-json's Introduction

lite-json

Crates.io GitHub

Simple JSON parser written with Rust. Wasm / no_std ready.

How to Add in Cargo.toml

std

[dependencies]
lite-json = "0.2.0"

no_std

[dependencies]
lite-json = { version = "0.2.0", default-features = false, defaults = ["no_std"] }

Example Usage

Creating JSON

This example will create a lite-json structure, then print it as a JSON string.

use lite_json::Serialize;
use lite_json::json::{JsonValue, NumberValue};

fn main()
{
	// We will create a bunch of elements that we will put into a JSON Object.
	let mut object_elements = vec!();

	// Create a boolean value and add it to our vector.
	let boolean_value = true;
	let object_key = "boolean".chars().collect();
	object_elements.push((object_key, JsonValue::Boolean(boolean_value)));

	// Create an array value and add it to our vector.
	let array_value = vec!(JsonValue::Boolean(true), JsonValue::Boolean(false), JsonValue::Boolean(true));
	let object_key = "array".chars().collect();
	object_elements.push((object_key, JsonValue::Array(array_value)));

	// Create a string value and add it to our vector.
	let string_value = "Hello World!".chars().collect();
	let object_key = "string".chars().collect();
	object_elements.push((object_key, JsonValue::String(string_value)));

	// Create a number value and add it to our vector.
	let number_value = NumberValue
	{
		integer: 1234,
		fraction: 0,
		fraction_length: 0,
		exponent: 0,
	};
	let object_key = "number".chars().collect();
	object_elements.push((object_key, JsonValue::Number(number_value)));

	// Create a null value and add it to our vector.
	let object_key = "null".chars().collect();
	object_elements.push((object_key, JsonValue::Null));

	// Create the object value from the vector of elements.
	let object_value = JsonValue::Object(object_elements);

	// Convert the object to a JSON string.
	let json = object_value.format(4);
	let json_output = std::str::from_utf8(&json).unwrap();

	println!("{}", json_output);
}

This will output:

{
    "boolean": true,
    "array": [
        true,
        false,
        true
    ],
    "string": "Hello World!",
    "number": 1234,
    "null": null
}

Parsing JSON

This example will parse a JSON string into a lite-json structure.

use lite_json::json_parser::parse_json;

fn main()
{
	// This is the JSON string we will use.
	let json_string =
	r#"
		{
			"boolean": true,
			"array":
			[
				true,
				false,
				true
			],
			"string": "Hello World!",
			"number": 1234,
			"null": null
		}
	"#;

	// Parse the JSON and print the resulting lite-json structure.
	let json_data = parse_json(json_string).expect("Invalid JSON specified!");
	println!("{:?}", json_data);
}

Parsing JSON with Options

The parser options allows you to set the max depth of parsing nested objects. This code will result in an error because the max nest level is set to 1, but the depth of our JSON is 2 due to the presence of a nested array.

Note: This example requires the lite-parser crate to be added to Cargo.toml.

use lite_json::json_parser::parse_json_with_options;
use lite_parser::parser::ParserOptions;

fn main()
{
	// This is the JSON string we will use.
	let json_string =
	r#"
		{
			"boolean": true,
			"array":
			[
				true,
				false,
				true
			],
			"string": "Hello World!",
			"number": 1234,
			"null": null
		}
	"#;

	let parser_options = ParserOptions
	{
		max_nest_level: Some(1)
	};

	// Parse the JSON and print the resulting lite-json structure.
	let json_data = parse_json_with_options(json_string, parser_options).expect("Invalid JSON specified!");
	println!("{:?}", json_data);
}

lite-json's People

Contributors

dtolnay avatar enfipy avatar jnaviask avatar jordanmack avatar kichjang avatar shawntabrizi avatar xanewok avatar xlc 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

Watchers

 avatar  avatar  avatar

lite-json's Issues

Facing difficulty in fetching data

Api data

{
  "student": [
    {
      "listing": {
        "price": 54,
      },
    },
 ]
}

Iโ€™m using below code for fetching data till listing:

let student = match val.ok()? {
  JsonValue::Object(obj) => {
     let (_, v) = obj.into_iter().find(|(k, _)| k.iter().copied().eq("student".chars()))?;
     match v {
        JsonValue::Array(vec) => vec,
        _ => return None,
     }
  },
  _ => return None,
};

for (i, obj) in instances.clone().into_iter().enumerate() {
  let list = match obj.clone() {
     JsonValue::Object(obj_data) => {
        let (_, v) = obj_data.into_iter().find(|(k, _)| k.iter().copied().eq("listings".chars()))?;
        match v {
           JsonValue::Object(list) => list,
           _ => return None,
        }
     },
     _ => return None,
  };

 // ?? price
}

Ques: How to fetch price from list?
Ques: I'm returning object from listing, Is that correct?

Parse valid JSON array of objects and insert each object into a substrate pallet's StorageMap

@xlc i've been trying to use lite-json, and i noticed that you actually created it!

i'm want to use off-chain workers to query an external API endpoint in our code here where i've added the off-chain workers example code https://github.com/DataHighway-DHX/node/blob/luke/rewards-allowance-new-offchain/pallets/mining/rewards-allowance/src/lib.rs#L2684

i want to retrieve data in the following valid JSON format from the body of the http request response:

{
  "data": [
  	{
	    "acct_id": "1234",
	    "mpower": "1",
	    "date_last_updated": "1636096907000"
	},
  	{
	    "acct_id": "1234",
	    "mpower": "1",
	    "date_last_updated": "1636096907000"
	}
  ]
}

then iterate through each object in the array and whilst doing so i'll populate a data structure with each object's values and insert it into the pallet's storage where for each object in the that array i create a key (which is a tuple that contains both the start of the current date that we received the response, and the account_id that was in the response), and a value (which just contains the other two values received including the account_id's mining power mpower and the date that date was last updated off-chain)

    /// Recently submitted mPower data.
    #[pallet::storage]
    #[pallet::getter(fn mpower_of_account_for_date)]
    pub(super) type MPowerForAccountForDate<T: Config> = StorageMap<_, Blake2_128Concat,
        (
            Date, // converted to start of date
            T::AccountId,
        ),
        (
            u128, // mPower
            Date, // date last updated off-chain
            T::BlockNumber, // block receive using off-chain workers
        ),
    >;

i'd also like to know how to serialize and deserialize that kind of object.

how may i do this with lite-json?

negative decimal numbers are being offset by +1

As the title suggests the parser currently ignores the sign of decimal numbers.

Simple example code:

   use lite_json::JsonValue::Number;
   use lite_json::*;
   
    fn main() {
        let json = parse_json("-0.5").unwrap(); 
        if let Number(n) = json { 
            println!("{}", n.to_f64());
        };
    }

Expected Result:-0.5
Current Result:0.5

EDIT:spelling and gramma
EDIT2:After further testing i found that the parser does acknowledge the sign but secretly adds about 1 (will investigate further) to the result which lead to my first thought of it just ignoring the sign since i only used numbers above -1. So far it seems that the fraction has to be non zero for the bug to occur.

How to fetch floating no in offchain worker?

I want to fetch the floating value in offchain worker. I'm using lite_json for http request.

Api data: {
"price": 0.5
}

To fetch and store this price from api. I'm defining a struct:

pub struct data {
   price: ?
}

Ques 1: here what would be the type of price?

Here is the code for fetching data:

let obj_price = match obj.clone() {
				JsonValue::Object(obj_data) => {
					let (_, v) = obj_data.into_iter().find(|(k, _)| k.iter().copied().eq("price".chars()))?;
					match v {
						JsonValue::Number(val) => val,
						_ => return None,
					}
				},
				_ => return None,
			};

			let Price = obj_price; // ??

Ques2: How to convert obj_price to the type that we defined in the struct?

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.