Package com.alibaba.fastjson2.reader
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault TAccepts field values from a map and populates the specified object instance.default voidacceptExtra(Object object, String fieldName, Object fieldValue) Accepts extra field data that is not mapped to any specific field in the object.default voidacceptExtra(Object object, String fieldName, Object fieldValue, long features) Accepts extra field data that is not mapped to any specific field in the object.default ObjectReaderautoType(JSONReader.Context context, long typeHash) Resolves an ObjectReader for the specified type hash using the JSON reader context.default ObjectReaderautoType(ObjectReaderProvider provider, long typeHash) Resolves an ObjectReader for the specified type hash using the ObjectReaderProvider.default Tdefault TcreateInstance(long features) default TcreateInstance(Collection collection) default TcreateInstance(Collection collection, long features) default TcreateInstance(Collection collection, JSONReader.Feature... features) default TcreateInstance(Map map, long features) default TcreateInstance(Map map, JSONReader.Feature... features) Creates an instance of the object type from a map of field values using the specified features.default TCreates an instance of the object type using a non-default constructor with the specified values.default FunctionGets the build function used to construct the final object instance.default longGets the features enabled by this ObjectReader.default FieldReadergetFieldReader(long hashCode) Gets the FieldReader for the specified field hash code.default FieldReadergetFieldReader(String fieldName) Gets the FieldReader for the specified field name.default FieldReadergetFieldReaderLCase(long hashCode) Gets the FieldReader for the specified field hash code, using lowercase matching.Gets the class of objects that this ObjectReader can deserialize.default StringGets the type key used for auto-type support.default longGets the hash code of the type key used for auto-type support.default TreadArrayMappingJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) Reads an object from JSONB format with array mapping.default TreadArrayMappingObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) Reads an object from JSON format with array mapping.default TreadJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) Reads an object from JSONB format.default TreadObject(JSONReader jsonReader) Reads an object from the specified JSONReader.default TreadObject(JSONReader jsonReader, long features) Reads an object from the specified JSONReader using the provided features.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.default TreadObject(String str, JSONReader.Feature... features) Reads an object from the specified JSON string using the provided features.default booleansetFieldValue(Object object, String fieldName, long fieldNameHashCode, int value) Sets the value of a field in the specified object.default booleansetFieldValue(Object object, String fieldName, long fieldNameHashCode, long value) Sets the value of a field in the specified object.default booleansetFieldValue(Object object, String fieldName, Object value) Sets the value of a field in the specified object.
-
Field Details
-
HASH_TYPE
static final long HASH_TYPE -
VALUE_NAME
- See Also:
-
-
Method Details
-
createInstance
- Returns:
ObjectReader- Throws:
UnsupportedOperationException- If the method is not overloaded or otherwise
-
createInstance
- Returns:
ObjectReader- Throws:
UnsupportedOperationException- If the method is not overloaded or otherwise
-
createInstance
- Returns:
ObjectReader- Throws:
UnsupportedOperationException- If the method is not overloaded or otherwise
-
createInstance
- Returns:
ObjectReader- Throws:
UnsupportedOperationException- If the method is not overloaded or otherwise
-
createInstance
- Returns:
ObjectReader- Throws:
UnsupportedOperationException- If the method is not overloaded or otherwise
-
acceptExtra
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 deserializedfieldName- the name of the extra fieldfieldValue- the value of the extra field
-
acceptExtra
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 deserializedfieldName- the name of the extra fieldfieldValue- the value of the extra fieldfeatures- the JSON reader features to use
-
createInstance
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 valuesfeatures- the JSON reader features to use- Returns:
- a new instance of the object populated with values from the map
-
createInstance
- Returns:
ObjectReader- Throws:
JSONException- If a suitable ObjectReader is not found
-
accept
Accepts field values from a map and populates the specified object instance.- Parameters:
object- the object instance to populatemap- the map containing field names and valuesfeatures- the JSON reader features to use- Returns:
- the populated object instance
-
createInstanceNoneDefaultConstructor
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
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
Gets the class of objects that this ObjectReader can deserialize.- Returns:
- the object class, or null if not specified
-
getFieldReader
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
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
Sets the value of a field in the specified object.- Parameters:
object- the object in which to set the field valuefieldName- the name of the field to setfieldNameHashCode- the hash code of the field namevalue- the integer value to set- Returns:
- true if the field was successfully set, false otherwise
-
setFieldValue
Sets the value of a field in the specified object.- Parameters:
object- the object in which to set the field valuefieldName- the name of the field to setfieldNameHashCode- the hash code of the field namevalue- the long value to set- Returns:
- true if the field was successfully set, false otherwise
-
getFieldReader
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
Sets the value of a field in the specified object.- Parameters:
object- the object in which to set the field valuefieldName- the name of the field to setvalue- the object value to set- Returns:
- true if the field was successfully set, false otherwise
-
getBuildFunction
Gets the build function used to construct the final object instance.- Returns:
- the build function, or null if not specified
-
autoType
Resolves an ObjectReader for the specified type hash using the JSON reader context.- Parameters:
context- the JSON reader contexttypeHash- the hash code of the type name- Returns:
- the ObjectReader for the type, or null if not found
-
autoType
Resolves an ObjectReader for the specified type hash using the ObjectReaderProvider.- Parameters:
provider- the ObjectReaderProvidertypeHash- the hash code of the type name- Returns:
- the ObjectReader for the type, or null if not found
-
readJSONBObject
Reads an object from JSONB format.- Parameters:
jsonReader- the JSONReader to use for parsingfieldType- the type of the field being readfieldName- the name of the field being readfeatures- 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 parsingfieldType- the type of the field being readfieldName- the name of the field being readfeatures- 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 parsingfieldType- the type of the field being readfieldName- the name of the field being readfeatures- the features to use- Returns:
- the deserialized object
- Throws:
UnsupportedOperationException- if the method is not overloaded or otherwise
-
readObject
Reads an object from the specified JSON string using the provided features.- Parameters:
str- the JSON string to parsefeatures- the JSON reader features to use- Returns:
- the deserialized object
-
readObject
Reads an object from the specified JSONReader.- Parameters:
jsonReader- the JSONReader to use for parsing- Returns:
- the deserialized object
-
readObject
Reads an object from the specified JSONReader using the provided features.- Parameters:
jsonReader- the JSONReader to use for parsingfeatures- the features to use- Returns:
- the deserialized object
-
readObject
Reads an object from the specified JSONReader with the given field type, field name, and features.- Parameters:
jsonReader- the JSONReader to use for parsingfieldType- the type of the field being readfieldName- the name of the field being readfeatures- the features to use- Returns:
- the deserialized object
- Throws:
JSONException- if a suitable ObjectReader is not found
-