@@ -908,6 +908,10 @@ impl Value {
908908 Value :: Long ( n) => Ok ( Value :: Float ( n as f32 ) ) ,
909909 Value :: Float ( x) => Ok ( Value :: Float ( x) ) ,
910910 Value :: Double ( x) => Ok ( Value :: Float ( x as f32 ) ) ,
911+ Value :: String ( x) => match Self :: parse_special_float ( & x) {
912+ Some ( f) => Ok ( Value :: Float ( f) ) ,
913+ None => Err ( Error :: GetFloat ( ValueKind :: String ) ) ,
914+ } ,
911915 other => Err ( Error :: GetFloat ( other. into ( ) ) ) ,
912916 }
913917 }
@@ -918,10 +922,25 @@ impl Value {
918922 Value :: Long ( n) => Ok ( Value :: Double ( n as f64 ) ) ,
919923 Value :: Float ( x) => Ok ( Value :: Double ( f64:: from ( x) ) ) ,
920924 Value :: Double ( x) => Ok ( Value :: Double ( x) ) ,
925+ Value :: String ( x) => match Self :: parse_special_float ( & x) {
926+ Some ( f) => Ok ( Value :: Double ( f. into ( ) ) ) ,
927+ None => Err ( Error :: GetDouble ( ValueKind :: String ) ) ,
928+ } ,
921929 other => Err ( Error :: GetDouble ( other. into ( ) ) ) ,
922930 }
923931 }
924932
933+ /// IEEE 754 NaN and infinities are not valid JSON numbers.
934+ /// So they are represented in JSON as strings.
935+ fn parse_special_float ( s : & str ) -> Option < f32 > {
936+ match s. trim ( ) . to_ascii_lowercase ( ) . as_str ( ) {
937+ "nan" | "+nan" | "-nan" => Some ( f32:: NAN ) ,
938+ "inf" | "+inf" | "infinity" | "+infinity" => Some ( f32:: INFINITY ) ,
939+ "-inf" | "-infinity" => Some ( f32:: NEG_INFINITY ) ,
940+ _ => None ,
941+ }
942+ }
943+
925944 fn resolve_bytes ( self ) -> Result < Self , Error > {
926945 match self {
927946 Value :: Bytes ( bytes) => Ok ( Value :: Bytes ( bytes) ) ,
0 commit comments