Class JSONValueFactory

java.lang.Object
com.codename1.io.JSONValueFactory

public class JSONValueFactory extends Object

Builds the values JSONParser.parseStrict(String, JSONValueFactory) reads.

The strict reader calls these methods as it walks a document, and whatever they return is what the containers hold, so a caller can parse straight into its own types instead of converting a tree of maps and lists afterwards. A language runtime builds its own dictionaries and numbers this way, and a data binder can build its objects without a second pass.

This class answers exactly what JSONParser.parseStrict(String) answers: a LinkedHashMap per object, an ArrayList per array, String, Boolean, null, Long for an integer and Double for any other number. Override the methods whose result should differ. Containers are opened, filled in document order and closed; a value is complete when it is handed to its parent.

Two relaxations of RFC 8259 are off unless a subclass turns them on: allowNonFinite() for NaN, Infinity, -Infinity and numbers too large for a double, and allowControlCharacters() for unescaped control characters inside strings.

  • Constructor Details

    • JSONValueFactory

      public JSONValueFactory()
  • Method Details

    • newObject

      public Object newObject()

      Opens an object.

      Returns

      the new, empty container, handed back to put(Object, String, Object) and endObject(Object)

    • put

      public void put(Object object, String key, Object value)

      Adds one member to an object opened by newObject(). A key may repeat; the reader passes every occurrence in order.

      Parameters
      • object: the container

      • key: the member name

      • value: the member value

    • endObject

      public Object endObject(Object object)

      Closes an object.

      Parameters
      • object: the container
      Returns

      the value the parent receives, normally the container itself

    • newArray

      public Object newArray()

      Opens an array.

      Returns

      the new, empty container

    • add

      public void add(Object array, Object value)

      Appends one element to an array opened by newArray().

      Parameters
      • array: the container

      • value: the element

    • endArray

      public Object endArray(Object array)

      Closes an array.

      Parameters
      • array: the container
      Returns

      the value the parent receives, normally the container itself

    • string

      public Object string(String value)

      A string value. Member names are not passed here.

      Parameters
      • value: the decoded string
      Returns

      the value to store

    • integer

      public Object integer(long value)

      An integer that fits a long.

      Parameters
      • value: the integer
      Returns

      the value to store

    • bigInteger

      public Object bigInteger(String digits) throws IOException

      An integer too large for a long.

      Parameters
      • digits: the integer as written, with its sign
      Returns

      the value to store

      Throws
      • IOException: to refuse it, which this class does
      Throws:
      IOException
    • decimal

      public Object decimal(double value)

      A number with a fraction or an exponent, or a non-finite one when allowNonFinite() is on.

      Parameters
      • value: the number
      Returns

      the value to store

    • bool

      public Object bool(boolean value)

      true or false.

      Parameters
      • value: the literal
      Returns

      the value to store

    • nullValue

      public Object nullValue()

      null.

      Returns

      the value to store

    • allowNonFinite

      public boolean allowNonFinite()

      Whether NaN, Infinity and -Infinity are read, and a number such as 1e999 is read as infinite rather than refused. JavaScript's and Python's JSON writers emit these.

      Returns

      false, as RFC 8259 requires

    • allowControlCharacters

      public boolean allowControlCharacters()

      Whether a control character below U+0020 may appear unescaped inside a string.

      Returns

      false, as RFC 8259 requires