-
-
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
85f384a
commit 8ec1e5e
Showing
16 changed files
with
116 additions
and
95 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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/fasterxml/jackson/databind/node/InternalNodeMapper.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,37 @@ | ||
package com.fasterxml.jackson.databind.node; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
/** | ||
* Helper class used to implement <code>toString()</code> method for | ||
* {@link BaseJsonNode}, by embedding a private instance of | ||
* {@link JsonMapper}, only to be used for node serialization. | ||
* | ||
* @since 2.10 (but not to be included in 3.0) | ||
*/ | ||
final class InternalNodeMapper { | ||
private final static JsonMapper JSON_MAPPER = new JsonMapper(); | ||
private final static ObjectWriter STD_WRITER = JSON_MAPPER.writer(); | ||
private final static ObjectWriter PRETTY_WRITER = JSON_MAPPER.writer() | ||
.withDefaultPrettyPrinter(); | ||
|
||
public static String nodeToString(JsonNode n) { | ||
try { | ||
return STD_WRITER.writeValueAsString(n); | ||
} catch (IOException e) { // should never occur | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static String nodeToPrettyString(JsonNode n) { | ||
try { | ||
return PRETTY_WRITER.writeValueAsString(n); | ||
} catch (IOException e) { // should never occur | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
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
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
37 changes: 37 additions & 0 deletions
37
src/test/java/com/fasterxml/jackson/databind/node/ToStringForNodesTest.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,37 @@ | ||
package com.fasterxml.jackson.databind.node; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class ToStringForNodesTest extends BaseMapTest | ||
{ | ||
private final ObjectMapper MAPPER = objectMapper(); | ||
|
||
public void testObjectNode() throws Exception | ||
{ | ||
_verifyToStrings(MAPPER.readTree("{ \"key\" : 1, \"b\" : \"x\", \"array\" : [ 1, false ] }")); | ||
final ObjectNode n = MAPPER.createObjectNode().put("msg", "hello world"); | ||
assertEquals("{\"msg\":\"hello world\"}", n.toString()); | ||
assertEquals("{\n \"msg\" : \"hello world\"\n}", n.toPrettyString()); | ||
} | ||
|
||
public void testArrayNode() throws Exception | ||
{ | ||
_verifyToStrings(MAPPER.readTree("[ 1, true, null, [ \"abc\",3], { } ]")); | ||
final ArrayNode n = MAPPER.createArrayNode().add(0.25).add(true); | ||
assertEquals("[0.25,true]", n.toString()); | ||
assertEquals("[ 0.25, true ]", n.toPrettyString()); | ||
} | ||
|
||
public void testBinaryNode() throws Exception | ||
{ | ||
_verifyToStrings(MAPPER.getNodeFactory().binaryNode(new byte[] { 1, 2, 3, 4, 6 })); | ||
} | ||
|
||
protected void _verifyToStrings(JsonNode node) throws Exception | ||
{ | ||
assertEquals(MAPPER.writeValueAsString(node), node.toString()); | ||
|
||
assertEquals(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(node), | ||
node.toPrettyString()); | ||
} | ||
} |