break;
 
        case jbvNumeric:
-           /* replace numeric NaN with string "NaN" */
-           if (numeric_is_nan(scalarVal->val.numeric))
-           {
-               appendToBuffer(buffer, "NaN", 3);
-               *jentry = 3;
-               break;
-           }
-
            numlen = VARSIZE_ANY(scalarVal->val.numeric);
            padlen = padBufferToInt(buffer);
 
 
                {
                    char       *tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
                                                                          NumericGetDatum(jb->val.numeric)));
+                   double      val;
                    bool        have_error = false;
 
-                   (void) float8in_internal_opt_error(tmp,
-                                                      NULL,
-                                                      "double precision",
-                                                      tmp,
-                                                      &have_error);
+                   val = float8in_internal_opt_error(tmp,
+                                                     NULL,
+                                                     "double precision",
+                                                     tmp,
+                                                     &have_error);
 
-                   if (have_error)
+                   if (have_error || isinf(val) || isnan(val))
                        RETURN_ERROR(ereport(ERROR,
                                             (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
                                              errmsg("numeric argument of jsonpath item method .%s() is out of range for type double precision",
                                                      tmp,
                                                      &have_error);
 
-                   if (have_error || isinf(val))
+                   if (have_error || isinf(val) || isnan(val))
                        RETURN_ERROR(ereport(ERROR,
                                             (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
                                              errmsg("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",
 
 select jsonb_path_query('1e1000', '$.double()');
 ERROR:  numeric argument of jsonpath item method .double() is out of range for type double precision
 select jsonb_path_query('"nan"', '$.double()');
- jsonb_path_query 
-------------------
- "NaN"
-(1 row)
-
+ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
 select jsonb_path_query('"NaN"', '$.double()');
- jsonb_path_query 
-------------------
- "NaN"
-(1 row)
-
+ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
 select jsonb_path_query('"inf"', '$.double()');
 ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
 select jsonb_path_query('"-inf"', '$.double()');