Interface ObjectReader<T>

Type Parameters:
T - the type of objects that this ObjectReader can deserialize
All Known Implementing Classes:
ApacheLang3Support.PairReader, ObjectArrayReader, ObjectReader1, ObjectReader10, ObjectReader11, ObjectReader12, ObjectReader2, ObjectReader3, ObjectReader4, ObjectReader5, ObjectReader6, ObjectReader7, ObjectReader8, ObjectReader9, ObjectReaderAdapter, ObjectReaderBean, ObjectReaderImplBitSet, ObjectReaderImplByte, ObjectReaderImplDate, ObjectReaderImplEnum, ObjectReaderImplField, ObjectReaderImplFromBoolean, ObjectReaderImplFromInt, ObjectReaderImplFromLong, ObjectReaderImplFromString, ObjectReaderImplInstant, ObjectReaderImplInt32Array, ObjectReaderImplInt32ValueArray, ObjectReaderImplInt64Array, ObjectReaderImplInt64ValueArray, ObjectReaderImplJSONP, ObjectReaderImplList, ObjectReaderImplListInt64, ObjectReaderImplListStr, ObjectReaderImplMap, ObjectReaderImplMapMultiValueType, ObjectReaderImplMethod, ObjectReaderImplObject, ObjectReaderImplShort, ObjectReaderImplStringArray, ObjectReaderImplValue, ObjectReaderImplValueInt, ObjectReaderImplValueString, ObjectReaderInterface, ObjectReaderMisc, ObjectReaderNoneDefaultConstructor, ObjectReaderRootName

public interface ObjectReader<T>
ObjectReader is responsible for deserializing JSON data into Java objects. It provides a set of methods for creating object instances, reading JSON data, and handling field mapping.

This interface supports various features including:

  • Object creation with different parameter types
  • JSON object and array reading
  • Field mapping and value setting
  • Auto-type support for polymorphic deserialization
  • Custom feature configuration

Example usage:

 // Get an ObjectReader for a specific type
 ObjectReader<User> reader = JSONFactory.getDefaultObjectReaderProvider().getObjectReader(User.class);

 // Read from JSON string
 String jsonString = "{\"id\":1,\"name\":\"John\"}";
 User user = reader.readObject(JSONReader.of(jsonString));

 // Create instance with map data
 Map<String, Object> data = new HashMap<>();
 data.put("id", 1);
 data.put("name", "John");
 User user2 = reader.createInstance(data);
 
Since:
2.0.0
  • Field Details

  • Method Details

    • createInstance

      default T createInstance()
      Returns:
      ObjectReader
      Throws:
      UnsupportedOperationException - If the method is not overloaded or otherwise
    • createInstance

      default T createInstance(long features)
      Returns:
      ObjectReader
      Throws:
      UnsupportedOperationException - If the method is not overloaded or otherwise
    • createInstance

      default T createInstance(Collection collection)
      Returns:
      ObjectReader
      Throws:
      UnsupportedOperationException - If the method is not overloaded or otherwise
    • createInstance

      default T createInstance(Collection collection, JSONReader.Feature... features)
      Returns:
      ObjectReader
      Throws:
      UnsupportedOperationException - If the method is not overloaded or otherwise
    • createInstance

      default T createInstance(Collection collection, long features)
      Returns:
      ObjectReader
      Throws:
      UnsupportedOperationException - If the method is not overloaded or otherwise
    • acceptExtra

      default void acceptExtra(Object object, String fieldName, Object fieldValue)
      Accepts extra field data that is not mapped to any specific field in the object. This method is called when deserializing JSON objects that contain fields not present in the target class.
      Parameters:
      object - the object being deserialized
      fieldName - the name of the extra field
      fieldValue - the value of the extra field
    • acceptExtra

      default void acceptExtra(Object object, String fieldName, Object fieldValue, long features)
      Accepts extra field data that is not mapped to any specific field in the object. This method is called when deserializing JSON objects that contain fields not present in the target class.
      Parameters:
      object - the object being deserialized
      fieldName - the name of the extra field
      fieldValue - the value of the extra field
      features - the JSON reader features to use
    • createInstance

      default T createInstance(Map map, JSONReader.Feature... features)
      Creates an instance of the object type from a map of field values using the specified features.
      Parameters:
      map - the map containing field names and values
      features - the JSON reader features to use
      Returns:
      a new instance of the object populated with values from the map
    • createInstance

      default T createInstance(Map map, long features)
      Returns:
      ObjectReader
      Throws:
      JSONException - If a suitable ObjectReader is not found
    • accept

      default T accept(T object, Map map, long features)
      Accepts field values from a map and populates the specified object instance.
      Parameters:
      object - the object instance to populate
      map - the map containing field names and values
      features - the JSON reader features to use
      Returns:
      the populated object instance
    • createInstanceNoneDefaultConstructor

      default T createInstanceNoneDefaultConstructor(Map<Long,Object> values)
      Creates an instance of the object type using a non-default constructor with the specified values.
      Parameters:
      values - the map of field hash codes to values
      Returns:
      a new instance of the object created with a non-default constructor
      Throws:
      UnsupportedOperationException - if the method is not overloaded or otherwise
    • getFeatures

      default long getFeatures()
      Gets the features enabled by this ObjectReader.
      Returns:
      the enabled features as a bit mask
    • getTypeKey

      default String getTypeKey()
      Gets the type key used for auto-type support. This key is used to identify the type information in JSON objects.
      Returns:
      the type key string
    • getTypeKeyHash

      default long getTypeKeyHash()
      Gets the hash code of the type key used for auto-type support.
      Returns:
      the hash code of the type key
    • getObjectClass

      default Class<T> getObjectClass()
      Gets the class of objects that this ObjectReader can deserialize.
      Returns:
      the object class, or null if not specified
    • getFieldReader

      default FieldReader getFieldReader(long hashCode)
      Gets the FieldReader for the specified field hash code.
      Parameters:
      hashCode - the hash code of the field name
      Returns:
      the FieldReader for the field, or null if not found
    • getFieldReaderLCase

      default FieldReader getFieldReaderLCase(long hashCode)
      Gets the FieldReader for the specified field hash code, using lowercase matching.
      Parameters:
      hashCode - the hash code of the field name (lowercase)
      Returns:
      the FieldReader for the field, or null if not found
    • setFieldValue

      default boolean setFieldValue(Object object, String fieldName, long fieldNameHashCode, int value)
      Sets the value of a field in the specified object.
      Parameters:
      object - the object in which to set the field value
      fieldName - the name of the field to set
      fieldNameHashCode - the hash code of the field name
      value - the integer value to set
      Returns:
      true if the field was successfully set, false otherwise
    • setFieldValue

      default boolean setFieldValue(Object object, String fieldName, long fieldNameHashCode, long value)
      Sets the value of a field in the specified object.
      Parameters:
      object - the object in which to set the field value
      fieldName - the name of the field to set
      fieldNameHashCode - the hash code of the field name
      value - the long value to set
      Returns:
      true if the field was successfully set, false otherwise
    • getFieldReader

      default FieldReader getFieldReader(String fieldName)
      Gets the FieldReader for the specified field name.
      Parameters:
      fieldName - the name of the field
      Returns:
      the FieldReader for the field, or null if not found
    • setFieldValue

      default boolean setFieldValue(Object object, String fieldName, Object value)
      Sets the value of a field in the specified object.
      Parameters:
      object - the object in which to set the field value
      fieldName - the name of the field to set
      value - the object value to set
      Returns:
      true if the field was successfully set, false otherwise
    • getBuildFunction

      default Function getBuildFunction()
      Gets the build function used to construct the final object instance.
      Returns:
      the build function, or null if not specified
    • autoType

      default ObjectReader autoType(JSONReader.Context context, long typeHash)
      Resolves an ObjectReader for the specified type hash using the JSON reader context.
      Parameters:
      context - the JSON reader context
      typeHash - the hash code of the type name
      Returns:
      the ObjectReader for the type, or null if not found
    • autoType

      default ObjectReader autoType(ObjectReaderProvider provider, long typeHash)
      Resolves an ObjectReader for the specified type hash using the ObjectReaderProvider.
      Parameters:
      provider - the ObjectReaderProvider
      typeHash - the hash code of the type name
      Returns:
      the ObjectReader for the type, or null if not found
    • readJSONBObject

      default T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features)
      Reads an object from JSONB format.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      fieldType - the type of the field being read
      fieldName - the name of the field being read
      features - the features to use
      Returns:
      the deserialized object
      Throws:
      JSONException - if a suitable ObjectReader is not found
    • readArrayMappingJSONBObject

      default T readArrayMappingJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features)
      Reads an object from JSONB format with array mapping.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      fieldType - the type of the field being read
      fieldName - the name of the field being read
      features - the features to use
      Returns:
      the deserialized object
      Throws:
      UnsupportedOperationException - if the method is not overloaded or otherwise
    • readArrayMappingObject

      default T readArrayMappingObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features)
      Reads an object from JSON format with array mapping.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      fieldType - the type of the field being read
      fieldName - the name of the field being read
      features - the features to use
      Returns:
      the deserialized object
      Throws:
      UnsupportedOperationException - if the method is not overloaded or otherwise
    • readObject

      default T readObject(String str, JSONReader.Feature... features)
      Reads an object from the specified JSON string using the provided features.
      Parameters:
      str - the JSON string to parse
      features - the JSON reader features to use
      Returns:
      the deserialized object
    • readObject

      default T readObject(JSONReader jsonReader)
      Reads an object from the specified JSONReader.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      Returns:
      the deserialized object
    • readObject

      default T readObject(JSONReader jsonReader, long features)
      Reads an object from the specified JSONReader using the provided features.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      features - the features to use
      Returns:
      the deserialized object
    • readObject

      T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features)
      Reads an object from the specified JSONReader with the given field type, field name, and features.
      Parameters:
      jsonReader - the JSONReader to use for parsing
      fieldType - the type of the field being read
      fieldName - the name of the field being read
      features - the features to use
      Returns:
      the deserialized object
      Throws:
      JSONException - if a suitable ObjectReader is not found