1 use std::num::ParseFloatError;
2 use std::num::ParseIntError;
3
4 use protobuf::reflect::EnumDescriptor;
5 use protobuf::reflect::EnumValueDescriptor;
6 use protobuf::reflect::FieldDescriptor;
7 use protobuf::reflect::MessageDescriptor;
8 use protobuf::reflect::ReflectValueBox;
9 use protobuf::reflect::RuntimeFieldType;
10 use protobuf::reflect::RuntimeType;
11 use protobuf::well_known_types::any::Any;
12 use protobuf::well_known_types::duration::Duration;
13 use protobuf::well_known_types::field_mask::FieldMask;
14 use protobuf::well_known_types::struct_;
15 use protobuf::well_known_types::struct_::ListValue;
16 use protobuf::well_known_types::struct_::NullValue;
17 use protobuf::well_known_types::struct_::Struct;
18 use protobuf::well_known_types::struct_::Value;
19 use protobuf::well_known_types::timestamp::Timestamp;
20 use protobuf::well_known_types::wrappers::BoolValue;
21 use protobuf::well_known_types::wrappers::BytesValue;
22 use protobuf::well_known_types::wrappers::DoubleValue;
23 use protobuf::well_known_types::wrappers::FloatValue;
24 use protobuf::well_known_types::wrappers::Int32Value;
25 use protobuf::well_known_types::wrappers::Int64Value;
26 use protobuf::well_known_types::wrappers::StringValue;
27 use protobuf::well_known_types::wrappers::UInt32Value;
28 use protobuf::well_known_types::wrappers::UInt64Value;
29 use protobuf::Enum;
30 use protobuf::MessageDyn;
31 use protobuf::MessageFull;
32 use protobuf_support::lexer::json_number_lit::JsonNumberLit;
33 use protobuf_support::lexer::lexer_impl::Lexer;
34 use protobuf_support::lexer::lexer_impl::LexerError;
35 use protobuf_support::lexer::loc::Loc;
36 use protobuf_support::lexer::parser_language::ParserLanguage;
37 use protobuf_support::lexer::token::Token;
38 use protobuf_support::lexer::tokenizer::Tokenizer;
39 use protobuf_support::lexer::tokenizer::TokenizerError;
40
41 use super::base64;
42 use super::float;
43 use super::rfc_3339;
44 use crate::base64::FromBase64Error;
45 use crate::well_known_wrapper::WellKnownWrapper;
46
47 #[derive(Debug, thiserror::Error)]
48 enum ParseErrorWithoutLocInner {
49 #[error(transparent)]
50 TokenizerError(#[from] TokenizerError),
51 #[error("Unknown field name: `{}`", .0)]
52 UnknownFieldName(String),
53 #[error("Unknown enum variant name: `{}`", .0)]
54 UnknownEnumVariantName(String),
55 #[error(transparent)]
56 FromBase64Error(#[from] FromBase64Error),
57 #[error(transparent)]
58 IncorrectStrLit(#[from] LexerError),
59 #[error("Incorrect duration")]
60 IncorrectDuration,
61 #[error(transparent)]
62 Rfc3339(#[from] rfc_3339::Rfc3339ParseError),
63 #[error(transparent)]
64 ParseIntError(#[from] ParseIntError),
65 #[error(transparent)]
66 ParseFloatError(#[from] ParseFloatError),
67 #[error("Expecting bool")]
68 ExpectingBool,
69 #[error("Expecting string or integer")]
70 ExpectingStrOrInt,
71 #[error("Expecting number")]
72 ExpectingNumber,
73 #[error("Unexpected token")]
74 UnexpectedToken,
75 #[error("Any parsing is not implemented")]
76 AnyParsingIsNotImplemented,
77 #[error("Message not initialized")]
78 MessageNotInitialized,
79 }
80
81 /// JSON parse error.
82 #[derive(Debug, thiserror::Error)]
83 #[error(transparent)]
84 struct ParseErrorWithoutLoc(ParseErrorWithoutLocInner);
85
86 impl From<TokenizerError> for ParseErrorWithoutLoc {
from(e: TokenizerError) -> Self87 fn from(e: TokenizerError) -> Self {
88 ParseErrorWithoutLoc(ParseErrorWithoutLocInner::TokenizerError(e))
89 }
90 }
91
92 impl From<FromBase64Error> for ParseErrorWithoutLoc {
from(e: FromBase64Error) -> Self93 fn from(e: FromBase64Error) -> Self {
94 ParseErrorWithoutLoc(ParseErrorWithoutLocInner::FromBase64Error(e))
95 }
96 }
97
98 impl From<ParseIntError> for ParseErrorWithoutLoc {
from(e: ParseIntError) -> Self99 fn from(e: ParseIntError) -> Self {
100 ParseErrorWithoutLoc(ParseErrorWithoutLocInner::ParseIntError(e))
101 }
102 }
103
104 impl From<ParseFloatError> for ParseErrorWithoutLoc {
from(e: ParseFloatError) -> Self105 fn from(e: ParseFloatError) -> Self {
106 ParseErrorWithoutLoc(ParseErrorWithoutLocInner::ParseFloatError(e))
107 }
108 }
109
110 impl From<rfc_3339::Rfc3339ParseError> for ParseErrorWithoutLoc {
from(e: rfc_3339::Rfc3339ParseError) -> Self111 fn from(e: rfc_3339::Rfc3339ParseError) -> Self {
112 ParseErrorWithoutLoc(ParseErrorWithoutLocInner::Rfc3339(e))
113 }
114 }
115
116 /// JSON parse error
117 #[derive(Debug, thiserror::Error)]
118 #[error("{} at {}", error, loc)]
119 pub struct ParseError {
120 error: ParseErrorWithoutLoc,
121 loc: Loc,
122 }
123
124 type ParseResultWithoutLoc<A> = Result<A, ParseErrorWithoutLoc>;
125 type ParseResult<A> = Result<A, ParseError>;
126
127 #[derive(Clone)]
128 struct Parser<'a> {
129 tokenizer: Tokenizer<'a>,
130 parse_options: ParseOptions,
131 }
132
133 trait FromJsonNumber: PartialEq + Sized {
from_f64(v: f64) -> Self134 fn from_f64(v: f64) -> Self;
to_f64(&self) -> f64135 fn to_f64(&self) -> f64;
from_string(v: &str) -> ParseResultWithoutLoc<Self>136 fn from_string(v: &str) -> ParseResultWithoutLoc<Self>;
137 }
138
139 impl FromJsonNumber for u32 {
from_f64(v: f64) -> Self140 fn from_f64(v: f64) -> Self {
141 v as u32
142 }
143
to_f64(&self) -> f64144 fn to_f64(&self) -> f64 {
145 *self as f64
146 }
147
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>148 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
149 Ok(v.parse()?)
150 }
151 }
152
153 impl FromJsonNumber for u64 {
from_f64(v: f64) -> Self154 fn from_f64(v: f64) -> Self {
155 v as u64
156 }
157
to_f64(&self) -> f64158 fn to_f64(&self) -> f64 {
159 *self as f64
160 }
161
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>162 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
163 Ok(v.parse()?)
164 }
165 }
166
167 impl FromJsonNumber for i32 {
from_f64(v: f64) -> Self168 fn from_f64(v: f64) -> Self {
169 v as i32
170 }
171
to_f64(&self) -> f64172 fn to_f64(&self) -> f64 {
173 *self as f64
174 }
175
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>176 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
177 Ok(v.parse()?)
178 }
179 }
180
181 impl FromJsonNumber for i64 {
from_f64(v: f64) -> Self182 fn from_f64(v: f64) -> Self {
183 v as i64
184 }
185
to_f64(&self) -> f64186 fn to_f64(&self) -> f64 {
187 *self as f64
188 }
189
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>190 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
191 Ok(v.parse()?)
192 }
193 }
194
195 impl FromJsonNumber for f32 {
from_f64(v: f64) -> Self196 fn from_f64(v: f64) -> Self {
197 v as f32
198 }
199
to_f64(&self) -> f64200 fn to_f64(&self) -> f64 {
201 *self as f64
202 }
203
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>204 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
205 if v == float::PROTOBUF_JSON_INF {
206 Ok(f32::INFINITY)
207 } else if v == float::PROTOBUF_JSON_MINUS_INF {
208 Ok(f32::NEG_INFINITY)
209 } else if v == float::PROTOBUF_JSON_NAN {
210 Ok(f32::NAN)
211 } else {
212 Ok(v.parse()?)
213 }
214 }
215 }
216
217 impl FromJsonNumber for f64 {
from_f64(v: f64) -> Self218 fn from_f64(v: f64) -> Self {
219 v
220 }
221
to_f64(&self) -> f64222 fn to_f64(&self) -> f64 {
223 *self
224 }
225
from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc>226 fn from_string(v: &str) -> Result<Self, ParseErrorWithoutLoc> {
227 if v == float::PROTOBUF_JSON_INF {
228 Ok(f64::INFINITY)
229 } else if v == float::PROTOBUF_JSON_MINUS_INF {
230 Ok(f64::NEG_INFINITY)
231 } else if v == float::PROTOBUF_JSON_NAN {
232 Ok(f64::NAN)
233 } else {
234 Ok(v.parse()?)
235 }
236 }
237 }
238
239 impl<'a> Parser<'a> {
read_bool(&mut self) -> ParseResultWithoutLoc<bool>240 fn read_bool(&mut self) -> ParseResultWithoutLoc<bool> {
241 if self.tokenizer.next_ident_if_eq("true")? {
242 Ok(true)
243 } else if self.tokenizer.next_ident_if_eq("false")? {
244 Ok(false)
245 } else {
246 Err(ParseErrorWithoutLoc(
247 ParseErrorWithoutLocInner::ExpectingBool,
248 ))
249 }
250 }
251
parse_bool(&self, s: &str) -> ParseResultWithoutLoc<bool>252 fn parse_bool(&self, s: &str) -> ParseResultWithoutLoc<bool> {
253 if s == "true" {
254 Ok(true)
255 } else if s == "false" {
256 Ok(false)
257 } else {
258 Err(ParseErrorWithoutLoc(
259 ParseErrorWithoutLocInner::ExpectingBool,
260 ))
261 }
262 }
263
read_json_number_opt(&mut self) -> ParseResultWithoutLoc<Option<JsonNumberLit>>264 fn read_json_number_opt(&mut self) -> ParseResultWithoutLoc<Option<JsonNumberLit>> {
265 Ok(self.tokenizer.next_token_if_map(|t| match t {
266 Token::JsonNumber(v) => Some(v.clone()),
267 _ => None,
268 })?)
269 }
270
read_number<V: FromJsonNumber>(&mut self) -> ParseResultWithoutLoc<V>271 fn read_number<V: FromJsonNumber>(&mut self) -> ParseResultWithoutLoc<V> {
272 if let Some(v) = self.read_json_number_opt()? {
273 V::from_string(&v.0)
274 } else if self.tokenizer.lookahead_is_str_lit()? {
275 let v = self.read_string()?;
276 self.parse_number(&v)
277 } else {
278 Err(ParseErrorWithoutLoc(
279 ParseErrorWithoutLocInner::ExpectingNumber,
280 ))
281 }
282 }
283
parse_number<V: FromJsonNumber>(&self, s: &str) -> ParseResultWithoutLoc<V>284 fn parse_number<V: FromJsonNumber>(&self, s: &str) -> ParseResultWithoutLoc<V> {
285 V::from_string(s)
286 }
287
merge_wrapper<W>(&mut self, w: &mut W) -> ParseResultWithoutLoc<()> where W: WellKnownWrapper, W::Underlying: FromJsonNumber,288 fn merge_wrapper<W>(&mut self, w: &mut W) -> ParseResultWithoutLoc<()>
289 where
290 W: WellKnownWrapper,
291 W::Underlying: FromJsonNumber,
292 {
293 *w.get_mut() = self.read_number()?;
294 Ok(())
295 }
296
merge_bool_value(&mut self, w: &mut BoolValue) -> ParseResultWithoutLoc<()>297 fn merge_bool_value(&mut self, w: &mut BoolValue) -> ParseResultWithoutLoc<()> {
298 w.value = self.read_bool()?;
299 Ok(())
300 }
301
merge_string_value(&mut self, w: &mut StringValue) -> ParseResultWithoutLoc<()>302 fn merge_string_value(&mut self, w: &mut StringValue) -> ParseResultWithoutLoc<()> {
303 w.value = self.read_string()?;
304 Ok(())
305 }
306
merge_bytes_value(&mut self, w: &mut BytesValue) -> ParseResultWithoutLoc<()>307 fn merge_bytes_value(&mut self, w: &mut BytesValue) -> ParseResultWithoutLoc<()> {
308 w.value = self.read_bytes()?;
309 Ok(())
310 }
311
read_u32(&mut self) -> ParseResultWithoutLoc<u32>312 fn read_u32(&mut self) -> ParseResultWithoutLoc<u32> {
313 self.read_number()
314 }
315
read_u64(&mut self) -> ParseResultWithoutLoc<u64>316 fn read_u64(&mut self) -> ParseResultWithoutLoc<u64> {
317 self.read_number()
318 }
319
read_i32(&mut self) -> ParseResultWithoutLoc<i32>320 fn read_i32(&mut self) -> ParseResultWithoutLoc<i32> {
321 self.read_number()
322 }
323
read_i64(&mut self) -> ParseResultWithoutLoc<i64>324 fn read_i64(&mut self) -> ParseResultWithoutLoc<i64> {
325 self.read_number()
326 }
327
read_f32(&mut self) -> ParseResultWithoutLoc<f32>328 fn read_f32(&mut self) -> ParseResultWithoutLoc<f32> {
329 self.read_number()
330 }
331
read_f64(&mut self) -> ParseResultWithoutLoc<f64>332 fn read_f64(&mut self) -> ParseResultWithoutLoc<f64> {
333 self.read_number()
334 }
335
read_string(&mut self) -> ParseResultWithoutLoc<String>336 fn read_string(&mut self) -> ParseResultWithoutLoc<String> {
337 let str_lit = self.tokenizer.next_str_lit()?;
338
339 let mut lexer = Lexer::new(&str_lit.escaped, ParserLanguage::Json);
340 let mut r = String::new();
341 while !lexer.eof() {
342 r.push(
343 lexer
344 .next_json_char_value()
345 .map_err(ParseErrorWithoutLocInner::IncorrectStrLit)
346 .map_err(ParseErrorWithoutLoc)?,
347 );
348 }
349 Ok(r)
350 }
351
read_bytes(&mut self) -> ParseResultWithoutLoc<Vec<u8>>352 fn read_bytes(&mut self) -> ParseResultWithoutLoc<Vec<u8>> {
353 let s = self.read_string()?;
354 self.parse_bytes(&s)
355 }
356
parse_bytes(&self, s: &str) -> ParseResultWithoutLoc<Vec<u8>>357 fn parse_bytes(&self, s: &str) -> ParseResultWithoutLoc<Vec<u8>> {
358 Ok(base64::decode(s)?)
359 }
360
read_enum(&mut self, descriptor: &EnumDescriptor) -> ParseResultWithoutLoc<i32>361 fn read_enum(&mut self, descriptor: &EnumDescriptor) -> ParseResultWithoutLoc<i32> {
362 if descriptor.is::<NullValue>() {
363 return Ok(self.read_wk_null_value()?.value());
364 }
365
366 if self.tokenizer.lookahead_is_str_lit()? {
367 let name = self.read_string()?;
368 Ok(self.parse_enum(name, descriptor)?.value())
369 } else if self.tokenizer.lookahead_is_json_number()? {
370 self.read_i32()
371 } else {
372 Err(ParseErrorWithoutLoc(
373 ParseErrorWithoutLocInner::ExpectingStrOrInt,
374 ))
375 }
376 }
377
parse_enum( &self, name: String, descriptor: &EnumDescriptor, ) -> ParseResultWithoutLoc<EnumValueDescriptor>378 fn parse_enum(
379 &self,
380 name: String,
381 descriptor: &EnumDescriptor,
382 ) -> ParseResultWithoutLoc<EnumValueDescriptor> {
383 match descriptor.value_by_name(&name) {
384 Some(v) => Ok(v),
385 None => Err(ParseErrorWithoutLoc(
386 ParseErrorWithoutLocInner::UnknownEnumVariantName(name),
387 )),
388 }
389 }
390
read_wk_null_value(&mut self) -> ParseResultWithoutLoc<NullValue>391 fn read_wk_null_value(&mut self) -> ParseResultWithoutLoc<NullValue> {
392 self.tokenizer.next_ident_expect_eq("null")?;
393 Ok(NullValue::NULL_VALUE)
394 }
395
read_message( &mut self, descriptor: &MessageDescriptor, ) -> ParseResultWithoutLoc<Box<dyn MessageDyn>>396 fn read_message(
397 &mut self,
398 descriptor: &MessageDescriptor,
399 ) -> ParseResultWithoutLoc<Box<dyn MessageDyn>> {
400 let mut m = descriptor.new_instance();
401 self.merge_inner(&mut *m)?;
402 Ok(m)
403 }
404
read_value(&mut self, t: &RuntimeType) -> ParseResultWithoutLoc<ReflectValueBox>405 fn read_value(&mut self, t: &RuntimeType) -> ParseResultWithoutLoc<ReflectValueBox> {
406 match t {
407 RuntimeType::I32 => self.read_i32().map(ReflectValueBox::from),
408 RuntimeType::I64 => self.read_i64().map(ReflectValueBox::from),
409 RuntimeType::U32 => self.read_u32().map(ReflectValueBox::from),
410 RuntimeType::U64 => self.read_u64().map(ReflectValueBox::from),
411 RuntimeType::F32 => self.read_f32().map(ReflectValueBox::from),
412 RuntimeType::F64 => self.read_f64().map(ReflectValueBox::from),
413 RuntimeType::Bool => self.read_bool().map(ReflectValueBox::from),
414 RuntimeType::String => self.read_string().map(ReflectValueBox::from),
415 RuntimeType::VecU8 => self.read_bytes().map(ReflectValueBox::from),
416 RuntimeType::Enum(e) => self
417 .read_enum(&e)
418 .map(|v| ReflectValueBox::Enum(e.clone(), v)),
419 RuntimeType::Message(m) => self.read_message(&m).map(ReflectValueBox::from),
420 }
421 }
422
merge_singular_field( &mut self, message: &mut dyn MessageDyn, field: &FieldDescriptor, t: &RuntimeType, ) -> ParseResultWithoutLoc<()>423 fn merge_singular_field(
424 &mut self,
425 message: &mut dyn MessageDyn,
426 field: &FieldDescriptor,
427 t: &RuntimeType,
428 ) -> ParseResultWithoutLoc<()> {
429 field.set_singular_field(message, self.read_value(t)?);
430 Ok(())
431 }
432
read_list<C>(&mut self, mut read_item: C) -> ParseResultWithoutLoc<()> where C: for<'b> FnMut(&'b mut Self) -> ParseResultWithoutLoc<()>,433 fn read_list<C>(&mut self, mut read_item: C) -> ParseResultWithoutLoc<()>
434 where
435 C: for<'b> FnMut(&'b mut Self) -> ParseResultWithoutLoc<()>,
436 {
437 if self.tokenizer.next_ident_if_eq("null")? {
438 return Ok(());
439 }
440
441 // TODO: better error reporting on wrong field type
442 self.tokenizer.next_symbol_expect_eq('[', "list")?;
443 let mut first = true;
444 while !self.tokenizer.next_symbol_if_eq(']')? {
445 if !first {
446 self.tokenizer.next_symbol_expect_eq(',', "list")?;
447 }
448 first = false;
449
450 read_item(self)?;
451 }
452
453 Ok(())
454 }
455
merge_repeated_field( &mut self, message: &mut dyn MessageDyn, field: &FieldDescriptor, t: &RuntimeType, ) -> ParseResultWithoutLoc<()>456 fn merge_repeated_field(
457 &mut self,
458 message: &mut dyn MessageDyn,
459 field: &FieldDescriptor,
460 t: &RuntimeType,
461 ) -> ParseResultWithoutLoc<()> {
462 let mut repeated = field.mut_repeated(message);
463 repeated.clear();
464
465 self.read_list(|s| {
466 repeated.push(s.read_value(t)?);
467 Ok(())
468 })
469 }
470
merge_wk_list_value(&mut self, list: &mut ListValue) -> ParseResultWithoutLoc<()>471 fn merge_wk_list_value(&mut self, list: &mut ListValue) -> ParseResultWithoutLoc<()> {
472 list.values.clear();
473
474 self.read_list(|s| {
475 list.values.push(s.read_wk_value()?);
476 Ok(())
477 })
478 }
479
read_map<K, Fk, Fi>( &mut self, mut parse_key: Fk, mut read_value_and_insert: Fi, ) -> ParseResultWithoutLoc<()> where Fk: for<'b> FnMut(&Self, String) -> ParseResultWithoutLoc<K>, Fi: for<'b> FnMut(&mut Self, K) -> ParseResultWithoutLoc<()>,480 fn read_map<K, Fk, Fi>(
481 &mut self,
482 mut parse_key: Fk,
483 mut read_value_and_insert: Fi,
484 ) -> ParseResultWithoutLoc<()>
485 where
486 Fk: for<'b> FnMut(&Self, String) -> ParseResultWithoutLoc<K>,
487 Fi: for<'b> FnMut(&mut Self, K) -> ParseResultWithoutLoc<()>,
488 {
489 if self.tokenizer.next_ident_if_eq("null")? {
490 return Ok(());
491 }
492
493 self.tokenizer.next_symbol_expect_eq('{', "map")?;
494 let mut first = true;
495 while !self.tokenizer.next_symbol_if_eq('}')? {
496 if !first {
497 self.tokenizer.next_symbol_expect_eq(',', "map")?;
498 }
499 first = false;
500
501 let key_string = self.read_string()?;
502 let k = parse_key(self, key_string)?;
503
504 self.tokenizer.next_symbol_expect_eq(':', "map")?;
505 read_value_and_insert(self, k)?;
506 }
507
508 Ok(())
509 }
510
parse_key(&self, key: String, t: &RuntimeType) -> ParseResultWithoutLoc<ReflectValueBox>511 fn parse_key(&self, key: String, t: &RuntimeType) -> ParseResultWithoutLoc<ReflectValueBox> {
512 match t {
513 RuntimeType::I32 => self.parse_number::<i32>(&key).map(ReflectValueBox::I32),
514 RuntimeType::I64 => self.parse_number::<i64>(&key).map(ReflectValueBox::I64),
515 RuntimeType::U32 => self.parse_number::<u32>(&key).map(ReflectValueBox::U32),
516 RuntimeType::U64 => self.parse_number::<u64>(&key).map(ReflectValueBox::U64),
517 RuntimeType::Bool => self.parse_bool(&key).map(ReflectValueBox::Bool),
518 RuntimeType::String => Ok(ReflectValueBox::String(key)),
519 t @ RuntimeType::F32
520 | t @ RuntimeType::F64
521 | t @ RuntimeType::VecU8
522 | t @ RuntimeType::Enum(..) => panic!("{} cannot be a map key", t),
523 RuntimeType::Message(_) => panic!("message cannot be a map key"),
524 }
525 }
526
merge_map_field( &mut self, message: &mut dyn MessageDyn, field: &FieldDescriptor, kt: &RuntimeType, vt: &RuntimeType, ) -> ParseResultWithoutLoc<()>527 fn merge_map_field(
528 &mut self,
529 message: &mut dyn MessageDyn,
530 field: &FieldDescriptor,
531 kt: &RuntimeType,
532 vt: &RuntimeType,
533 ) -> ParseResultWithoutLoc<()> {
534 let mut map = field.mut_map(message);
535 map.clear();
536
537 self.read_map(
538 |ss, s| ss.parse_key(s, kt),
539 |s, k| {
540 let v = s.read_value(vt)?;
541 map.insert(k, v);
542 Ok(())
543 },
544 )
545 }
546
merge_wk_struct(&mut self, struct_value: &mut Struct) -> ParseResultWithoutLoc<()>547 fn merge_wk_struct(&mut self, struct_value: &mut Struct) -> ParseResultWithoutLoc<()> {
548 struct_value.fields.clear();
549
550 self.read_map(
551 |_, s| Ok(s),
552 |s, k| {
553 let v = s.read_wk_value()?;
554 struct_value.fields.insert(k, v);
555 Ok(())
556 },
557 )
558 }
559
skip_json_value(&mut self) -> ParseResultWithoutLoc<()>560 fn skip_json_value(&mut self) -> ParseResultWithoutLoc<()> {
561 if self
562 .tokenizer
563 .next_ident_if_in(&["true", "false", "null"])?
564 .is_some()
565 {
566 } else if self.tokenizer.lookahead_is_str_lit()? {
567 self.tokenizer.next_str_lit()?;
568 } else if self.tokenizer.lookahead_is_json_number()? {
569 self.read_json_number_opt()?;
570 } else if self.tokenizer.lookahead_is_symbol('[')? {
571 self.read_list(|s| s.skip_json_value())?;
572 } else if self.tokenizer.lookahead_is_symbol('{')? {
573 self.read_map(|_, _| Ok(()), |s, ()| s.skip_json_value())?;
574 } else {
575 return Err(ParseErrorWithoutLoc(
576 ParseErrorWithoutLocInner::UnexpectedToken,
577 ));
578 }
579 Ok(())
580 }
581
merge_field( &mut self, message: &mut dyn MessageDyn, field: &FieldDescriptor, ) -> ParseResultWithoutLoc<()>582 fn merge_field(
583 &mut self,
584 message: &mut dyn MessageDyn,
585 field: &FieldDescriptor,
586 ) -> ParseResultWithoutLoc<()> {
587 match field.runtime_field_type() {
588 RuntimeFieldType::Singular(t) => self.merge_singular_field(message, field, &t),
589 RuntimeFieldType::Repeated(t) => self.merge_repeated_field(message, field, &t),
590 RuntimeFieldType::Map(kt, vt) => self.merge_map_field(message, field, &kt, &vt),
591 }
592 }
593
merge_inner(&mut self, message: &mut dyn MessageDyn) -> ParseResultWithoutLoc<()>594 fn merge_inner(&mut self, message: &mut dyn MessageDyn) -> ParseResultWithoutLoc<()> {
595 if let Some(duration) = message.downcast_mut() {
596 return self.merge_wk_duration(duration);
597 }
598
599 if let Some(timestamp) = message.downcast_mut() {
600 return self.merge_wk_timestamp(timestamp);
601 }
602
603 if let Some(field_mask) = message.downcast_mut() {
604 return self.merge_wk_field_mask(field_mask);
605 }
606
607 if let Some(value) = message.downcast_mut() {
608 return self.merge_wk_value(value);
609 }
610
611 if let Some(value) = message.downcast_mut() {
612 return self.merge_wk_any(value);
613 }
614
615 if let Some(value) = message.downcast_mut::<DoubleValue>() {
616 return self.merge_wrapper(value);
617 }
618
619 if let Some(value) = message.downcast_mut::<FloatValue>() {
620 return self.merge_wrapper(value);
621 }
622
623 if let Some(value) = message.downcast_mut::<Int64Value>() {
624 return self.merge_wrapper(value);
625 }
626
627 if let Some(value) = message.downcast_mut::<UInt64Value>() {
628 return self.merge_wrapper(value);
629 }
630
631 if let Some(value) = message.downcast_mut::<Int32Value>() {
632 return self.merge_wrapper(value);
633 }
634
635 if let Some(value) = message.downcast_mut::<UInt32Value>() {
636 return self.merge_wrapper(value);
637 }
638
639 if let Some(value) = message.downcast_mut::<BoolValue>() {
640 return self.merge_bool_value(value);
641 }
642
643 if let Some(value) = message.downcast_mut::<StringValue>() {
644 return self.merge_string_value(value);
645 }
646
647 if let Some(value) = message.downcast_mut::<BytesValue>() {
648 return self.merge_bytes_value(value);
649 }
650
651 if let Some(value) = message.downcast_mut::<ListValue>() {
652 return self.merge_wk_list_value(value);
653 }
654
655 if let Some(value) = message.downcast_mut::<Struct>() {
656 return self.merge_wk_struct(value);
657 }
658
659 let descriptor = message.descriptor_dyn();
660
661 self.tokenizer.next_symbol_expect_eq('{', "object")?;
662 let mut first = true;
663 while !self.tokenizer.next_symbol_if_eq('}')? {
664 if !first {
665 self.tokenizer.next_symbol_expect_eq(',', "object")?;
666 }
667 first = false;
668
669 let field_name = self.read_string()?;
670 // Proto3 JSON parsers are required to accept both
671 // the converted `lowerCamelCase` name and the proto field name.
672 match descriptor.field_by_name_or_json_name(&field_name) {
673 Some(field) => {
674 self.tokenizer.next_symbol_expect_eq(':', "object")?;
675 self.merge_field(message, &field)?;
676 }
677 None if self.parse_options.ignore_unknown_fields => {
678 self.tokenizer.next_symbol_expect_eq(':', "object")?;
679 self.skip_json_value()?;
680 }
681 None => {
682 return Err(ParseErrorWithoutLoc(
683 ParseErrorWithoutLocInner::UnknownFieldName(field_name),
684 ))
685 }
686 };
687 }
688 Ok(())
689 }
690
merge_wk_duration(&mut self, duration: &mut Duration) -> ParseResultWithoutLoc<()>691 fn merge_wk_duration(&mut self, duration: &mut Duration) -> ParseResultWithoutLoc<()> {
692 let s = self.read_string()?;
693 let mut lexer = Lexer::new(&s, ParserLanguage::Json);
694
695 fn next_dec(lexer: &mut Lexer) -> ParseResultWithoutLoc<(u64, u32)> {
696 let s = lexer.take_while(|c| c >= '0' && c <= '9');
697
698 if s.len() == 0 {
699 Ok((0, 0))
700 } else {
701 match s.parse() {
702 Ok(n) => Ok((n, s.len() as u32)),
703 Err(_) => Err(ParseErrorWithoutLoc(
704 ParseErrorWithoutLocInner::IncorrectDuration,
705 )),
706 }
707 }
708 }
709
710 let minus = lexer.next_char_if_eq('-');
711 let seconds = match next_dec(&mut lexer)? {
712 (_, 0) => {
713 return Err(ParseErrorWithoutLoc(
714 ParseErrorWithoutLocInner::IncorrectDuration,
715 ))
716 }
717 (s, _) => s,
718 };
719 let nanos = if lexer.next_char_if_eq('.') {
720 let (mut a, mut b) = next_dec(&mut lexer)?;
721 if b > 9 {
722 return Err(ParseErrorWithoutLoc(
723 ParseErrorWithoutLocInner::IncorrectDuration,
724 ));
725 }
726 while b != 9 {
727 b += 1;
728 a *= 10;
729 }
730
731 if a > 999_999_999 {
732 return Err(ParseErrorWithoutLoc(
733 ParseErrorWithoutLocInner::IncorrectDuration,
734 ));
735 }
736
737 a
738 } else {
739 0
740 };
741
742 // The suffix "s" is required
743 if !lexer.next_char_if_eq('s') {
744 return Err(ParseErrorWithoutLoc(
745 ParseErrorWithoutLocInner::IncorrectDuration,
746 ));
747 }
748
749 if !lexer.eof() {
750 return Err(ParseErrorWithoutLoc(
751 ParseErrorWithoutLocInner::IncorrectDuration,
752 ));
753 }
754
755 if minus {
756 duration.seconds = -(seconds as i64);
757 duration.nanos = -(nanos as i32);
758 } else {
759 duration.seconds = seconds as i64;
760 duration.nanos = nanos as i32;
761 }
762 Ok(())
763 }
764
merge_wk_timestamp(&mut self, timestamp: &mut Timestamp) -> ParseResultWithoutLoc<()>765 fn merge_wk_timestamp(&mut self, timestamp: &mut Timestamp) -> ParseResultWithoutLoc<()> {
766 let s = self.read_string()?;
767 let (seconds, nanos) = rfc_3339::TmUtc::parse_rfc_3339(&s)?;
768 timestamp.seconds = seconds;
769 timestamp.nanos = nanos as i32;
770 Ok(())
771 }
772
merge_wk_field_mask(&mut self, field_mask: &mut FieldMask) -> ParseResultWithoutLoc<()>773 fn merge_wk_field_mask(&mut self, field_mask: &mut FieldMask) -> ParseResultWithoutLoc<()> {
774 let s = self.read_string()?;
775 if !s.is_empty() {
776 field_mask.paths = s.split(',').map(|s| s.to_owned()).collect();
777 }
778 Ok(())
779 }
780
read_wk_list_value(&mut self) -> ParseResultWithoutLoc<ListValue>781 fn read_wk_list_value(&mut self) -> ParseResultWithoutLoc<ListValue> {
782 let mut r = ListValue::new();
783 self.merge_wk_list_value(&mut r)?;
784 Ok(r)
785 }
786
read_wk_struct(&mut self) -> ParseResultWithoutLoc<Struct>787 fn read_wk_struct(&mut self) -> ParseResultWithoutLoc<Struct> {
788 let mut r = Struct::new();
789 self.merge_wk_struct(&mut r)?;
790 Ok(r)
791 }
792
merge_wk_value(&mut self, value: &mut Value) -> ParseResultWithoutLoc<()>793 fn merge_wk_value(&mut self, value: &mut Value) -> ParseResultWithoutLoc<()> {
794 if self.tokenizer.lookahead_is_ident("null")? {
795 value.kind = Some(struct_::value::Kind::NullValue(
796 self.read_wk_null_value()?.into(),
797 ));
798 } else if self.tokenizer.lookahead_is_ident("true")?
799 || self.tokenizer.lookahead_is_ident("false")?
800 {
801 value.kind = Some(struct_::value::Kind::BoolValue(self.read_bool()?));
802 } else if self.tokenizer.lookahead_is_json_number()? {
803 value.kind = Some(struct_::value::Kind::NumberValue(self.read_f64()?));
804 } else if self.tokenizer.lookahead_is_str_lit()? {
805 value.kind = Some(struct_::value::Kind::StringValue(self.read_string()?));
806 } else if self.tokenizer.lookahead_is_symbol('[')? {
807 value.kind = Some(struct_::value::Kind::ListValue(self.read_wk_list_value()?));
808 } else if self.tokenizer.lookahead_is_symbol('{')? {
809 value.kind = Some(struct_::value::Kind::StructValue(self.read_wk_struct()?));
810 } else {
811 return Err(ParseErrorWithoutLoc(
812 ParseErrorWithoutLocInner::UnexpectedToken,
813 ));
814 }
815 Ok(())
816 }
817
merge_wk_any(&mut self, _value: &mut Any) -> ParseResultWithoutLoc<()>818 fn merge_wk_any(&mut self, _value: &mut Any) -> ParseResultWithoutLoc<()> {
819 Err(ParseErrorWithoutLoc(
820 ParseErrorWithoutLocInner::AnyParsingIsNotImplemented,
821 ))
822 }
823
read_wk_value(&mut self) -> ParseResultWithoutLoc<Value>824 fn read_wk_value(&mut self) -> ParseResultWithoutLoc<Value> {
825 let mut v = Value::new();
826 self.merge_wk_value(&mut v)?;
827 Ok(v)
828 }
829
merge(&mut self, message: &mut dyn MessageDyn) -> ParseResult<()>830 fn merge(&mut self, message: &mut dyn MessageDyn) -> ParseResult<()> {
831 match self.merge_inner(message) {
832 Ok(()) => Ok(()),
833 Err(error) => Err(ParseError {
834 error,
835 loc: self.tokenizer.loc(),
836 }),
837 }
838 }
839 }
840
841 /// JSON parse options.
842 ///
843 /// # Examples
844 ///
845 /// ```
846 /// let parse_options = protobuf_json_mapping::ParseOptions {
847 /// ignore_unknown_fields: true,
848 /// ..Default::default()
849 /// };
850 /// ```
851 #[derive(Default, Debug, Clone)]
852 pub struct ParseOptions {
853 /// Ignore unknown fields when parsing.
854 ///
855 /// When `true` fields with unknown names are ignored.
856 /// When `false` parser returns an error on unknown field.
857 pub ignore_unknown_fields: bool,
858 /// Prevent initializing `ParseOptions` enumerating all field.
859 pub _future_options: (),
860 }
861
862 /// Merge JSON into provided message
merge_from_str_with_options( message: &mut dyn MessageDyn, json: &str, parse_options: &ParseOptions, ) -> ParseResult<()>863 pub fn merge_from_str_with_options(
864 message: &mut dyn MessageDyn,
865 json: &str,
866 parse_options: &ParseOptions,
867 ) -> ParseResult<()> {
868 let mut parser = Parser {
869 tokenizer: Tokenizer::new(json, ParserLanguage::Json),
870 parse_options: parse_options.clone(),
871 };
872 parser.merge(message)
873 }
874
875 /// Merge JSON into provided message
merge_from_str(message: &mut dyn MessageDyn, json: &str) -> ParseResult<()>876 pub fn merge_from_str(message: &mut dyn MessageDyn, json: &str) -> ParseResult<()> {
877 merge_from_str_with_options(message, json, &ParseOptions::default())
878 }
879
880 /// Parse JSON to protobuf message.
parse_dyn_from_str_with_options( d: &MessageDescriptor, json: &str, parse_options: &ParseOptions, ) -> ParseResult<Box<dyn MessageDyn>>881 pub fn parse_dyn_from_str_with_options(
882 d: &MessageDescriptor,
883 json: &str,
884 parse_options: &ParseOptions,
885 ) -> ParseResult<Box<dyn MessageDyn>> {
886 let mut m = d.new_instance();
887 merge_from_str_with_options(&mut *m, json, parse_options)?;
888 if let Err(_) = m.check_initialized_dyn() {
889 return Err(ParseError {
890 error: ParseErrorWithoutLoc(ParseErrorWithoutLocInner::MessageNotInitialized),
891 loc: Loc::start(),
892 });
893 }
894 Ok(m)
895 }
896
897 /// Parse JSON to protobuf message.
parse_dyn_from_str(d: &MessageDescriptor, json: &str) -> ParseResult<Box<dyn MessageDyn>>898 pub fn parse_dyn_from_str(d: &MessageDescriptor, json: &str) -> ParseResult<Box<dyn MessageDyn>> {
899 parse_dyn_from_str_with_options(d, json, &ParseOptions::default())
900 }
901
902 /// Parse JSON to protobuf message.
parse_from_str_with_options<M: MessageFull>( json: &str, parse_options: &ParseOptions, ) -> ParseResult<M>903 pub fn parse_from_str_with_options<M: MessageFull>(
904 json: &str,
905 parse_options: &ParseOptions,
906 ) -> ParseResult<M> {
907 let m = parse_dyn_from_str_with_options(&M::descriptor(), json, parse_options)?;
908 Ok(*m.downcast_box().unwrap())
909 }
910
911 /// Parse JSON to protobuf message.
parse_from_str<M: MessageFull>(json: &str) -> ParseResult<M>912 pub fn parse_from_str<M: MessageFull>(json: &str) -> ParseResult<M> {
913 parse_from_str_with_options(json, &ParseOptions::default())
914 }
915