Skip to content

Commit

Permalink
fix toJavaObject features not work, for issue #2632
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 29, 2024
1 parent 5809792 commit d81ee9d
Show file tree
Hide file tree
Showing 32 changed files with 191 additions and 43 deletions.
22 changes: 19 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,29 @@ public byte[] toJSONBBytes(JSONWriter.Feature... features) {
*/
@SuppressWarnings("unchecked")
public <T> T to(Type type) {
return to(type, 0L);
}

/**
* Convert this {@link JSONArray} to the specified Object
*
* <pre>{@code
* JSONArray array = ...
* List<User> users = array.to(new TypeReference<ArrayList<User>>(){}.getType());
* }</pre>
*
* @param type specify the {@link Type} to be converted
* @since 2.0.51
*/
@SuppressWarnings("unchecked")
public <T> T to(Type type, long features) {
if (type == String.class) {
return (T) toString();
}

ObjectReaderProvider provider = JSONFactory.getDefaultObjectReaderProvider();
ObjectReader<T> objectReader = provider.getObjectReader(type);
return objectReader.createInstance(this);
return objectReader.createInstance(this, features);
}

/**
Expand Down Expand Up @@ -1179,7 +1195,7 @@ public <T> T getObject(int index, Type type, JSONReader.Feature... features) {

if (value instanceof Collection) {
ObjectReader<T> objectReader = provider.getObjectReader(type, fieldBased);
return objectReader.createInstance((Collection) value);
return objectReader.createInstance((Collection) value, featuresValue);
}

Class clazz = TypeUtils.getMapping(type);
Expand Down Expand Up @@ -1238,7 +1254,7 @@ public <T> T getObject(int index, Class<T> type, JSONReader.Feature... features)

if (value instanceof Collection) {
ObjectReader<T> objectReader = provider.getObjectReader(type, fieldBased);
return objectReader.createInstance((Collection) value);
return objectReader.createInstance((Collection) value, featuresValue);
}

Class clazz = TypeUtils.getMapping(type);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ public <T> T getObject(String key, Class<T> type, JSONReader.Feature... features

if (value instanceof Collection) {
ObjectReader<T> objectReader = provider.getObjectReader(type, fieldBased);
return objectReader.createInstance((Collection) value);
return objectReader.createInstance((Collection) value, features);
}

Class clazz = TypeUtils.getMapping(type);
Expand Down Expand Up @@ -1411,7 +1411,7 @@ public <T> T getObject(String key, Type type, JSONReader.Feature... features) {

if (value instanceof Collection) {
ObjectReader<T> objectReader = provider.getObjectReader(type, fieldBased);
return objectReader.createInstance((Collection) value);
return objectReader.createInstance((Collection) value, features);
}

if (type instanceof Class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ protected void acceptAny(T object, Object fieldValue, long features) {
: new JSONReader.Feature[0];
typedFieldValue = ((JSONObject) fieldValue).to(fieldType, toFeatures);
} else if (fieldValue instanceof JSONArray) {
typedFieldValue = ((JSONArray) fieldValue).to(fieldType);
typedFieldValue = ((JSONArray) fieldValue).to(fieldType, features);
} else if ((features == 0 || features == JSONReader.Feature.SupportSmartMatch.mask) // default or fastjson 1.x default
&& !fieldClass.isInstance(fieldValue) && format == null
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Collection<V> createList(JSONReader.Context context) {
return new ArrayList<>();
}

return (Collection<V>) getObjectReader(context).createInstance();
return (Collection<V>) getObjectReader(context).createInstance(features);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ObjectArrayReader() {
}

@Override
public Object[] createInstance(Collection collection) {
public Object[] createInstance(Collection collection, long features) {
Object[] array = new Object[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
return new Object[types.length];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Object[] values = (Object[]) Array.newInstance(componentClass, collection.size());
int index = 0;
for (Object item : collection) {
Expand All @@ -160,9 +160,9 @@ public Object createInstance(Collection collection) {
if (item instanceof Map) {
item = objectReader.createInstance((Map) item);
} else if (item instanceof Collection) {
item = objectReader.createInstance((Collection) item);
item = objectReader.createInstance((Collection) item, features);
} else if (item instanceof Object[]) {
item = objectReader.createInstance(JSONArray.of((Object[]) item));
item = objectReader.createInstance(JSONArray.of((Object[]) item), features);
} else if (item != null) {
Class<?> itemClass = item.getClass();
if (itemClass.isArray()) {
Expand All @@ -171,7 +171,7 @@ public Object createInstance(Collection collection) {
for (int i = 0; i < length; i++) {
array.add(Array.get(item, i));
}
item = objectReader.createInstance(array);
item = objectReader.createInstance(array, features);
} else {
throw new JSONException("component type not match, expect " + componentType.getName() + ", but " + itemClass);
}
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/reader/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ default T createInstance(long features) {
* @throws UnsupportedOperationException If the method is not overloaded or otherwise
*/
default T createInstance(Collection collection) {
return createInstance(collection, 0L);
}

/**
* @return {@link T}
* @throws UnsupportedOperationException If the method is not overloaded or otherwise
*/
default T createInstance(Collection collection, JSONReader.Feature... features) {
return createInstance(collection, JSONReader.Feature.of(features));
}

/**
* @return {@link T}
* @throws UnsupportedOperationException If the method is not overloaded or otherwise
*/
default T createInstance(Collection collection, long features) {
throw new UnsupportedOperationException(this.getClass().getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ protected void initDefaultValue(T object) {
}
}

public T createInstance(Collection collection) {
public T createInstance(Collection collection, long features) {
T object = createInstance(0L);
int index = 0;
for (Object fieldValue : collection) {
Expand Down Expand Up @@ -632,7 +632,7 @@ typeName, getObjectClass(), features2
}
}

T object = createInstance(0L);
T object = createInstance(features);

if (extraFieldReader == null
&& (features2 & (JSONReader.Feature.SupportSmartMatch.mask | JSONReader.Feature.ErrorOnUnknownProperties.mask)) == 0
Expand All @@ -652,7 +652,7 @@ typeName, getObjectClass(), features2
&& fieldValue instanceof JSONArray
) {
ObjectReader objectReader = fieldReader.getObjectReader(provider);
Object fieldValueList = objectReader.createInstance((JSONArray) fieldValue);
Object fieldValueList = objectReader.createInstance((JSONArray) fieldValue, features);
fieldReader.accept(object, fieldValueList);
continue;
} else if (fieldValue instanceof JSONObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Double[] array = new Double[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
double[] array = new double[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Float[] array = new Float[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
float[] array = new float[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ObjectReaderImplInt16Array
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Short[] array = new Short[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
short[] array = new short[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class ObjectReaderImplInt32Array
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Integer[] array = new Integer[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
int[] array = new int[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class ObjectReaderImplInt64Array
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Long[] array = new Long[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
long[] array = new long[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Byte[] array = new Byte[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
byte[] bytes = new byte[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public Function getBuildFunction() {
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
int size = collection.size();

if (size == 0 && (listClass == List.class)) {
Expand Down Expand Up @@ -272,7 +272,7 @@ public Object createInstance(Collection collection) {
if (itemObjectReader == null) {
itemObjectReader = provider.getObjectReader(itemType);
}
value = itemObjectReader.createInstance((JSONObject) value, 0L);
value = itemObjectReader.createInstance((JSONObject) value, features);
} else if (valueClass != itemType) {
Function typeConvert = provider.getTypeConvert(valueClass, itemType);
if (typeConvert != null) {
Expand All @@ -287,7 +287,7 @@ public Object createInstance(Collection collection) {
if (itemObjectReader == null) {
itemObjectReader = provider.getObjectReader(itemType);
}
value = itemObjectReader.createInstance((Collection) value);
value = itemObjectReader.createInstance((Collection) value, features);
} else if (itemClass.isInstance(value)) {
// skip
} else if (Enum.class.isAssignableFrom(itemClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public Object createInstance(long features) {
}

@Override
public Object createInstance(Collection collection) {
Collection list = (Collection) createInstance(0);
public Object createInstance(Collection collection, long features) {
Collection list = (Collection) createInstance(features);
for (Object item : collection) {
list.add(TypeUtils.toLong(item));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Object createInstance(long features) {
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
if (listType.isInstance(collection)) {
boolean typeMatch = true;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Object createInstance(Map input, long features) {
valueObjectReader = provider.getObjectReader(valueType);
}
try {
value = valueObjectReader.createInstance((JSONArray) value);
value = valueObjectReader.createInstance((JSONArray) value, features);
} catch (Exception ignored) {
// ignored
}
Expand All @@ -127,7 +127,7 @@ public Object createInstance(Map input, long features) {
if (valueObjectReader == null) {
valueObjectReader = provider.getObjectReader(valueType);
}
value = valueObjectReader.createInstance((Collection) value);
value = valueObjectReader.createInstance((Collection) value, features);
} else {
if (!valueClass.isInstance(value)) {
throw new JSONException("can not convert from " + valueClass + " to " + valueType);
Expand Down Expand Up @@ -193,7 +193,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
} else {
object = instanceType == HashMap.class
? new HashMap<>()
: (Map) createInstance();
: (Map) createInstance(features);
}

for (int i = 0; ; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
Number[] array = new Number[collection.size()];
int i = 0;
for (Object item : collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Object createInstance(long features) {
return new JSONObject();
}

public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
return collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Object createInstance(Map map, long features) {
}

@Override
public Object createInstance(Collection collection) {
public Object createInstance(Collection collection, long features) {
if (collection == null) {
return null;
}
Expand Down
Loading

0 comments on commit d81ee9d

Please sign in to comment.