File: serde.rs

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (59 lines) | stat: -rw-r--r-- 1,289 bytes parent folder | download | duplicates (54)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#![cfg(all(feature = "serde", feature = "alloc"))]
#![allow(clippy::blacklisted_name)]

use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Foo {
    #[serde(with = "hex")]
    bar: Vec<u8>,
}

#[test]
fn serialize() {
    let foo = Foo {
        bar: vec![1, 10, 100],
    };

    let ser = serde_json::to_string(&foo).expect("serialization failed");
    assert_eq!(ser, r#"{"bar":"010a64"}"#);
}

#[test]
fn deserialize() {
    let foo = Foo {
        bar: vec![1, 10, 100],
    };

    let de: Foo = serde_json::from_str(r#"{"bar":"010a64"}"#).expect("deserialization failed");
    assert_eq!(de, foo);
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Bar {
    #[serde(
        serialize_with = "hex::serialize_upper",
        deserialize_with = "hex::deserialize"
    )]
    foo: Vec<u8>,
}

#[test]
fn serialize_upper() {
    let bar = Bar {
        foo: vec![1, 10, 100],
    };

    let ser = serde_json::to_string(&bar).expect("serialization failed");
    assert_eq!(ser, r#"{"foo":"010A64"}"#);
}

#[test]
fn deserialize_upper() {
    let bar = Bar {
        foo: vec![1, 10, 100],
    };

    let de: Bar = serde_json::from_str(r#"{"foo":"010A64"}"#).expect("deserialization failed");
    assert_eq!(de, bar);
}