-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd57a2d
commit 0e06d15
Showing
6 changed files
with
67 additions
and
12 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
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
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
55 changes: 55 additions & 0 deletions
55
src/test/java/com/fasterxml/jackson/databind/node/POJONodeTest.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,55 @@ | ||
package com.fasterxml.jackson.databind.node; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
public class POJONodeTest extends NodeTestBase | ||
{ | ||
@JsonSerialize(using = CustomSer.class) | ||
public static class Data { | ||
public String aStr; | ||
} | ||
|
||
@SuppressWarnings("serial") | ||
public static class CustomSer extends StdSerializer<Data> { | ||
public CustomSer() { | ||
super(Data.class); | ||
} | ||
|
||
@Override | ||
public void serialize(Data value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
String attrStr = (String) provider.getAttribute("myAttr"); | ||
gen.writeStartObject(); | ||
gen.writeStringField("aStr", "The value is: " + (attrStr == null ? "NULL" : attrStr)); | ||
gen.writeEndObject(); | ||
} | ||
} | ||
|
||
final ObjectMapper MAPPER = newObjectMapper(); | ||
|
||
public void testPOJONodeCustomSer() throws Exception | ||
{ | ||
Data data = new Data(); | ||
data.aStr = "Hello"; | ||
|
||
Map<String, Object> mapTest = new HashMap<>(); | ||
mapTest.put("data", data); | ||
|
||
ObjectNode treeTest = MAPPER.createObjectNode(); | ||
treeTest.putPOJO("data", data); | ||
|
||
final String EXP = "{\"data\":{\"aStr\":\"The value is: Hello!\"}}"; | ||
|
||
String mapOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(mapTest); | ||
assertEquals(EXP, mapOut); | ||
|
||
String treeOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(treeTest); | ||
assertEquals(EXP, treeOut); | ||
} | ||
} |
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
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