facet-json 0.46.1

JSON serialization for facet using the new format architecture
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Tests for format-specific proxy attributes.
//!
//! This tests the `#[facet(json::proxy = ...)]` syntax for format-specific proxy types.

use facet::Facet;
use facet_json::{from_str, to_string};
use facet_testhelpers::test;

/// A proxy type that formats values as hex strings.
#[derive(Facet, Clone, Debug)]
#[facet(transparent)]
pub struct HexString(pub String);

/// A proxy type that formats values as binary strings.
#[derive(Facet, Clone, Debug)]
#[facet(transparent)]
pub struct BinaryString(pub String);

/// A type that uses different proxies for different formats.
/// - For JSON, the value is serialized as a hex string
/// - For other formats (without format_namespace), use the default proxy
#[derive(Facet, Debug, Clone, PartialEq)]
pub struct FormatAwareValue {
    pub name: String,
    #[facet(json::proxy = HexString)]
    #[facet(proxy = BinaryString)]
    pub value: u32,
}

// JSON proxy conversion: u32 <-> hex string
impl TryFrom<HexString> for u32 {
    type Error = std::num::ParseIntError;
    fn try_from(proxy: HexString) -> Result<Self, Self::Error> {
        let s = proxy.0.trim_start_matches("0x").trim_start_matches("0X");
        u32::from_str_radix(s, 16)
    }
}

impl From<&u32> for HexString {
    fn from(v: &u32) -> Self {
        HexString(format!("0x{:x}", v))
    }
}

// Default proxy conversion: u32 <-> binary string
impl TryFrom<BinaryString> for u32 {
    type Error = std::num::ParseIntError;
    fn try_from(proxy: BinaryString) -> Result<Self, Self::Error> {
        u32::from_str_radix(proxy.0.trim_start_matches("0b"), 2)
    }
}

impl From<&u32> for BinaryString {
    fn from(v: &u32) -> Self {
        BinaryString(format!("0b{:b}", v))
    }
}

#[test]
fn test_format_specific_proxy_serialization() {
    let data = FormatAwareValue {
        name: "test".to_string(),
        value: 255,
    };

    // JSON should use the hex proxy (json::proxy takes precedence)
    let json = to_string(&data).unwrap();
    assert!(
        json.contains("0xff"),
        "JSON should use hex format, got: {json}"
    );
}

#[test]
fn test_hex_string_conversion() {
    // Test that our TryFrom works correctly
    let hex = HexString("0x1a".to_string());
    let value: u32 = hex.try_into().unwrap();
    assert_eq!(value, 0x1a);
}

#[test]
fn test_format_specific_proxy_deserialization() {
    let json = r#"{"name":"test","value":"0x1a"}"#;
    let data: FormatAwareValue = from_str(json).unwrap();

    assert_eq!(data.name, "test");
    assert_eq!(data.value, 0x1a);
}

/// A struct that only has a format-specific proxy (no fallback).
#[derive(Facet, Debug, Clone, PartialEq)]
pub struct JsonOnlyProxy {
    pub label: String,
    #[facet(json::proxy = HexString)]
    pub id: u32,
}

#[test]
fn test_json_only_proxy_roundtrip() {
    let original = JsonOnlyProxy {
        label: "item".to_string(),
        id: 0xbeef,
    };

    let json = to_string(&original).unwrap();
    assert!(
        json.contains("0xbeef"),
        "JSON should use hex format, got: {json}"
    );

    let roundtripped: JsonOnlyProxy = from_str(&json).unwrap();
    assert_eq!(original, roundtripped);
}

/// Test that format-specific proxy shapes are correctly stored in the Field.
#[test]
fn test_format_proxy_field_metadata() {
    use facet::Facet;
    use facet_core::{Type, UserType};

    let shape = <FormatAwareValue as Facet>::SHAPE;

    // Get the struct type
    let struct_type = match shape.ty {
        Type::User(UserType::Struct(s)) => s,
        _ => panic!("Expected struct type, got {:?}", shape.ty),
    };

    // Find the "value" field
    let value_field = struct_type
        .fields
        .iter()
        .find(|f| f.name == "value")
        .expect("Should have value field");

    // Should have format_proxies
    assert!(
        !value_field.format_proxies.is_empty(),
        "Should have format-specific proxies"
    );

    // Should have one for "json"
    let json_proxy = value_field.format_proxy("json");
    assert!(json_proxy.is_some(), "Should have json proxy");

    // Should also have the default proxy
    assert!(value_field.proxy.is_some(), "Should have default proxy");

    // effective_proxy with "json" should return the json-specific one
    let effective_json = value_field.effective_proxy(Some("json"));
    assert!(effective_json.is_some());

    // effective_proxy with "xml" (no specific proxy) should fall back to default
    let effective_xml = value_field.effective_proxy(Some("xml"));
    assert!(effective_xml.is_some(), "Should fall back to default proxy");

    // They should be different (json-specific vs default)
    assert_ne!(
        effective_json.map(|p| p.shape.id),
        effective_xml.map(|p| p.shape.id),
        "JSON and XML should use different proxies"
    );
}

// =============================================================================
// Container-level format-specific proxy tests
// =============================================================================

/// A wrapper type that serializes to JSON as a hex string representation.
/// The container itself uses a proxy for the entire type.
#[derive(Facet, Debug, Clone, PartialEq)]
#[facet(transparent)]
pub struct JsonHexNumber(pub u32);

/// Proxy type for container-level JSON serialization.
#[derive(Facet, Clone, Debug)]
#[facet(transparent)]
pub struct JsonNumberProxy(pub String);

/// Proxy type for container-level fallback serialization.
#[derive(Facet, Clone, Debug)]
#[facet(transparent)]
pub struct DefaultNumberProxy(pub String);

/// A container type that uses different proxies at the container level.
/// - For JSON format: uses JsonNumberProxy (hex representation)
/// - For other formats: uses DefaultNumberProxy (decimal representation)
#[derive(Facet, Debug, Clone, PartialEq)]
#[facet(json::proxy = JsonNumberProxy)]
#[facet(proxy = DefaultNumberProxy)]
pub struct ContainerFormatProxy {
    pub inner: u32,
}

// Container-level JSON proxy conversions: ContainerFormatProxy <-> JsonNumberProxy
impl TryFrom<JsonNumberProxy> for ContainerFormatProxy {
    type Error = std::num::ParseIntError;
    fn try_from(proxy: JsonNumberProxy) -> Result<Self, Self::Error> {
        let s = proxy.0.trim_start_matches("0x").trim_start_matches("0X");
        let inner = u32::from_str_radix(s, 16)?;
        Ok(ContainerFormatProxy { inner })
    }
}

impl From<&ContainerFormatProxy> for JsonNumberProxy {
    fn from(v: &ContainerFormatProxy) -> Self {
        JsonNumberProxy(format!("0x{:x}", v.inner))
    }
}

// Container-level default proxy conversions: ContainerFormatProxy <-> DefaultNumberProxy
impl TryFrom<DefaultNumberProxy> for ContainerFormatProxy {
    type Error = std::num::ParseIntError;
    fn try_from(proxy: DefaultNumberProxy) -> Result<Self, Self::Error> {
        let inner = proxy.0.parse::<u32>()?;
        Ok(ContainerFormatProxy { inner })
    }
}

impl From<&ContainerFormatProxy> for DefaultNumberProxy {
    fn from(v: &ContainerFormatProxy) -> Self {
        DefaultNumberProxy(format!("{}", v.inner))
    }
}

#[test]
fn test_container_format_specific_proxy_serialization() {
    let data = ContainerFormatProxy { inner: 255 };

    // JSON should use the container-level json::proxy (hex format)
    let json = to_string(&data).unwrap();
    assert!(
        json.contains("0xff"),
        "JSON should use hex format via container proxy, got: {json}"
    );
}

#[test]
fn test_container_format_specific_proxy_deserialization() {
    let json = r#""0x1a""#;
    let data: ContainerFormatProxy = from_str(json).unwrap();

    assert_eq!(data.inner, 0x1a);
}

#[test]
fn test_container_format_specific_proxy_roundtrip() {
    let original = ContainerFormatProxy { inner: 0xbeef };

    let json = to_string(&original).unwrap();
    assert!(
        json.contains("0xbeef"),
        "JSON should use hex format, got: {json}"
    );

    let roundtripped: ContainerFormatProxy = from_str(&json).unwrap();
    assert_eq!(original, roundtripped);
}

#[test]
fn test_container_format_proxy_shape_metadata() {
    use facet::Facet;

    let shape = <ContainerFormatProxy as Facet>::SHAPE;

    // Should have format_proxies at the Shape level
    assert!(
        !shape.format_proxies.is_empty(),
        "Should have container-level format-specific proxies"
    );

    // Should have one for "json"
    let json_proxy = shape.format_proxy("json");
    assert!(
        json_proxy.is_some(),
        "Should have json proxy at container level"
    );

    // Should also have the default proxy
    assert!(
        shape.proxy.is_some(),
        "Should have default container-level proxy"
    );

    // effective_proxy with "json" should return the json-specific one
    let effective_json = shape.effective_proxy(Some("json"));
    assert!(effective_json.is_some());

    // effective_proxy with "xml" (no specific proxy) should fall back to default
    let effective_xml = shape.effective_proxy(Some("xml"));
    assert!(effective_xml.is_some(), "Should fall back to default proxy");

    // They should be different (json-specific vs default)
    assert_ne!(
        effective_json.map(|p| p.shape.id),
        effective_xml.map(|p| p.shape.id),
        "JSON and XML should use different container proxies"
    );
}

/// A container with only a format-specific proxy (no default).
#[derive(Facet, Debug, Clone, PartialEq)]
#[facet(json::proxy = JsonNumberProxy)]
pub struct JsonOnlyContainerProxy {
    pub inner: u32,
}

// Conversions for JsonOnlyContainerProxy
impl TryFrom<JsonNumberProxy> for JsonOnlyContainerProxy {
    type Error = std::num::ParseIntError;
    fn try_from(proxy: JsonNumberProxy) -> Result<Self, Self::Error> {
        let s = proxy.0.trim_start_matches("0x").trim_start_matches("0X");
        let inner = u32::from_str_radix(s, 16)?;
        Ok(JsonOnlyContainerProxy { inner })
    }
}

impl From<&JsonOnlyContainerProxy> for JsonNumberProxy {
    fn from(v: &JsonOnlyContainerProxy) -> Self {
        JsonNumberProxy(format!("0x{:x}", v.inner))
    }
}

#[test]
fn test_json_only_container_proxy_roundtrip() {
    let original = JsonOnlyContainerProxy { inner: 0xcafe };

    let json = to_string(&original).unwrap();
    assert!(
        json.contains("0xcafe"),
        "JSON should use hex format, got: {json}"
    );

    let roundtripped: JsonOnlyContainerProxy = from_str(&json).unwrap();
    assert_eq!(original, roundtripped);
}

#[test]
fn test_json_only_container_proxy_metadata() {
    use facet::Facet;

    let shape = <JsonOnlyContainerProxy as Facet>::SHAPE;

    // Should have format_proxies at the Shape level
    assert!(
        !shape.format_proxies.is_empty(),
        "Should have container-level format-specific proxies"
    );

    // Should have one for "json"
    let json_proxy = shape.format_proxy("json");
    assert!(
        json_proxy.is_some(),
        "Should have json proxy at container level"
    );

    // Should NOT have a default proxy
    assert!(
        shape.proxy.is_none(),
        "Should NOT have default container-level proxy"
    );

    // effective_proxy with "json" should return the json-specific one
    let effective_json = shape.effective_proxy(Some("json"));
    assert!(effective_json.is_some());

    // effective_proxy with "xml" (no specific proxy) should return None (no fallback)
    let effective_xml = shape.effective_proxy(Some("xml"));
    assert!(
        effective_xml.is_none(),
        "Should NOT have fallback proxy for xml"
    );
}

// =============================================================================
// Container-level proxy used as a field in another struct
// Regression test for https://github.com/facet-rs/facet/issues/1825
// =============================================================================

/// A proxy type that wraps strings (uses FromStr/Display).
#[derive(Facet, Clone, Debug)]
#[facet(transparent)]
pub struct StringRepr(pub String);

impl TryFrom<StringRepr> for JsonConstValue {
    type Error = &'static str;
    fn try_from(value: StringRepr) -> Result<Self, Self::Error> {
        value.0.parse()
    }
}

impl From<&JsonConstValue> for StringRepr {
    fn from(_value: &JsonConstValue) -> Self {
        StringRepr("CONST_VALUE".to_string())
    }
}

/// A zero-sized type that always serializes to a specific constant string.
/// The proxy is defined at the container level.
#[derive(Debug, Default, Clone, Copy, Facet, PartialEq)]
#[facet(json::proxy = StringRepr)]
pub struct JsonConstValue;

impl core::fmt::Display for JsonConstValue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "CONST_VALUE")
    }
}

impl core::str::FromStr for JsonConstValue {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "CONST_VALUE" {
            Ok(Self)
        } else {
            Err("expected `CONST_VALUE`")
        }
    }
}

/// A struct that uses JsonConstValue as a field.
/// The proxy is defined on JsonConstValue (container level), not on this field.
#[derive(Facet, Debug, PartialEq)]
struct StructWithContainerProxyField {
    name: String,
    const_val: JsonConstValue,
}

/// Test that container-level proxies work when the type is used as a field.
/// This is a regression test for <https://github.com/facet-rs/facet/issues/1825>.
#[test]
fn test_container_level_proxy_in_field_deserialization() {
    let json = r#"{"name":"test","const_val":"CONST_VALUE"}"#;
    let data: StructWithContainerProxyField = from_str(json).unwrap();
    assert_eq!(data.name, "test");
    assert_eq!(data.const_val, JsonConstValue);
}

/// Test serialization also works with container-level proxies.
#[test]
fn test_container_level_proxy_in_field_serialization() {
    let data = StructWithContainerProxyField {
        name: "test".to_string(),
        const_val: JsonConstValue,
    };
    let json = to_string(&data).unwrap();
    assert!(
        json.contains("CONST_VALUE"),
        "JSON should contain 'CONST_VALUE', got: {json}"
    );
}

/// Test round-trip with container-level proxy in a field.
#[test]
fn test_container_level_proxy_in_field_roundtrip() {
    let original = StructWithContainerProxyField {
        name: "example".to_string(),
        const_val: JsonConstValue,
    };
    let json = to_string(&original).unwrap();
    let roundtripped: StructWithContainerProxyField = from_str(&json).unwrap();
    assert_eq!(original, roundtripped);
}