o

zio.json.internal

SafeNumbers

object SafeNumbers

Total, fast, number parsing.

The Java and Scala standard libraries throw exceptions when we attempt to parse an invalid number. Unfortunately, exceptions are very expensive, and untrusted data can be maliciously constructed to DOS a server.

This suite of functions mitigates against such attacks by building up the numbers one character at a time, which has been shown through extensive benchmarking to be orders of magnitude faster than exception-throwing stdlib parsers, for valid and invalid inputs. This approach, proposed by alexknvl, was also benchmarked against regexp-based pre-validation.

Note that although the behaviour is identical to the Java stdlib when given the canonical form of a primitive (i.e. the .toString) of a number there may be differences in behaviour for non-canonical forms. e.g. the Java stdlib may reject "1.0" when parsed as an BigInteger but we may parse it as a 1, although "1.1" would be rejected. Parsing of BigDecimal preserves the trailing zeros on the right but not on the left, e.g. "000.00001000" will be "1.000e-5", which is useful in cases where the trailing zeros denote measurement accuracy.

BigInteger, BigDecimal, Float and Double have a configurable bit limit on the size of the significand, to avoid OOM style attacks, which is 128 bits by default.

Results are contained in a specialisation of Option that avoids boxing.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. SafeNumbers
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def bigDecimal(num: String, max_bits: Int = 128): Option[BigDecimal]
  6. def bigInteger(num: String, max_bits: Int = 128): Option[BigInteger]
  7. def byte(num: String): ByteOption
  8. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  9. def double(num: String, max_bits: Int = 128): DoubleOption
  10. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  11. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  13. def float(num: String, max_bits: Int = 128): FloatOption
  14. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  15. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  16. def int(num: String): IntOption
  17. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  18. def long(num: String): LongOption
  19. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  20. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  21. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  22. def short(num: String): ShortOption
  23. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  24. def toString(x: Float): String
  25. def toString(x: Double): String
  26. def toString(): String
    Definition Classes
    AnyRef → Any
  27. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  28. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  29. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from AnyRef

Inherited from Any

Ungrouped