Skip to content

Commit

Permalink
fix fastjson 1.x JSONField#unwrapped not work, for issue #2551
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 10, 2024
1 parent 3198ef5 commit 8dee60c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
24 changes: 20 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,13 @@ public static void setters(Class objectClass, BeanInfo beanInfo, Class mixin, Co
) {
Annotation[] annotations = getAnnotations(method);

boolean unwrapped = false;
AtomicBoolean unwrapped = new AtomicBoolean(false);
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
JSONField jsonField = findAnnotation(annotation, JSONField.class);
if (jsonField != null) {
if (jsonField.unwrapped()) {
unwrapped = true;
unwrapped.set(true);
break;
}
continue;
Expand All @@ -526,15 +526,31 @@ public static void setters(Class objectClass, BeanInfo beanInfo, Class mixin, Co
case "com.fasterxml.jackson.annotation.JsonAnySetter":
case "com.alibaba.fastjson2.adapter.jackson.annotation.JsonAnySetter":
if (JSONFactory.isUseJacksonAnnotation()) {
unwrapped = true;
unwrapped.set(true);
}
break;
case "com.alibaba.fastjson.annotation.JSONField": {
BeanUtils.annotationMethods(annotation.getClass(), m -> {
String name = m.getName();
try {
if ("unwrapped".equals(name)) {
Object result = m.invoke(annotation);
if ((Boolean) result) {
unwrapped.set(true);
}
}
} catch (Throwable ignored) {
// ignored
}
});
break;
}
default:
break;
}
}

if (unwrapped) {
if (unwrapped.get()) {
methodConsumer.accept(method);
}
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

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

public class Issue2551 {
@Test
public void test() {
String json = "{\"name\": \"zhangsan\", \"age\": 18}";
People p = JSON.parseObject(json, People.class);

assertEquals("zhangsan", p.getName());
assertEquals(18, p.getExtra().get("age"));

JSONPath.set(p, "$.age", 20);
assertEquals(20, p.getExtra().get("age"));
}

static class People {
private String name;
private Map<String, Object> extra = new HashMap<>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<String, Object> getExtra() {
return extra;
}

public void setExtra(Map<String, Object> extra) {
this.extra = extra;
}

@JSONField(unwrapped = true)
public void set(String key, Object value) {
this.extra.put(key, value);
}

@JSONField(unwrapped = true)
public Object get(String key) {
return this.extra.get(key);
}
}

@Test
public void testfj() {
String json = "{\"name\": \"zhangsan\", \"age\": 18}";
People1 p = com.alibaba.fastjson.JSON.parseObject(json, People1.class);

assertEquals("zhangsan", p.getName());
assertEquals(18, p.getExtra().get("age"));

com.alibaba.fastjson.JSONPath.set(p, "$.age", 20);
assertEquals(20, p.getExtra().get("age"));
}

static class People1 {
private String name;
private Map<String, Object> extra = new HashMap<>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<String, Object> getExtra() {
return extra;
}

public void setExtra(Map<String, Object> extra) {
this.extra = extra;
}

@com.alibaba.fastjson.annotation.JSONField(unwrapped = true)
public void set(String key, Object value) {
this.extra.put(key, value);
}

@com.alibaba.fastjson.annotation.JSONField(unwrapped = true)
public Object get(String key) {
return this.extra.get(key);
}
}
}

0 comments on commit 8dee60c

Please sign in to comment.