Macro js_object

Source
macro_rules! js_object {
    (@for_array [$($elem:expr)*]) => { ... };
    (@for_array [$($elem:expr)*] $val:tt , $($rest:tt)*) => { ... };
    (@for_array [$($elem:expr)*] $val:tt) => { ... };
    (@for_object $obj:ident $key:tt : $val:tt , $($rest:tt)*) => { ... };
    (@for_object $obj:ident $key:tt : $val:tt) => { ... };
    (@for_object $obj:ident $key:tt : $val:expr , $($rest:tt)*) => { ... };
    (@for_object $obj:ident $key:tt : $val:expr) => { ... };
    (@for_object $obj:ident $key:ident , $($rest:tt)*) => { ... };
    (@for_object $obj:ident $key:ident) => { ... };
    (@for_object $obj:ident) => { ... };
    (@obj_insert $obj:ident [ $key:expr ], $val: expr) => { ... };
    (@obj_insert $obj:ident $key:tt, $val: expr) => { ... };
    (null) => { ... };
    ([ $($val:tt)* ]) => { ... };
    ({ $($val:tt)* }) => { ... };
    ($val: expr) => { ... };
}
Expand description

Construct JsonElem with JS syntax

let code = 404;
let error_code = 1234;
let error_description: Option<String> = None;

let value = js_object!({
    code, // Property Shorthand
    success: code == 200, // Expressions
    errors: [
        {
            [error_code]: error_description // Computed Property Names
        }
    ]
});

Any type interpolated into an array element or a object value must implement ToJson trait, while any type interpolated into a object key must implement ToString trait.