Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a wrapper for readTree and treeToValue
- Loading branch information
1 parent
8d95b96
commit 4c18c64
Showing
8 changed files
with
142 additions
and
49 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
rskj-core/src/main/java/co/rsk/util/JacksonParserUtil.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 co.rsk.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.TreeNode; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public final class JacksonParserUtil { | ||
private JacksonParserUtil() { | ||
} | ||
|
||
public static <T> T treeToValue(ObjectMapper mapper, TreeNode treeNode, Class<T> aClass) throws IllegalArgumentException, JsonProcessingException { | ||
T result = mapper.treeToValue(treeNode, aClass); | ||
|
||
if (result == null) { | ||
throw new NullPointerException("Input is null"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static JsonNode readTree(ObjectMapper mapper, InputStream is) throws IOException { | ||
JsonNode result = mapper.readTree(is); | ||
|
||
if (result.isEmpty()) { | ||
throw JsonMappingException.from(mapper.getDeserializationContext(), "Input is empty"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static JsonNode readTree(ObjectMapper mapper, String content) throws IOException { | ||
JsonNode result = mapper.readTree(content); | ||
|
||
if (result.isEmpty()) { | ||
throw JsonMappingException.from(mapper.getDeserializationContext(), "Input is empty"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static JsonNode readTree(ObjectMapper mapper, byte[] content) throws IOException { | ||
JsonNode result = mapper.readTree(content); | ||
|
||
if (result.isEmpty()) { | ||
throw JsonMappingException.from(mapper.getDeserializationContext(), "Input is empty"); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
rskj-core/src/test/java/co/rsk/util/JacksonParserUtilTest.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,65 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2017 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.util; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class JacksonParserUtilTest { | ||
|
||
|
||
@Test | ||
public void test_treeToValue() throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode jsonNode = JacksonParserUtil.readTree(mapper, "{\"prop\": \"value\"}"); | ||
Map jsonMap = JacksonParserUtil.treeToValue(new ObjectMapper(), jsonNode, Map.class); | ||
|
||
Assert.assertEquals("value", jsonNode.get("prop").asText()); | ||
} | ||
|
||
@Test | ||
public void test_readTreeStringContent() throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode jsonNode = JacksonParserUtil.readTree(mapper, "{\"prop\": \"value\"}"); | ||
|
||
Assert.assertEquals("value", jsonNode.get("prop").asText()); | ||
} | ||
|
||
@Test | ||
public void test_readTreeBytesContent() throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode jsonNode = JacksonParserUtil.readTree(mapper, "{\"prop\": \"value\"}".getBytes()); | ||
|
||
Assert.assertEquals("value", jsonNode.get("prop").asText()); | ||
} | ||
|
||
@Test | ||
public void test_readTreeInputStreamContent() throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonNode jsonNode = JacksonParserUtil.readTree(mapper, new ByteArrayInputStream("{\"prop\": \"value\"}".getBytes())); | ||
|
||
Assert.assertEquals("value", jsonNode.get("prop").asText()); | ||
} | ||
} |