• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Construct JSON objects in Rust from JSON-like literals. (deprecado, use serde_json's json! macro)

json_macros

Crates.io [Build Status] (https://travis-ci.org/tomjakubowski/json_macros)

let properties = json! {
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                {"value": "New", "onclick": "CreateNewDoc()"},
                {"value": "Open", "onclick": "OpenDoc()"},
                {"value": "Close", "onclick": "CloseDoc()"}
            ]
        }
    }
};

let menu_value = properties.find_path(["menu", "value"])
    .map(|x| x.as_string());

assert_eq!(menu_value, Some("File"));

Use JSON-like literals in Rust to construct serde_json Values or rustc-serialize Json values.

Because json_macros is a compiler plugin, it's only compatible with the Rust nightly channel.

Depending on your project's needs, you may ask json_macros to generate code that constructs serde_json values or code that constructs rustc-serialize values.

Using json_macros with rustc-serialize

By default, json_macros generates code for rustc-serialize. In a future release, the default may switch to serde_json, but json_macros should be at least optionally compatible with rustc-serialize for as long as that crate is supported.

To use json_macros with rustc-serialize, add both packages as dependencies to your Cargo.toml.

[dependencies]
json_macros = "^0.3"
rustc-serialize = "^0.3"

Your crate will also need to link with rustc_serialize and use it in any submodule that uses the json!() macro.

extern crate rustc_serialize;

// ...

mod foo {
    use rustc_serialize;
    // ...
}

Example

#![feature(plugin)]
#![plugin(json_macros)]

extern crate rustc_serialize;

pub fn main() {
    let x = 123i32;
    println!("{}", json!({ // object literal
        "foo": "foooooo", // string literal keys and values
        "bar": [true, null, 123, 123.4], // array, boolean, null, numeric literals
        "quux": { // nest as deeply as you like
          "a": [1, 2, 3, 4],
          "b": { "a": null },
          "c": false
        },
        "waldo": (192 - x) // wrap in parens to splice ToJson expressions directly
    }).pretty().to_string());
}

Using json_macros with serde_json

To use json_macros with serde_json, add both packages as dependencies to your Cargo.toml. Enable the with-serde_json feature for json_macros and disable the default features so as to not depend on rustc-serialize.

[dependencies]
rustc-serialize = "^0.3"

[dependencies.json_macros]
version = "^0.3"
default-features = false
features = ["with-serde"]

Your crate will also need to link with serde_json and use it in any submodule that uses the json!() macro.

extern crate serde_json;

// ...

mod foo {
    use serde_json;
    // ...
}

Example

#![feature(plugin)]
#![plugin(json_macros)]

extern crate serde_json;

pub fn main() {
    let x = 123i32;
    println!("{}", serde_json::to_string_pretty(&json!({ // object literal
        "foo": "foooooo", // string literal keys and values
        "bar": [true, null, 123, 123.4], // array, boolean, null, numeric literals
        "quux": { // nest as deeply as you like
          "a": [1, 2, 3, 4],
          "b": { "a": null },
          "c": false
        },
        "waldo": (192 - x) // wrap in parens to splice ToJson expressions directly
    })).unwrap());
}

More Repositories

1

rethinkdb-rs

(dead, do not use, contact me if you want the crate name) in-progress RethinkDB client in Rust
Rust
17
star
2

fbaas

FizzBuzz as a Service: answer inane interview questions with ease!
10
star
3

dark-souls.el

Emacs Lisp port of Dark Souls
Emacs Lisp
8
star
4

gerrit-ref-updated-hudson

Cleverly named project which provides a useful ref-updated hook for Gerrit to trigger a Hudson build.
Python
3
star
5

yasuc

yet another sprunge.us clone
Go
3
star
6

rust-hidapi

Rust bindings for hidapi
C
2
star
7

doc_file

Move your Rust crate's documentation into external files.
Rust
2
star
8

mdrun-rerun

Just me playing around with mdrun -rerun
Shell
2
star
9

jquery.joyce.js

stress-test your layouts
JavaScript
2
star
10

gopher-rs

Rust
2
star
11

sprunge.el

emacs package to upload text to sprunge.us, the command line pastebin
Emacs Lisp
2
star
12

jquery.barebox.js

Barely-there lightbox plugin for jQuery.
JavaScript
1
star
13

quagmire

Rust
1
star
14

automatra

an elementary cellular automata web service built on Sinatra
Ruby
1
star
15

ruleN

elementary cellular automata with javascript + HTML
JavaScript
1
star
16

multimodifier-example

Rust
1
star
17

land-of-lisp

my journey through the land of lisp
Common Lisp
1
star
18

restbook

A RESTful guestbook engine for Rails 3.1.
Ruby
1
star
19

drinkify-proxy

a JSON proxy for Drinkify
Ruby
1
star
20

custard

Rust
1
star
21

yummetro

Metro-accessible restaurants, shops, nightlife, and more in Los Angeles.
Ruby
1
star
22

edn-rs

Rust
1
star
23

sublime-text-mode

care package for erstwhile Sublime Text users
Emacs Lisp
1
star
24

resume-old

my resume source files
Ruby
1
star