Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/anowell/static-json-pointer

Rust macro to extract literals and tokens from JSON at compile time
https://github.com/anowell/static-json-pointer

Last synced: about 2 months ago
JSON representation

Rust macro to extract literals and tokens from JSON at compile time

Awesome Lists containing this project

README

        

## static-json-pointer

Macro to extract Rust tokens and literals from a JSON schema

If you have a file `schema.json`:

```json
{
"person": {
"name": {
"type": "String",
"value": "Zazu"
},
"age": {
"type": "Option",
"value": 42
}
}
}
```

You can use a JSON pointer to specify the field for extracting a token or literal from JSON at compile time:

```rust
#![feature(proc_macro)]
extern crate static_json_pointer;
use static_json_pointer::json_token;

// let name = String::from("Zazu");
let name = json_token!("schema.json", "/person/name/type")::from(json_literal!("schema.json", "/person/name/value"));

// let age = Option::from(42);
let age = json_token!("schema.json", "/person/age/type")::from(json_literal!("schema.json", "/person/age/value"));

assert_eq!(name, "Zazu".to_string());
assert_eq!(age, Some(42));
```

The deserialized JSON is cached during build to prevent redundant reading and parsing during build.