You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// let float_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
630
+
/// let float_jsonb = "1e100".parse::<OwnedJsonb>().unwrap();
613
631
/// let result = float_jsonb.as_raw().to_i64();
614
632
/// assert!(result.is_err());
615
633
///
@@ -647,12 +665,12 @@ impl RawJsonb<'_> {
647
665
}
648
666
JsonbItem::Number(num) => {
649
667
let value = num.as_number()?;
650
-
ifletSome(v) = value.as_i64(){
668
+
ifletSome(v) = value.to_i64(){
651
669
returnOk(v);
652
670
}
653
671
}
654
672
JsonbItem::String(s) => {
655
-
ifletOk(v) = s.parse::<i64>(){
673
+
ifletSome(v) = parse_string_to_i64(&s){
656
674
returnOk(v);
657
675
}
658
676
}
@@ -661,18 +679,18 @@ impl RawJsonb<'_> {
661
679
Err(Error::InvalidCast)
662
680
}
663
681
664
-
/// Checks if the JSONB value is an unsigned integer that can be represented as a u64.
682
+
/// Checks whether the JSONB value is an exact `u64`.
665
683
///
666
-
/// This function checks if the JSONB value is a number and can be converted to a `u64` without loss of information.
684
+
/// Decimal and floating-point numbers must already be integral.
667
685
///
668
686
/// # Arguments
669
687
///
670
688
/// * `self` - The JSONB value.
671
689
///
672
690
/// # Returns
673
691
///
674
-
/// * `Ok(true)` - If the value is an unsigned integer representable as a `u64`.
675
-
/// * `Ok(false)` - If the value is not an unsigned integer or cannot be represented as a `u64`.
692
+
/// * `Ok(true)` - If the value is an exact `u64`.
693
+
/// * `Ok(false)` - Otherwise.
676
694
/// * `Err(Error)` - If the JSONB data is invalid.
677
695
///
678
696
/// # Examples
@@ -689,8 +707,12 @@ impl RawJsonb<'_> {
689
707
/// let raw_u64 = u64_jsonb.as_raw();
690
708
/// assert!(raw_u64.is_u64().unwrap());
691
709
///
710
+
/// let float_jsonb = "123.0".parse::<OwnedJsonb>().unwrap();
711
+
/// let raw_float = float_jsonb.as_raw();
712
+
/// assert!(raw_float.is_u64().unwrap());
713
+
///
692
714
/// // Non-u64 values
693
-
/// let float_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
715
+
/// let float_jsonb = "123.1".parse::<OwnedJsonb>().unwrap();
694
716
/// let raw_float = float_jsonb.as_raw();
695
717
/// assert!(!raw_float.is_u64().unwrap());
696
718
///
@@ -728,20 +750,19 @@ impl RawJsonb<'_> {
728
750
self.as_u64().map(|v| v.is_some())
729
751
}
730
752
731
-
/// Extracts a u64 unsigned integer from a JSONB value.
753
+
/// Extracts an exact `u64` from a JSONB value.
732
754
///
733
-
/// This function attempts to extract a `u64` unsigned integer from the JSONB value.
734
-
/// If the JSONB value is a number and can be represented as a `u64` without loss of information (i.e., it's a non-negative integer within the `u64` range),
735
-
/// the unsigned integer value is returned. Otherwise, `None` is returned.
755
+
/// Decimal and floating-point numbers are accepted only when they already
756
+
/// represent a non-negative integer.
736
757
///
737
758
/// # Arguments
738
759
///
739
760
/// * `self` - The JSONB value.
740
761
///
741
762
/// # Returns
742
763
///
743
-
/// * `Ok(Some(u64))` - If the value is an unsigned integer that can be represented as a `u64`.
744
-
/// * `Ok(None)` - If the value is not an unsigned integer or cannot be represented as a `u64`.
764
+
/// * `Ok(Some(u64))` - If the value is an exact `u64`.
765
+
/// * `Ok(None)` - Otherwise.
745
766
/// * `Err(Error)` - If the JSONB data is invalid.
/// Converts a JSONB value to a u64 unsigned integer.
832
+
/// Converts a JSONB value to `u64`.
804
833
///
805
-
/// This function attempts to convert a JSONB value to a `u64` unsigned integer.
806
-
/// It prioritizes direct conversion from a number if possible.
807
-
/// If the value is a boolean, it's converted to 1 (for `true`) or 0 (for `false`).
808
-
/// If the value is a string that can be parsed as a `u64`, that parsed value is returned.
809
-
/// Otherwise, an error is returned.
834
+
/// Numbers are rounded to the nearest integer. Booleans map to `1` and
835
+
/// `0`. Strings are parsed as `u64`, or as `f64` and then rounded.
810
836
///
811
837
/// # Arguments
812
838
///
813
839
/// * `self` - The JSONB value.
814
840
///
815
841
/// # Returns
816
842
///
817
-
/// * `Ok(u64)` - The `u64` representation of the JSONB value.
818
-
/// * `Err(Error::InvalidCast)` - If the value cannot be converted to a `u64` (e.g., it's a floating-point number, a negative number, an array, an object, or a string that is not a valid unsigned integer).
843
+
/// * `Ok(u64)` - The converted value.
844
+
/// * `Err(Error::InvalidCast)` - If conversion fails.
819
845
/// * `Err(Error)` - If the JSONB data is invalid.
820
846
///
821
847
/// # Examples
@@ -841,8 +867,18 @@ impl RawJsonb<'_> {
841
867
/// let str_jsonb = r#""123""#.parse::<OwnedJsonb>().unwrap();
0 commit comments