oxiproto-json 0.1.0

Canonical Protobuf-JSON mapping for OxiProto (camelCase, base64 bytes, RFC3339 timestamps)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use std::collections::HashMap;

use base64::{engine::general_purpose::STANDARD, Engine as _};
use prost::bytes::Bytes;
use prost_reflect::{
    DescriptorPool, DynamicMessage, FieldDescriptor, Kind, MapKey, MessageDescriptor, Value,
};
use serde_json::Value as JsonValue;

use crate::codec::JsonCodec;

/// Errors produced by [`from_json`].
#[derive(Debug)]
pub enum JsonError {
    /// The JSON value for a field had an incompatible type.
    WrongType {
        /// Name of the field (or context) where the error occurred.
        field: String,
        /// Expected type description.
        expected: &'static str,
        /// Short description of what was actually found.
        got: String,
    },
    /// A field name present in the JSON object was not found in the descriptor.
    UnknownField(String),
    /// A scalar value could not be parsed or decoded.
    MalformedValue(String),
}

impl std::fmt::Display for JsonError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JsonError::WrongType {
                field,
                expected,
                got,
            } => {
                write!(f, "field '{field}': expected {expected}, got {got}")
            }
            JsonError::UnknownField(name) => write!(f, "unknown field '{name}'"),
            JsonError::MalformedValue(msg) => write!(f, "malformed value: {msg}"),
        }
    }
}

impl std::error::Error for JsonError {}

impl From<oxiproto_core::OxiProtoError> for JsonError {
    fn from(e: oxiproto_core::OxiProtoError) -> Self {
        JsonError::MalformedValue(e.to_string())
    }
}

impl From<JsonError> for oxiproto_core::OxiProtoError {
    fn from(e: JsonError) -> Self {
        oxiproto_core::OxiProtoError::ParseError(e.to_string())
    }
}

/// Decode a [`serde_json::Value`] into a [`DynamicMessage`] following the
/// canonical Protobuf-JSON mapping.
///
/// The `descriptor` must match the expected message type.  Use
/// [`JsonCodec::default()`] for standard behaviour.
///
/// # Deferred (not yet implemented)
/// - `google.protobuf.Any`: decoded as an empty message.
/// - NaN / Infinity strings for float fields.
/// - `google.protobuf.Struct`, `Value`, `ListValue`: decoded as regular
///   messages.
///
/// # Errors
///
/// Returns [`JsonError`] on type mismatches, unknown fields, or base64 decode
/// failures.
pub fn from_json(
    value: &JsonValue,
    descriptor: &MessageDescriptor,
    codec: &JsonCodec,
) -> Result<DynamicMessage, JsonError> {
    from_json_message(value, descriptor, codec)
}

fn from_json_message(
    value: &JsonValue,
    descriptor: &MessageDescriptor,
    codec: &JsonCodec,
) -> Result<DynamicMessage, JsonError> {
    let full_name = descriptor.full_name();

    // Well-Known Type special decoding
    match full_name {
        "google.protobuf.Timestamp" => return decode_timestamp(value, descriptor),
        "google.protobuf.Duration" => return decode_duration(value, descriptor),
        _ => {}
    }

    let JsonValue::Object(obj) = value else {
        return Err(JsonError::WrongType {
            field: descriptor.full_name().to_owned(),
            expected: "object",
            got: json_type_name(value),
        });
    };

    let mut msg = DynamicMessage::new(descriptor.clone());

    for (json_key, json_val) in obj {
        // Try camelCase name first, then proto name
        let field_desc = descriptor
            .get_field_by_json_name(json_key)
            .or_else(|| descriptor.get_field_by_name(json_key));

        let field_desc = match field_desc {
            Some(f) => f,
            None => {
                return Err(JsonError::UnknownField(json_key.clone()));
            }
        };

        let value = decode_field_value(json_val, &field_desc, descriptor.parent_pool(), codec)?;
        msg.try_set_field(&field_desc, value).map_err(|e| {
            JsonError::MalformedValue(format!(
                "failed to set field '{}': {}",
                field_desc.name(),
                e
            ))
        })?;
    }

    Ok(msg)
}

fn decode_field_value(
    json_val: &JsonValue,
    field_desc: &FieldDescriptor,
    pool: &DescriptorPool,
    codec: &JsonCodec,
) -> Result<Value, JsonError> {
    let field_name = field_desc.name().to_owned();

    if field_desc.is_map() {
        return decode_map(json_val, field_desc, pool, codec, &field_name);
    }

    if field_desc.is_list() {
        return decode_list(json_val, field_desc, pool, codec, &field_name);
    }

    decode_scalar(json_val, &field_desc.kind(), pool, codec, &field_name)
}

fn decode_list(
    json_val: &JsonValue,
    field_desc: &FieldDescriptor,
    pool: &DescriptorPool,
    codec: &JsonCodec,
    field_name: &str,
) -> Result<Value, JsonError> {
    let JsonValue::Array(arr) = json_val else {
        return Err(JsonError::WrongType {
            field: field_name.to_owned(),
            expected: "array",
            got: json_type_name(json_val),
        });
    };

    let kind = field_desc.kind();
    let mut items = Vec::with_capacity(arr.len());
    for item in arr {
        items.push(decode_scalar(item, &kind, pool, codec, field_name)?);
    }
    Ok(Value::List(items))
}

fn decode_map(
    json_val: &JsonValue,
    field_desc: &FieldDescriptor,
    pool: &DescriptorPool,
    codec: &JsonCodec,
    field_name: &str,
) -> Result<Value, JsonError> {
    let JsonValue::Object(obj) = json_val else {
        return Err(JsonError::WrongType {
            field: field_name.to_owned(),
            expected: "object",
            got: json_type_name(json_val),
        });
    };

    let Kind::Message(entry_desc) = field_desc.kind() else {
        return Err(JsonError::MalformedValue(format!(
            "map field '{field_name}' has non-message entry descriptor"
        )));
    };

    let key_field = entry_desc.map_entry_key_field();
    let value_field = entry_desc.map_entry_value_field();

    let mut map: HashMap<MapKey, Value> = HashMap::new();
    for (k_str, v_json) in obj {
        let key_val = decode_map_key(k_str, &key_field.kind(), field_name)?;
        let val = decode_scalar(v_json, &value_field.kind(), pool, codec, field_name)?;
        map.insert(key_val, val);
    }
    Ok(Value::Map(map))
}

fn decode_map_key(s: &str, kind: &Kind, field_name: &str) -> Result<MapKey, JsonError> {
    match kind {
        Kind::String => Ok(MapKey::String(s.to_owned())),
        Kind::Bool => match s {
            "true" => Ok(MapKey::Bool(true)),
            "false" => Ok(MapKey::Bool(false)),
            other => Err(JsonError::MalformedValue(format!(
                "field '{field_name}': invalid bool map key '{other}'"
            ))),
        },
        Kind::Int32 | Kind::Sint32 | Kind::Sfixed32 => {
            s.parse::<i32>().map(MapKey::I32).map_err(|_| {
                JsonError::MalformedValue(format!("field '{field_name}': bad i32 key '{s}'"))
            })
        }
        Kind::Int64 | Kind::Sint64 | Kind::Sfixed64 => {
            s.parse::<i64>().map(MapKey::I64).map_err(|_| {
                JsonError::MalformedValue(format!("field '{field_name}': bad i64 key '{s}'"))
            })
        }
        Kind::Uint32 | Kind::Fixed32 => s.parse::<u32>().map(MapKey::U32).map_err(|_| {
            JsonError::MalformedValue(format!("field '{field_name}': bad u32 key '{s}'"))
        }),
        Kind::Uint64 | Kind::Fixed64 => s.parse::<u64>().map(MapKey::U64).map_err(|_| {
            JsonError::MalformedValue(format!("field '{field_name}': bad u64 key '{s}'"))
        }),
        other => Err(JsonError::MalformedValue(format!(
            "field '{field_name}': unsupported map key kind {:?}",
            other
        ))),
    }
}

fn decode_scalar(
    json_val: &JsonValue,
    kind: &Kind,
    _pool: &DescriptorPool,
    codec: &JsonCodec,
    field_name: &str,
) -> Result<Value, JsonError> {
    match kind {
        Kind::Bool => match json_val {
            JsonValue::Bool(b) => Ok(Value::Bool(*b)),
            other => Err(JsonError::WrongType {
                field: field_name.to_owned(),
                expected: "bool",
                got: json_type_name(other),
            }),
        },

        Kind::Int32 | Kind::Sint32 | Kind::Sfixed32 => {
            let n = json_number_as_i32(json_val, field_name)?;
            Ok(Value::I32(n))
        }

        Kind::Uint32 | Kind::Fixed32 => {
            let n = json_number_as_u32(json_val, field_name)?;
            Ok(Value::U32(n))
        }

        // i64/u64 accept either JSON number or JSON string (lenient-in)
        Kind::Int64 | Kind::Sint64 | Kind::Sfixed64 => {
            let n = json_number_or_string_as_i64(json_val, field_name)?;
            Ok(Value::I64(n))
        }

        Kind::Uint64 | Kind::Fixed64 => {
            let n = json_number_or_string_as_u64(json_val, field_name)?;
            Ok(Value::U64(n))
        }

        Kind::Float => match json_val {
            JsonValue::Number(n) => {
                let f = n.as_f64().unwrap_or(0.0) as f32;
                Ok(Value::F32(f))
            }
            other => Err(JsonError::WrongType {
                field: field_name.to_owned(),
                expected: "number",
                got: json_type_name(other),
            }),
        },

        Kind::Double => match json_val {
            JsonValue::Number(n) => {
                let f = n.as_f64().unwrap_or(0.0);
                Ok(Value::F64(f))
            }
            other => Err(JsonError::WrongType {
                field: field_name.to_owned(),
                expected: "number",
                got: json_type_name(other),
            }),
        },

        Kind::String => match json_val {
            JsonValue::String(s) => Ok(Value::String(s.clone())),
            other => Err(JsonError::WrongType {
                field: field_name.to_owned(),
                expected: "string",
                got: json_type_name(other),
            }),
        },

        Kind::Bytes => {
            let s = json_val.as_str().ok_or_else(|| JsonError::WrongType {
                field: field_name.to_owned(),
                expected: "base64 string",
                got: json_type_name(json_val),
            })?;
            let bytes = STANDARD.decode(s).map_err(|e| {
                JsonError::MalformedValue(format!("field '{field_name}': base64 decode error: {e}"))
            })?;
            Ok(Value::Bytes(Bytes::from(bytes)))
        }

        Kind::Enum(enum_desc) => {
            // Accept string name or integer
            match json_val {
                JsonValue::String(name) => {
                    if let Some(ev) = enum_desc.get_value_by_name(name) {
                        Ok(Value::EnumNumber(ev.number()))
                    } else {
                        Err(JsonError::MalformedValue(format!(
                            "field '{field_name}': unknown enum value '{name}'"
                        )))
                    }
                }
                JsonValue::Number(n) => {
                    let num = n.as_i64().ok_or_else(|| {
                        JsonError::MalformedValue(format!(
                            "field '{field_name}': enum number out of i64 range"
                        ))
                    })? as i32;
                    Ok(Value::EnumNumber(num))
                }
                other => Err(JsonError::WrongType {
                    field: field_name.to_owned(),
                    expected: "string or number",
                    got: json_type_name(other),
                }),
            }
        }

        Kind::Message(msg_desc) => {
            let nested = from_json_message(json_val, msg_desc, codec)?;
            Ok(Value::Message(nested))
        }
    }
}

// ---------------------------------------------------------------------------
// Number helpers
// ---------------------------------------------------------------------------

fn json_number_as_i32(v: &JsonValue, field: &str) -> Result<i32, JsonError> {
    match v {
        JsonValue::Number(n) => n
            .as_i64()
            .and_then(|n| i32::try_from(n).ok())
            .ok_or_else(|| {
                JsonError::MalformedValue(format!("field '{field}': value out of i32 range"))
            }),
        other => Err(JsonError::WrongType {
            field: field.to_owned(),
            expected: "number",
            got: json_type_name(other),
        }),
    }
}

fn json_number_as_u32(v: &JsonValue, field: &str) -> Result<u32, JsonError> {
    match v {
        JsonValue::Number(n) => n
            .as_u64()
            .and_then(|n| u32::try_from(n).ok())
            .ok_or_else(|| {
                JsonError::MalformedValue(format!("field '{field}': value out of u32 range"))
            }),
        other => Err(JsonError::WrongType {
            field: field.to_owned(),
            expected: "number",
            got: json_type_name(other),
        }),
    }
}

fn json_number_or_string_as_i64(v: &JsonValue, field: &str) -> Result<i64, JsonError> {
    match v {
        JsonValue::Number(n) => n.as_i64().ok_or_else(|| {
            JsonError::MalformedValue(format!("field '{field}': value out of i64 range"))
        }),
        JsonValue::String(s) => s.parse::<i64>().map_err(|_| {
            JsonError::MalformedValue(format!("field '{field}': cannot parse '{s}' as i64"))
        }),
        other => Err(JsonError::WrongType {
            field: field.to_owned(),
            expected: "number or string",
            got: json_type_name(other),
        }),
    }
}

fn json_number_or_string_as_u64(v: &JsonValue, field: &str) -> Result<u64, JsonError> {
    match v {
        JsonValue::Number(n) => n.as_u64().ok_or_else(|| {
            JsonError::MalformedValue(format!("field '{field}': value out of u64 range"))
        }),
        JsonValue::String(s) => s.parse::<u64>().map_err(|_| {
            JsonError::MalformedValue(format!("field '{field}': cannot parse '{s}' as u64"))
        }),
        other => Err(JsonError::WrongType {
            field: field.to_owned(),
            expected: "number or string",
            got: json_type_name(other),
        }),
    }
}

// ---------------------------------------------------------------------------
// WKT decoders
// ---------------------------------------------------------------------------

fn decode_timestamp(
    value: &JsonValue,
    descriptor: &MessageDescriptor,
) -> Result<DynamicMessage, JsonError> {
    let s = value.as_str().ok_or_else(|| JsonError::WrongType {
        field: "Timestamp".to_owned(),
        expected: "RFC3339 string",
        got: json_type_name(value),
    })?;

    let dt = chrono::DateTime::parse_from_rfc3339(s)
        .map_err(|e| JsonError::MalformedValue(format!("invalid RFC3339 timestamp '{s}': {e}")))?;

    let mut msg = DynamicMessage::new(descriptor.clone());
    let seconds = dt.timestamp();
    let nanos = dt.timestamp_subsec_nanos() as i32;

    let secs_field = descriptor
        .get_field_by_name("seconds")
        .ok_or_else(|| JsonError::MalformedValue("Timestamp missing 'seconds' field".to_owned()))?;
    let nanos_field = descriptor
        .get_field_by_name("nanos")
        .ok_or_else(|| JsonError::MalformedValue("Timestamp missing 'nanos' field".to_owned()))?;

    msg.try_set_field(&secs_field, Value::I64(seconds))
        .map_err(|e| JsonError::MalformedValue(e.to_string()))?;
    msg.try_set_field(&nanos_field, Value::I32(nanos))
        .map_err(|e| JsonError::MalformedValue(e.to_string()))?;

    Ok(msg)
}

fn decode_duration(
    value: &JsonValue,
    descriptor: &MessageDescriptor,
) -> Result<DynamicMessage, JsonError> {
    let s = value.as_str().ok_or_else(|| JsonError::WrongType {
        field: "Duration".to_owned(),
        expected: "duration string",
        got: json_type_name(value),
    })?;

    let s = s.strip_suffix('s').ok_or_else(|| {
        JsonError::MalformedValue(format!("duration string must end with 's': '{s}'"))
    })?;

    let (seconds, nanos) = parse_duration_str(s)
        .map_err(|e| JsonError::MalformedValue(format!("invalid duration '{s}s': {e}")))?;

    let mut msg = DynamicMessage::new(descriptor.clone());
    let secs_field = descriptor
        .get_field_by_name("seconds")
        .ok_or_else(|| JsonError::MalformedValue("Duration missing 'seconds' field".to_owned()))?;
    let nanos_field = descriptor
        .get_field_by_name("nanos")
        .ok_or_else(|| JsonError::MalformedValue("Duration missing 'nanos' field".to_owned()))?;

    msg.try_set_field(&secs_field, Value::I64(seconds))
        .map_err(|e| JsonError::MalformedValue(e.to_string()))?;
    msg.try_set_field(&nanos_field, Value::I32(nanos))
        .map_err(|e| JsonError::MalformedValue(e.to_string()))?;

    Ok(msg)
}

/// Parse a duration string like `"1.5"` or `"-3600"` (without the trailing
/// `s`).  Returns `(seconds, nanos)`.
fn parse_duration_str(s: &str) -> Result<(i64, i32), String> {
    if let Some(dot) = s.find('.') {
        let int_part = &s[..dot];
        let frac_part = &s[dot + 1..];

        let secs: i64 = int_part
            .parse()
            .map_err(|_| format!("invalid integer part '{int_part}'"))?;
        let negative = secs < 0 || int_part.starts_with('-');

        // Pad/truncate frac to 9 digits
        let frac_padded = format!("{:0<9}", &frac_part[..frac_part.len().min(9)]);
        let nanos_abs: i32 = frac_padded
            .parse()
            .map_err(|_| format!("invalid fractional part '{frac_part}'"))?;
        let nanos = if negative { -nanos_abs } else { nanos_abs };
        Ok((secs, nanos))
    } else {
        let secs: i64 = s.parse().map_err(|_| format!("invalid integer '{s}'"))?;
        Ok((secs, 0))
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn json_type_name(v: &JsonValue) -> String {
    match v {
        JsonValue::Null => "null".to_owned(),
        JsonValue::Bool(_) => "bool".to_owned(),
        JsonValue::Number(_) => "number".to_owned(),
        JsonValue::String(_) => "string".to_owned(),
        JsonValue::Array(_) => "array".to_owned(),
        JsonValue::Object(_) => "object".to_owned(),
    }
}