fastjson2
fastjson2 copied to clipboard
[BUG] Deserialization to JSONObject when using SortedMap
xxxObject { private SortedMap<Integer, AckSampleConfig> ackSampleConfig; }
it works if the SortedMap is Map, otherwise classcastException throws.
@Test
public void test() {
String str = "{\"items\":{1:{\"id\":123}}}";
Bean bean = JSON.parseObject(str, Bean.class);
assertEquals(1, bean.items.size());
assertEquals(123, bean.items.get(1).id);
}
@Data
public static class Bean {
private SortedMap<Integer, Item> items;
}
public static class Item {
public int id;
}
问题没重现
我们多了一个层级。
class NamespaceConfigs {
Map<String, XXXConfig> configs;
}
class XXXConfig {
private SortedMap<Integer, AckSampleConfig> ackSampleConfig;
}
然后解析的时候用的
var configs = JSON.parseObject(json, NamespaceConfigs.class, new TypeReference<>);
@Test
public void test() {
String str = "{\"items\":{1:{\"config\":{2:{\"id\":123}}}}}";
Bean bean = JSON.parseObject(str, Bean.class);
assertEquals(1, bean.items.size());
assertEquals(123, bean.items.get(1).getConfig().get(2).id);
}
@Data
public static class Bean {
private Map<Integer, XXXConfig> items;
}
public static class XXXConfig {
private SortedMap<Integer, AckSampleConfig> config;
public SortedMap<Integer, AckSampleConfig> getConfig() {
return config;
}
public void setConfig(SortedMap<Integer, AckSampleConfig> config) {
this.config = config;
}
}
public static class AckSampleConfig {
public int id;
}
问题未重现,你可帮忙构建一个简化能重现问题的case么?