Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Spring RestController 返回类型为map<Long, Object>时格式错误, long 数据多了一层双引号 #4388 #4389

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 17 additions & 12 deletions src/main/java/com/alibaba/fastjson/serializer/MapSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package com.alibaba.fastjson.serializer;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
* @author wenshao[[email protected]]
Expand Down Expand Up @@ -118,7 +123,7 @@ public void write(JSONSerializer serializer
continue;
}
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
if (!this.applyName(serializer, object, strKey)) {
continue;
}
Expand All @@ -133,7 +138,7 @@ public void write(JSONSerializer serializer
continue;
}
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
if (!this.applyName(serializer, object, strKey)) {
continue;
}
Expand All @@ -149,7 +154,7 @@ public void write(JSONSerializer serializer
continue;
}
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
if (!this.apply(serializer, object, strKey, value)) {
continue;
}
Expand All @@ -164,7 +169,7 @@ public void write(JSONSerializer serializer
continue;
}
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
if (!this.apply(serializer, object, strKey, value)) {
continue;
}
Expand All @@ -178,7 +183,7 @@ public void write(JSONSerializer serializer
if (entryKey == null || entryKey instanceof String) {
entryKey = this.processKey(serializer, object, (String) entryKey, value);
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
entryKey = this.processKey(serializer, object, strKey, value);
}
}
Expand All @@ -189,7 +194,7 @@ public void write(JSONSerializer serializer
if (entryKey == null || entryKey instanceof String) {
entryKey = this.processKey(serializer, object, (String) entryKey, value);
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
entryKey = this.processKey(serializer, object, strKey, value);
}
}
Expand All @@ -201,7 +206,7 @@ public void write(JSONSerializer serializer
} else {
boolean objectOrArray = entryKey instanceof Map || entryKey instanceof Collection;
if (!objectOrArray) {
String strKey = JSON.toJSONString(entryKey);
String strKey = TypeUtils.cast(entryKey, String.class);
value = this.processValue(serializer, null, object, strKey, value, features);
}
}
Expand Down Expand Up @@ -231,7 +236,7 @@ public void write(JSONSerializer serializer

if ((out.isEnabled(NON_STRINGKEY_AS_STRING) || SerializerFeature.isEnabled(features, SerializerFeature.WriteNonStringKeyAsString))
&& !(entryKey instanceof Enum)) {
String strEntryKey = JSON.toJSONString(entryKey);
String strEntryKey = TypeUtils.cast(entryKey, String.class);
serializer.write(strEntryKey);
} else {
serializer.write(entryKey);
Expand Down
45 changes: 40 additions & 5 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,47 @@
import com.alibaba.fastjson.serializer.CalendarCodec;
import com.alibaba.fastjson.serializer.SerializeBeanInfo;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Clob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -1086,6 +1115,10 @@ public Object apply(Object obj, Class clazz) {
}
};

public static <T> T cast(final Object obj, final Class<T> clazz) {
return cast(obj, clazz, null);
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> T cast(final Object obj, final Class<T> clazz, ParserConfig config) {
if (obj == null) {
Expand Down Expand Up @@ -1313,7 +1346,9 @@ public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping)
}
throw new JSONException("can not cast to : " + clazz.getName());
}

public static <T> T cast(Object obj, Type type){
return cast(obj, type, null);
}
@SuppressWarnings("unchecked")
public static <T> T cast(Object obj, Type type, ParserConfig mapping) {
if (obj == null) {
Expand Down