@@ -524,8 +524,7 @@ impl Document {
524
524
Ok ( ( ) )
525
525
}
526
526
527
- /// Attempts to deserialize a `Document` from a byte stream.
528
- pub fn from_reader < R : Read + ?Sized > ( reader : & mut R ) -> crate :: de:: Result < Document > {
527
+ fn decode < R : Read + ?Sized > ( reader : & mut R , utf_lossy : bool ) -> crate :: de:: Result < Document > {
529
528
let mut doc = Document :: new ( ) ;
530
529
531
530
let length = read_i32 ( reader) ?;
@@ -550,7 +549,7 @@ impl Document {
550
549
break ;
551
550
}
552
551
553
- let ( key, val) = deserialize_bson_kvp ( cursor, tag, false ) ?;
552
+ let ( key, val) = deserialize_bson_kvp ( cursor, tag, utf_lossy ) ?;
554
553
doc. insert ( key, val) ;
555
554
}
556
555
Ok ( ( ) )
@@ -559,6 +558,21 @@ impl Document {
559
558
560
559
Ok ( doc)
561
560
}
561
+
562
+ /// Attempts to deserialize a `Document` from a byte stream.
563
+ pub fn from_reader < R : Read + ?Sized > ( reader : & mut R ) -> crate :: de:: Result < Document > {
564
+ Self :: decode ( reader, false )
565
+ }
566
+
567
+ /// Attempt to deserialize a `Document` that may contain invalid UTF-8 strings from a byte
568
+ /// stream.
569
+ ///
570
+ /// This is mainly useful when reading raw BSON returned from a MongoDB server, which
571
+ /// in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
572
+ /// For most use cases, `Document::from_reader` can be used instead.
573
+ pub fn from_reader_utf8_lossy < R : Read + ?Sized > ( reader : & mut R ) -> crate :: de:: Result < Document > {
574
+ Self :: decode ( reader, true )
575
+ }
562
576
}
563
577
564
578
pub struct Entry < ' a > {
0 commit comments