Skip to content

Commit

Permalink
fix JSONWriter#getPath not correct on Map, for issue #2590
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 30, 2024
1 parent 6fca66c commit 24828fc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
PropertyFilter propertyFilter = context.getPropertyFilter();
AfterFilter afterFilter = context.getAfterFilter();
boolean writeNulls = context.isEnabled(JSONWriter.Feature.WriteNulls.mask);
boolean refDetect = context.isEnabled(ReferenceDetection.mask);

for (Map.Entry entry : (Iterable<Map.Entry>) map.entrySet()) {
Object value = entry.getValue();
Expand All @@ -618,41 +619,58 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
key = entryKey.toString();
}

if (propertyPreFilter != null) {
if (!propertyPreFilter.process(jsonWriter, object, key)) {
String refPath = null;
if (refDetect) {
refPath = jsonWriter.setPath(key, value);
if (refPath != null) {
jsonWriter.writeName(key);
jsonWriter.writeReference(refPath);
jsonWriter.popPath(value);
continue;
}
}

if (nameFilter != null) {
key = nameFilter.process(object, key, value);
}
try {
if (propertyPreFilter != null) {
if (!propertyPreFilter.process(jsonWriter, object, key)) {
continue;
}
}

if (propertyFilter != null) {
if (!propertyFilter.apply(object, key, value)) {
continue;
if (nameFilter != null) {
key = nameFilter.process(object, key, value);
}
}

if (valueFilter != null) {
value = valueFilter.apply(object, key, value);
}
if (propertyFilter != null) {
if (!propertyFilter.apply(object, key, value)) {
continue;
}
}

if (value == null) {
if ((jsonWriter.getFeatures(features) & JSONWriter.Feature.WriteNulls.mask) == 0) {
continue;
if (valueFilter != null) {
value = valueFilter.apply(object, key, value);
}
}

jsonWriter.writeName(key);
jsonWriter.writeColon();
if (value == null) {
if ((jsonWriter.getFeatures(features) & JSONWriter.Feature.WriteNulls.mask) == 0) {
continue;
}
}

if (value == null) {
jsonWriter.writeNull();
} else {
Class<?> valueType = value.getClass();
ObjectWriter valueWriter = jsonWriter.getObjectWriter(valueType);
valueWriter.write(jsonWriter, value, fieldName, fieldType, this.features);
jsonWriter.writeName(key);
jsonWriter.writeColon();

if (value == null) {
jsonWriter.writeNull();
} else {
Class<?> valueType = value.getClass();
ObjectWriter valueWriter = jsonWriter.getObjectWriter(valueType);
valueWriter.write(jsonWriter, value, fieldName, fieldType, this.features);
}
} finally {
if (refDetect) {
jsonWriter.popPath(value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.alibaba.fastjson2.issues_2500;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class Issue2590A {
@Test
public void test_map1_childItem() {
Bean bean = new Bean();
bean.map1 = new HashMap<String, Item>() {
{
put("childItem", new Item(301));
put("childItem1", new Item(401));
}
};

List<String> paths = new ArrayList<>();
FilterDemo filter = new FilterDemo(paths);

JSON.toJSONString(bean, filter, JSONWriter.Feature.ReferenceDetection);
assertTrue(paths.contains("$.map1.childItem"));
assertTrue(paths.contains("$.map1.childItem1"));
}

public static class FilterDemo
implements PropertyPreFilter {
private final List<String> paths;

public FilterDemo(List<String> paths) {
this.paths = paths;
}

@Override
public boolean process(JSONWriter writer, Object source, String name) {
String path = writer.getPath();
paths.add(path);
return true;
}
}

public static class Bean {
public Map<String, Item> map1;
}

public static class Item {
public int id;

public Item() {
}

public Item(int id) {
this.id = id;
}
}
}

0 comments on commit 24828fc

Please sign in to comment.