Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize codes update comments #155

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ static <T> T parseObject(byte[] bytes, int offset, int length, Charset charset,
* @param type specify the {@link Type} to be converted
* @param consumer the consumer of the parsing result object
* @param features features to be enabled in parsing
* @throws JSONException IO exception occurred in reading
* @throws JSONException If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs
* @see JSON#parseObject(InputStream, Charset, char, Type, Consumer, JSONReader.Feature...)
* @since 2.0.2
*/
static <T> void parseObject(InputStream input, Type type, Consumer<T> consumer, JSONReader.Feature... features) {
parseObject(input, StandardCharsets.UTF_8, '\n', type, consumer, features);
Expand All @@ -474,7 +475,8 @@ static <T> void parseObject(InputStream input, Type type, Consumer<T> consumer,
* @param type specify the {@link Type} to be converted
* @param consumer the consumer of the parsing result object
* @param features features to be enabled in parsing
* @throws JSONException IO exception occurred in reading
* @throws JSONException If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs
* @since 2.0.2
*/
@SuppressWarnings("unchecked")
static <T> void parseObject(InputStream input, Charset charset, char delimiter, Type type, Consumer<T> consumer, JSONReader.Feature... features) {
Expand Down Expand Up @@ -546,7 +548,8 @@ static <T> void parseObject(InputStream input, Charset charset, char delimiter,
* @param delimiter specify the delimiter
* @param type specify the {@link Type} to be converted
* @param consumer the consumer of the parsing result object
* @throws JSONException IO exception occurred in reading
* @throws JSONException If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs
* @since 2.0.2
*/
@SuppressWarnings("unchecked")
static <T> void parseObject(Reader input, char delimiter, Type type, Consumer<T> consumer) {
Expand Down Expand Up @@ -1166,6 +1169,9 @@ static <T> T toJavaObject(Object object, Class<T> clazz) {
return TypeUtils.cast(object, clazz);
}

/**
* @since 2.0.2
*/
static void mixIn(Class<?> target, Class<?> mixinSource) {
JSONFactory.defaultObjectWriterProvider.mixIn(target, mixinSource);
JSONFactory.getDefaultObjectReaderProvider().mixIn(target, mixinSource);
Expand All @@ -1176,6 +1182,7 @@ static void mixIn(Class<?> target, Class<?> mixinSource) {
*
* @see JSONFactory#getDefaultObjectReaderProvider()
* @see com.alibaba.fastjson2.reader.ObjectReaderProvider#register(Type, ObjectReader)
* @since 2.0.2
*/
static boolean register(Type type, ObjectReader<?> objectReader) {
return JSONFactory.getDefaultObjectReaderProvider().register(type, objectReader);
Expand All @@ -1186,6 +1193,7 @@ static boolean register(Type type, ObjectReader<?> objectReader) {
*
* @see JSONFactory#getDefaultObjectReaderProvider()
* @see com.alibaba.fastjson2.writer.ObjectWriterProvider#register(Type, ObjectWriter)
* @since 2.0.2
*/
static boolean register(Type type, ObjectWriter<?> objectReader) {
return JSONFactory.defaultObjectWriterProvider.register(type, objectReader);
Expand Down
44 changes: 34 additions & 10 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Object get(Object key) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
return super.get(
key.toString()
Expand All @@ -97,35 +98,56 @@ public Object get(Object key) {
return super.get(key);
}

/**
* Returns a set view of the mappings contained in this map
*/
@Override
@SuppressWarnings("unchecked")
public Set<Map.Entry<String, Object>> entrySet() {
return super.entrySet();
}

/**
* Returns a set view of the keys contained in this map
*/
@Override
@SuppressWarnings("unchecked")
public Set<String> keySet() {
return super.keySet();
}

/**
* Returns true if this map contains a mapping for the specified key
*
* @param key the key whose presence in this map is to be tested
*/
public boolean containsKey(String key) {
return super.containsKey(key);
}

/**
* Returns true if this map contains a mapping for the specified key
*
* @param key the key whose presence in this map is to be tested
*/
@Override
public boolean containsKey(Object key) {
boolean result = super.containsKey(key);
if (!result) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
result = super.containsKey(key.toString());
}
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
return super.containsKey(key.toString());
}
return result;

return super.containsKey(key);
}

/**
* @param key the key whose associated value is to be returned
* @param defaultValue the default mapping of the key
*/
@SuppressWarnings("unchecked")
public Object getOrDefault(String key, Object defaultValue) {
return super.getOrDefault(
key, defaultValue
Expand All @@ -138,10 +160,12 @@ public Object getOrDefault(String key, Object defaultValue) {
* @since 2.0.2
*/
@Override
@SuppressWarnings("unchecked")
public Object getOrDefault(Object key, Object defaultValue) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
return super.getOrDefault(
key.toString(), defaultValue
Expand Down
18 changes: 15 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/TypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TypeReference() {
* @throws NullPointerException If the {@link Type} is null
*/
@SuppressWarnings("unchecked")
public TypeReference(Type type, boolean raw) {
public TypeReference(Type type) {
if (type == null) {
throw new NullPointerException();
}
Expand All @@ -60,11 +60,23 @@ public TypeReference(Type type, boolean raw) {
}

/**
* For example
* <pre>{@code
* Class<T> klass = ...;
* TypeReference<Response<T>> ref = new TypeReference<Response<T>>(new Type[]{klass}){};
* }</pre>
*
* @param actualTypeArguments an array of Type objects representing the actual type arguments to this type
* @throws NullPointerException If the {@code actualTypeArguments} is null or empty
* @since 2.0.2
*/
@SuppressWarnings("unchecked")
public TypeReference(Type... actualTypeArguments) {
public TypeReference(Type[] actualTypeArguments) {
if (actualTypeArguments == null ||
actualTypeArguments.length == 0) {
throw new NullPointerException();
}

Class<?> thisClass = getClass();
Type superClass = thisClass.getGenericSuperclass();
ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0];
Expand Down Expand Up @@ -145,7 +157,7 @@ public T parseArray(JSONArray object) {
* @param type specify the {@link Type} to be converted
*/
public static TypeReference<?> get(Type type) {
return new TypeReference<Object>(type, true) {
return new TypeReference<Object>(type) {
// nothing
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.alibaba.fastjson2.TypeReference;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Type;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
Expand All @@ -23,12 +25,12 @@ public void test() {
assertNotNull(user);
}

public static <T> RegResponse<T> testFastJson(Class<T> clasz) {
public static <T> RegResponse<T> testFastJson(Class<T> clazz) {

//把body解析成一个对象
String body = "{\"retCode\":\"200\", \"result\":{\"name\":\"Zhangsan\",\"password\":\"123\"}}";

return JSON.parseObject(body, new TypeReference<RegResponse<T>>(clasz) {});
return JSON.parseObject(body, new TypeReference<RegResponse<T>>(new Type[]{clazz}) {});
}
}

Expand Down