Macro json::object [] [src]

macro_rules! object {
    (
      let decoder = $dec: ident;
      let buffer  = $buf: expr;
      $T: ident {
          $($name: ident: $modus: ident. $key: expr => $action: expr),+
      }
    ) => { ... };
    (
      let decoder = $dec: ident;
      $T: ident {
          $($name: ident: $modus: ident. $key: expr => $action: expr),+
      }
    ) => { ... };
}

Macro to support declarative decoding into struct types.

Optionally object! accepts a Utf8Buffer to use when decoding object keys.

Example:

#[macro_use] extern crate json;

use json::Decoder;

#[derive(Debug, PartialEq)]
struct Point {
    x: u32,
    y: u32
}

let mut d = Decoder::default(r#"{"x": 0, "y": 1}"#.chars());

let p = object! {
    let decoder = d;
    Point {
        x: req. "x" => d.u32(),
        y: req. "y" => d.u32()
    }
};

assert_eq!(Some(Point { x: 0, y: 1}), p.ok());