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

默认字段不排序 #245

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 11 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,22 @@ static <T> List<T> parseArray(byte[] bytes, Type type, JSONReader.Feature... fea
* @param object Java Object to be serialized into JSON {@link String}
*/
static String toJSONString(Object object) {
return toJSONString(object, Boolean.FALSE);
}

/**
* Serialize Java Object to JSON {@link String}
*
* @param object Java Object to be serialized into JSON {@link String}
* @param sort need to sort
*/
static String toJSONString(Object object, boolean sort) {
try (JSONWriter writer = JSONWriter.of()) {
if (object == null) {
writer.writeNull();
} else {
Class<?> valueClass = object.getClass();
ObjectWriter<?> objectWriter = writer.getObjectWriter(valueClass, valueClass);
ObjectWriter<?> objectWriter = writer.getObjectWriter(valueClass, valueClass, sort);
objectWriter.write(writer, object, null, null, 0);
}
return writer.toString();
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,12 @@ public ObjectWriter getObjectWriter(Class objectClass) {
}

public ObjectWriter getObjectWriter(Type objectType, Class objectClass) {
return this.getObjectWriter(objectType, objectClass, Boolean.FALSE);
}

public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean sort) {
boolean fieldBased = (context.features & Feature.FieldBased.mask) != 0;
return context.provider.getObjectWriter(objectType, objectClass, fieldBased);
return context.provider.getObjectWriter(objectType, objectClass, fieldBased, sort);
}

public static JSONWriter of() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ObjectWriter createObjectWriter(Class objectType) {
, 0
, JSONFactory
.getDefaultObjectWriterProvider()
.getModules()
.getModules(), Boolean.FALSE
);
}

Expand All @@ -56,7 +56,7 @@ public ObjectWriter createObjectWriter(
, FieldWriter... fieldWriters
) {
if (fieldWriters.length == 0) {
return createObjectWriter(objectType, features, Collections.emptyList());
return createObjectWriter(objectType, features, Collections.emptyList(), Boolean.FALSE);
} else {
return new ObjectWriterAdapter(objectType, features, fieldWriters);
}
Expand Down Expand Up @@ -146,6 +146,14 @@ public ObjectWriter createObjectWriter(
Class objectClass
, long features
, final List<ObjectWriterModule> modules
) {
return this.createObjectWriter(objectClass, features, modules, Boolean.FALSE);
}

public ObjectWriter createObjectWriter(
Class objectClass
, long features
, final List<ObjectWriterModule> modules, boolean sort
) {
BeanInfo beanInfo = new BeanInfo();
for (ObjectWriterModule module : modules) {
Expand Down Expand Up @@ -184,7 +192,7 @@ public ObjectWriter createObjectWriter(


if (fieldBased) {
Map<String, FieldWriter> fieldWriterMap = new TreeMap<>();
Map<String, FieldWriter> fieldWriterMap = sort ? new LinkedHashMap<>() : new TreeMap<>();
BeanUtils.declaredFields(objectClass, field -> {
if (Modifier.isTransient(field.getModifiers())) {
return;
Expand All @@ -207,7 +215,7 @@ public ObjectWriter createObjectWriter(
}

if (!fieldWritersCreated) {
Map<String, FieldWriter> fieldWriterMap = new TreeMap<>();
Map<String, FieldWriter> fieldWriterMap = sort ? new LinkedHashMap<>() : new TreeMap<>();

BeanUtils.fields(objectClass, field -> {
if (!Modifier.isPublic(field.getModifiers())) {
Expand Down Expand Up @@ -309,8 +317,8 @@ public ObjectWriter createObjectWriter(
}

handleIgnores(beanInfo, fieldWriters);

Collections.sort(fieldWriters);
if(sort)
Collections.sort(fieldWriters);

ObjectWriterAdapter writerAdapter = new ObjectWriterAdapter(objectClass, beanInfo.typeKey, beanInfo.typeName, writerFeatures, fieldWriters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,23 @@ public ObjectWriter createObjectWriter(
Class objectClass,
long features,
List<ObjectWriterModule> modules
) {
return this.createObjectWriter(objectClass, features, modules, false);
}

@Override
public ObjectWriter createObjectWriter(
Class objectClass,
long features,
List<ObjectWriterModule> modules, boolean sort
) {
int modifiers = objectClass.getModifiers();
boolean externalClass = classLoader.isExternalClass(objectClass);
boolean publicClass = Modifier.isPublic(modifiers);

if (!publicClass || externalClass) {
if (!JDKUtils.UNSAFE_SUPPORT) {
return super.createObjectWriter(objectClass, features, modules);
return super.createObjectWriter(objectClass, features, modules, Boolean.FALSE);
}
}

Expand All @@ -135,7 +144,7 @@ public ObjectWriter createObjectWriter(
}

if (beanInfo.serializeFilters != null && beanInfo.serializeFilters.length > 0) {
return super.createObjectWriter(objectClass, features, modules);
return super.createObjectWriter(objectClass, features, modules, Boolean.FALSE);
}

long writerFeatures = features | beanInfo.writerFeatures;
Expand All @@ -145,14 +154,14 @@ public ObjectWriter createObjectWriter(
}

if (fieldBased && JDKUtils.JVM_VERSION >= 11 && !JDKUtils.LANG_UNNAMED && Throwable.class.isAssignableFrom(objectClass)) {
return super.createObjectWriter(objectClass, features, modules);
return super.createObjectWriter(objectClass, features, modules, Boolean.FALSE);
}

boolean record = BeanUtils.isRecord(objectClass);

List<FieldWriter> fieldWriters;
if (fieldBased && !record) {
Map<String, FieldWriter> fieldWriterMap = new TreeMap<>();
Map<String, FieldWriter> fieldWriterMap = sort ? new TreeMap<>() : new LinkedHashMap<>();
final FieldInfo fieldInfo = new FieldInfo();
BeanUtils.declaredFields(objectClass, field -> {
if (Modifier.isTransient(field.getModifiers())) {
Expand All @@ -166,7 +175,7 @@ boolean record = BeanUtils.isRecord(objectClass);
});
fieldWriters = new ArrayList<>(fieldWriterMap.values());
} else {
Map<String, FieldWriter> fieldWriterMap = new TreeMap<>();
Map<String, FieldWriter> fieldWriterMap = sort ? new TreeMap<>() : new LinkedHashMap<>();
List<FieldWriter> fieldWriterList = new ArrayList<>();
boolean fieldWritersCreated = false;
for (ObjectWriterModule module : modules) {
Expand All @@ -180,7 +189,7 @@ boolean record = BeanUtils.isRecord(objectClass);
for (FieldWriter fieldWriter : fieldWriterList) {
Method method = fieldWriter.getMethod();
if (method == null) {
return super.createObjectWriter(objectClass, writerFeatures, modules);
return super.createObjectWriter(objectClass, writerFeatures, modules, Boolean.FALSE);
}
fieldWriterMap.putIfAbsent(fieldWriter.getFieldName(), fieldWriter);
}
Expand Down Expand Up @@ -286,7 +295,8 @@ boolean record = BeanUtils.isRecord(objectClass);
}

handleIgnores(beanInfo, fieldWriters);
Collections.sort(fieldWriters);
if(sort)
Collections.sort(fieldWriters);

boolean match = true;
if (fieldWriters.size() >= 100 || Throwable.class.isAssignableFrom(objectClass)) {
Expand All @@ -307,7 +317,7 @@ boolean record = BeanUtils.isRecord(objectClass);
}

if (!match) {
return super.createObjectWriter(objectClass, writerFeatures, modules);
return super.createObjectWriter(objectClass, writerFeatures, modules, Boolean.FALSE);
}

ClassWriter cw = new ClassWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public ObjectWriter getObjectWriter(Type objectType, Class objectClass) {
}

public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean fieldBased) {
return this.getObjectWriter(objectClass, objectClass, fieldBased, Boolean.FALSE);
}

public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean fieldBased, boolean sort) {
ObjectWriter objectWriter = fieldBased
? cacheFieldBased.get(objectType)
: cache.get(objectType);
Expand Down Expand Up @@ -155,7 +159,7 @@ public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean
objectWriter = creator.createObjectWriter(
objectClass,
fieldBased ? JSONWriter.Feature.FieldBased.mask : 0
, modules
, modules, sort
);
ObjectWriter previous = fieldBased
? cacheFieldBased.putIfAbsent(objectType, objectWriter)
Expand Down