com.fasterxml.jackson.core.io

Class NumberInput



  • public final class NumberInput
    extends Object
    Helper class for efficient parsing of various JSON numbers.

    NOTE! Does NOT validate against maximum length limits: caller must do that if and as necessary.

    • Field Detail

      • NASTY_SMALL_DOUBLE

        @Deprecated
        public static final String NASTY_SMALL_DOUBLE
        Deprecated. Since 2.14 -- do not use
        Formerly used constant for a value that was problematic on certain pre-1.8 JDKs.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NumberInput

        public NumberInput()
    • Method Detail

      • parseInt

        public static int parseInt(char[] ch,
                                   int off,
                                   int len)
        Fast method for parsing unsigned integers that are known to fit into regular 32-bit signed int type. This means that length is between 1 and 9 digits (inclusive) and there is no sign character.

        Note: public to let unit tests call it; not meant to be used by any code outside this package.

        Parameters:
        ch - Buffer that contains integer value to decode
        off - Offset of the first digit character in buffer
        len - Length of the number to decode (in characters)
        Returns:
        Decoded int value
      • parseInt

        public static int parseInt(String s)
        Helper method to (more) efficiently parse integer numbers from String values. Input String must be simple Java integer value. No range checks are made to verify that the value fits in 32-bit Java int: caller is expected to only calls this in cases where this can be guaranteed (basically: number of digits does not exceed 9)

        NOTE: semantics differ significantly from parseInt(char[], int, int).

        Parameters:
        s - String that contains integer value to decode
        Returns:
        Decoded int value
      • parseLong

        public static long parseLong(char[] ch,
                                     int off,
                                     int len)
      • parseLong19

        public static long parseLong19(char[] ch,
                                       int off,
                                       boolean negative)
        Parses an unsigned long made up of exactly 19 digits.

        It is the callers responsibility to make sure the input is exactly 19 digits. and fits into a 64bit long by calling inLongRange(char[], int, int, boolean) first.

        Note that input String must NOT contain leading minus sign (even if negative is set to true).

        Parameters:
        ch - Buffer that contains integer value to decode
        off - Offset of the first digit character in buffer
        negative - Whether original number had a minus sign
        Returns:
        Decoded long value
        Since:
        2.15.0
      • parseLong

        public static long parseLong(String s)
        Similar to parseInt(String) but for long values.
        Parameters:
        s - String that contains long value to decode
        Returns:
        Decoded long value
      • inLongRange

        public static boolean inLongRange(char[] ch,
                                          int off,
                                          int len,
                                          boolean negative)
        Helper method for determining if given String representation of an integral number would fit in 64-bit Java long or not. Note that input String must NOT contain leading minus sign (even if 'negative' is set to true).
        Parameters:
        ch - Buffer that contains long value to check
        off - Offset of the first digit character in buffer
        len - Length of the number to decode (in characters)
        negative - Whether original number had a minus sign (which is NOT passed to this method) or not
        Returns:
        True if specified String representation is within Java long range; false if not.
      • inLongRange

        public static boolean inLongRange(String s,
                                          boolean negative)
        Similar to inLongRange(char[],int,int,boolean), but with String argument
        Parameters:
        s - String that contains long value to check
        negative - Whether original number had a minus sign (which is NOT passed to this method) or not
        Returns:
        True if specified String representation is within Java long range; false if not.
      • parseAsInt

        public static int parseAsInt(String s,
                                     int def)
      • parseAsLong

        public static long parseAsLong(String s,
                                       long def)
      • parseAsDouble

        public static double parseAsDouble(String s,
                                           double def)
        Parameters:
        s - a string representing a number to parse
        def - the default to return if `s` is not a parseable number
        Returns:
        closest matching double (or `def` if there is an issue with `s`) where useFastParser=false
        See Also:
        parseAsDouble(String, double, boolean)
      • parseAsDouble

        public static double parseAsDouble(String s,
                                           double def,
                                           boolean useFastParser)
        Parameters:
        s - a string representing a number to parse
        def - the default to return if `s` is not a parseable number
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching double (or `def` if there is an issue with `s`)
        Since:
        2.14
      • parseDouble

        public static double parseDouble(String s,
                                         boolean useFastParser)
                                  throws NumberFormatException
        Parameters:
        s - a string representing a number to parse
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching double
        Throws:
        NumberFormatException - if string cannot be represented by a double
        Since:
        v2.14
      • parseDouble

        public static double parseDouble(char[] array,
                                         boolean useFastParser)
                                  throws NumberFormatException
        Parameters:
        array - a char array containing a number to parse
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching double
        Throws:
        NumberFormatException - if value cannot be represented by a double
        Since:
        2.18
      • parseDouble

        public static double parseDouble(char[] array,
                                         int offset,
                                         int len,
                                         boolean useFastParser)
                                  throws NumberFormatException
        Parameters:
        array - a char array containing a number to parse
        offset - the offset to apply when parsing the number in the char array
        len - the length of the number in the char array
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching double
        Throws:
        NumberFormatException - if value cannot be represented by a double
        Since:
        2.18
      • parseFloat

        public static float parseFloat(String s,
                                       boolean useFastParser)
                                throws NumberFormatException
        Parameters:
        s - a string representing a number to parse
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching float
        Throws:
        NumberFormatException - if string cannot be represented by a float
        Since:
        v2.14
      • parseFloat

        public static float parseFloat(char[] array,
                                       boolean useFastParser)
                                throws NumberFormatException
        Parameters:
        array - a char array containing a number to parse
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching float
        Throws:
        NumberFormatException - if value cannot be represented by a float
        Since:
        2.18
      • parseFloat

        public static float parseFloat(char[] array,
                                       int offset,
                                       int len,
                                       boolean useFastParser)
                                throws NumberFormatException
        Parameters:
        array - a char array containing a number to parse
        offset - the offset to apply when parsing the number in the char array
        len - the length of the number in the char array
        useFastParser - whether to use FastDoubleParser
        Returns:
        closest matching float
        Throws:
        NumberFormatException - if value cannot be represented by a float
        Since:
        2.18
      • parseBigDecimal

        public static BigDecimal parseBigDecimal(String s,
                                                 boolean useFastParser)
                                          throws NumberFormatException
        Parameters:
        s - a string representing a number to parse
        useFastParser - whether to use custom fast parser (true) or JDK default (false) parser
        Returns:
        a BigDecimal
        Throws:
        NumberFormatException - if the char array cannot be represented by a BigDecimal
        Since:
        v2.15
      • parseBigDecimal

        public static BigDecimal parseBigDecimal(char[] ch,
                                                 int off,
                                                 int len,
                                                 boolean useFastParser)
                                          throws NumberFormatException
        Parameters:
        ch - a char array with text that makes up a number
        off - the offset to apply when parsing the number in the char array
        len - the length of the number in the char array
        useFastParser - whether to use custom fast parser (true) or JDK default (false) parser
        Returns:
        a BigDecimal
        Throws:
        NumberFormatException - if the char array cannot be represented by a BigDecimal
        Since:
        v2.15
      • parseBigDecimal

        public static BigDecimal parseBigDecimal(char[] ch,
                                                 boolean useFastParser)
                                          throws NumberFormatException
        Parameters:
        ch - a char array with text that makes up a number
        useFastParser - whether to use custom fast parser (true) or JDK default (false) parser
        Returns:
        a BigDecimal
        Throws:
        NumberFormatException - if the char array cannot be represented by a BigDecimal
        Since:
        v2.15
      • parseBigInteger

        public static BigInteger parseBigInteger(String s,
                                                 boolean useFastParser)
                                          throws NumberFormatException
        Parameters:
        s - a string representing a number to parse
        useFastParser - whether to use custom fast parser (true) or JDK default (false) parser
        Returns:
        a BigInteger
        Throws:
        NumberFormatException - if string cannot be represented by a BigInteger
        Since:
        v2.15
      • parseBigIntegerWithRadix

        public static BigInteger parseBigIntegerWithRadix(String s,
                                                          int radix,
                                                          boolean useFastParser)
                                                   throws NumberFormatException
        Parameters:
        s - a string representing a number to parse
        radix - for parse
        useFastParser - whether to use custom fast parser (true) or JDK default (false) parser
        Returns:
        a BigInteger
        Throws:
        NumberFormatException - if string cannot be represented by a BigInteger
        Since:
        v2.15
      • looksLikeValidNumber

        public static boolean looksLikeValidNumber(String s)
        Method called to check whether given pattern looks like a valid Java Number (which is bit looser definition than valid JSON Number). Used as pre-parsing check when parsing "Stringified numbers".

        The differences to stricter JSON Number are:

        • Positive sign is allowed
        • Leading zeroes are allowed

        Note: no trimming (String.trim()) nor null checks are performed on String passed.

        Note: this method returning true DOES NOT GUARANTEE String is valid number but just that it looks close enough.

        Parameters:
        s - String to validate
        Returns:
        True if String looks like valid Java number; false otherwise.
        Since:
        2.17

Copyright © 2008–2024 FasterXML. All rights reserved.