Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

JSON

The json format serializes your struct to JSON using serde_json. This requires the json feature flag.

Setup

Important

The json feature flag must be enabled and the struct must also derive serde::Serialize. Without both, the json format flag will not compile.

Enable the json feature in your Cargo.toml:

[dependencies]
moxy = { version = "0.0.4", features = ["json"] }
serde = { version = "1", features = ["derive"] }

Your struct must derive both Display and serde::Serialize:

use moxy::Display;

#[derive(Display, serde::Serialize)]
#[moxy(display(json))]
struct User {
    name: String,
    age: i32,
}

Named Structs

Named structs produce JSON objects:

#[derive(Display, serde::Serialize)]
#[moxy(display(json))]
struct User {
    name: String,
    age: i32,
}

let user = User { name: "John".into(), age: 30 };
// {"age":30,"name":"John"}

Tuple Structs

Tuple structs produce JSON arrays:

#[derive(Display, serde::Serialize)]
#[moxy(display(json))]
struct Pair(String, i32);

let pair = Pair("hello".into(), 42);
// ["hello",42]

Pretty JSON

Combine with pretty for indented output:

#[derive(Display, serde::Serialize)]
#[moxy(display(json, pretty))]
struct User {
    name: String,
    age: i32,
}

let user = User { name: "John".into(), age: 30 };
// {
//   "age": 30,
//   "name": "John"
// }

Skipping Fields

Use #[moxy(display(skip))] to exclude fields from JSON output:

#[derive(Display, serde::Serialize)]
#[moxy(display(json))]
struct User {
    name: String,
    #[moxy(display(skip))]
    secret: String,
}

let user = User { name: "John".into(), secret: "hunter2".into() };
// {"name":"John"}

Field Aliases

Field aliases are applied to JSON keys:

#[derive(Display, serde::Serialize)]
#[moxy(display(json))]
struct User {
    #[moxy(display(alias = "full_name"))]
    name: String,
}

let user = User { name: "John".into() };
// {"full_name":"John"}