Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Formatter::write_byte_array #1039

Merged
merged 7 commits into from Jul 12, 2023
Merged

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jul 12, 2023

Example that writes bytes as base64 JSON strings:

// [dependencies]
// base64 = "0.21"
// serde = { version = "1", features = ["derive"] }
// serde_bytes = "0.11"
// serde_json = "1"

use serde::Serialize;
use std::io::{self, Write};

#[derive(Serialize)]
struct Struct {
    #[serde(with = "serde_bytes")]
    buf: Vec<u8>,
}

struct Base64Formatter;

impl serde_json::ser::Formatter for Base64Formatter {
    fn write_byte_array<W>(&mut self, writer: &mut W, value: &[u8]) -> io::Result<()>
    where
        W: ?Sized + io::Write,
    {
        writer.write_all(b"\"")?;
        let engine = &base64::engine::general_purpose::STANDARD_NO_PAD;
        let mut encoder = base64::write::EncoderWriter::new(writer, engine);
        encoder.write_all(value)?;
        let writer = encoder.finish()?;
        writer.write_all(b"\"")
    }
}

fn main() -> serde_json::Result<()> {
    let thing = Struct {
        buf: b".....\xed..\x1e\n.".to_vec(),
    };

    let out = io::stdout();
    let mut ser = serde_json::Serializer::with_formatter(out, Base64Formatter);
    thing.serialize(&mut ser)?;
    Ok(())
}
{"buf":"Li4uLi7tLi4eCi4"}

@dtolnay dtolnay merged commit 42dbd00 into serde-rs:master Jul 12, 2023
13 checks passed
@dtolnay dtolnay deleted the writebytearray branch July 12, 2023 00:19
crapStone added a commit to Calciumdibromid/CaBr2 that referenced this pull request Jul 14, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [serde_json](https://github.com/serde-rs/json) | dependencies | patch | `1.0.100` -> `1.0.102` |

---

### Release Notes

<details>
<summary>serde-rs/json (serde_json)</summary>

### [`v1.0.102`](https://github.com/serde-rs/json/releases/tag/v1.0.102)

[Compare Source](serde-rs/json@v1.0.101...v1.0.102)

-   Add a way to customize the serialization of byte arrays ([#&#8203;1039](serde-rs/json#1039))

### [`v1.0.101`](https://github.com/serde-rs/json/releases/tag/v1.0.101)

[Compare Source](serde-rs/json@v1.0.100...v1.0.101)

-   Allow f32 and f64 as keys in maps ([#&#8203;1027](serde-rs/json#1027), thanks [@&#8203;overdrivenpotato](https://github.com/overdrivenpotato))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44LjIiLCJ1cGRhdGVkSW5WZXIiOiIzNi44LjIiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1960
Reviewed-by: crapStone <crapstone01@gmail.com>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant