-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix fastjson 1.x JSONField#unwrapped not work, for issue #2551
- Loading branch information
Showing
2 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2551.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |