Skip to content

Commit

Permalink
support new Feature#SortMapEntriesByKeys, for issue #2318
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 17, 2024
1 parent 581eff7 commit a0fe523
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
18 changes: 17 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,14 @@ public enum Feature {
WriteNameAsSymbol(1 << 18),
WriteBigDecimalAsPlain(1 << 19),
UseSingleQuotes(1 << 20),

/**
* The serialized Map will first be sorted according to Key,
* and is used in some scenarios where serialized content needs to be signed.
* SortedMap and derived classes do not need to do this.
* This Feature does not work for LinkedHashMap.
* @deprecated Use {@link Feature#SortMapEntriesByKeys} instead.
*/
MapSortField(1 << 21),
WriteNullListAsEmpty(1 << 22),
/**
Expand Down Expand Up @@ -1891,7 +1899,15 @@ public enum Feature {
/**
* @since 2.0.34
*/
NotWriteNumberClassName(1L << 40);
NotWriteNumberClassName(1L << 40),

/**
* The serialized Map will first be sorted according to Key,
* and is used in some scenarios where serialized content needs to be signed.
* SortedMap and derived classes do not need to do this.
* @since 2.0.48
*/
SortMapEntriesByKeys(1L << 41);

public final long mask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import java.time.temporal.Temporal;
import java.util.*;

import static com.alibaba.fastjson2.JSONWriter.Feature.BrowserCompatible;
import static com.alibaba.fastjson2.JSONWriter.Feature.WriteNonStringKeyAsString;
import static com.alibaba.fastjson2.JSONWriter.Feature.*;
import static com.alibaba.fastjson2.util.JDKUtils.UNSAFE;
import static com.alibaba.fastjson2.util.TypeUtils.CLASS_JSON_OBJECT_1x;

Expand Down Expand Up @@ -419,8 +418,9 @@ public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type f
Map map = (Map) object;

features |= jsonWriter.getFeatures();
if ((features & JSONWriter.Feature.MapSortField.mask) != 0) {
if (!(map instanceof SortedMap) && map.getClass() != LinkedHashMap.class) {
if ((features & (MapSortField.mask | SortMapEntriesByKeys.mask)) != 0) {
if (!(map instanceof SortedMap)
&& (map.getClass() != LinkedHashMap.class || (features & SortMapEntriesByKeys.mask) != 0)) {
map = new TreeMap<>(map);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.fastjson2.issues_2300;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.Map;

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

public class Issue2318 {
@Test
public void test() {
Map<Object, Object> m = new LinkedHashMap<>();
m.put("abc", 1);
m.put("123", 2);
assertEquals("{\"abc\":1,\"123\":2}", JSON.toJSONString(m, JSONWriter.Feature.MapSortField));
assertEquals("{\"123\":2,\"abc\":1}", JSON.toJSONString(m, JSONWriter.Feature.SortMapEntriesByKeys));

Bean bean = new Bean();
bean.map = m;

assertEquals("{\"map\":{\"abc\":1,\"123\":2}}", JSON.toJSONString(bean, JSONWriter.Feature.MapSortField));
assertEquals("{\"map\":{\"123\":2,\"abc\":1}}", JSON.toJSONString(bean, JSONWriter.Feature.SortMapEntriesByKeys));
}

public static class Bean {
public Map map;
}
}

0 comments on commit a0fe523

Please sign in to comment.