Interface ObjectReader

  • All Superinterfaces:
    AutoCloseable, Closeable
    All Known Implementing Classes:
    JsonReader

    public interface ObjectReader
    extends Closeable
    ObjectReader is part of the streaming api, it's implementations allow you to read data from the stream. The root of the input should always be a object, an array or a literal. There may be some differences between implementations if they try to be compliant with their format specification.

    • To read an array call beginArray() then use hasNext() to check if there is a next element and then call next() to advance. When you call next in an array it will read the next value and return its type ValueType. Use it to check values type and to retrieve its value (if it is a literal) use one of valueAsXXX methods, otherwise it is an array or object. When hasNext returns false terminate the array by calling endArray().
    • To read a object call beginObject() then use hasNext() to check if there is a next property and then call next() to read the name/value pair and name() to retrieve its name. If the value is a literal retrieve its value with valueAsXXX methods, otherwise it is an array or object. When you finished reading all properties call endObject().
    • Objects can also contain metadata as their first properties. To read object metadata you have two options:
      1. Just begin your object with beginObject and then retrieve the metadata that you want with metadata(nameOfTheMetadataProperty).
      2. Use nextObjectMetadata() to read the next objects metadata without calling beginObject. This is useful when you want to handle some metadata in a converter and then delegate the rest to another converter (that will call beginObject or again nextObjectMetadata, so for him it will be transparent that you retrieved already some metadata and he will still be able to retrieve the same data).
    • To read a literal use valueAsXXX methods. Actual implementation allows literals as root and is relatively tolerant to wrong types, for example if the stream contains the string "123" but you want to retrieve it as a int, JsonReader.valueAsInt() will parse it and return 123. It does also conversion between numeric types (double <-> int etc).
    • To skip a value use skipValue(). If the value is an object or an array it will skip all its content.

    Here is an example if you want to use directly the streaming api instead of the databind api (or if you write a custom converter or deserializer).

     public static void main(String[] args) {
            // we will read from json to Person
            Person.read(new JsonReader("{\"name\":\"eugen\",\"age\":26, \"childrenYearOfBirth\":[]}"));
     }
    
     class Person {
            String name;
            int age;
            List<Integer> childrenYearOfBirth;
    
            public static Person read(ObjectReader reader) {
                    Person p = new Person();
                    for (; reader.hasNext();) {
                            if ("name".equals(reader.name()))
                                    p.name = reader.valueAsString();
                            else if ("age".equals(reader.name()))
                                    p.age = reader.valueAsInt();
                            else if ("childrenYearOfBirth".equals(reader.name())) {
                                    if (reader.getValueType() == TypeValue.NULL)
                                            p.childrenYearOfBirth = null;
                                    else {
                                            reader.beginArray();
                                            p.childrenYearOfBirth = new ArrayList<Integer>();
                                            for (int i = 0; reader.hasNext(); i++)
                                                    p.childrenYearOfBirth.add(reader.valueAsInt());
                                            reader.endArray();
            }
          }
        }
                    return p;
      }
     }
     
    Author:
    Eugen Cepoi
    See Also:
    ValueType, JsonReader, ObjectWriter, JsonWriter