Last active
July 15, 2020 02:41
-
-
Save jordanluyke/642ce3272203b00392201b3cfd45ca72 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.jordanluyke.example.util; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.io.IOException; | |
import java.lang.reflect.Field; | |
import java.util.Optional; | |
/** | |
* @author Jordan Luyke <[email protected]> | |
*/ | |
public class NodeUtil { | |
private static final Logger logger = LogManager.getLogger(NodeUtil.class); | |
public static ObjectMapper mapper = new ObjectMapper() | |
.registerModule(new Jdk8Module()) | |
.registerModule(new JavaTimeModule()) | |
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false) | |
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
public static boolean isValidJSON(byte[] json) { | |
try { | |
return !mapper.readTree(json).isNull(); | |
} catch(IOException e) { | |
return false; | |
} | |
} | |
public static JsonNode getJsonNode(byte[] json) { | |
try { | |
return mapper.readTree(json); | |
} catch(IOException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
public static byte[] writeValueAsBytes(JsonNode node) { | |
try { | |
return mapper.writeValueAsBytes(node); | |
} catch(JsonProcessingException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
public static String writeValueAsString(JsonNode node) { | |
try { | |
return mapper.writeValueAsString(node); | |
} catch (JsonProcessingException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
public static <T> T parseNodeInto(Class<T> clazz, JsonNode body) { | |
try { | |
return mapper.treeToValue(body, clazz); | |
} catch(JsonProcessingException e) { | |
logger.error("Json serialize fail: {} {}", clazz.getSimpleName(), writeValueAsString(body)); | |
for(Field field : clazz.getFields()) { | |
field.setAccessible(true); | |
String name = field.getName(); | |
if(body.get(name) == null) | |
throw new RuntimeException("Field not found: " + name); | |
} | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
public static Optional<String> get(String field, JsonNode node) { | |
JsonNode fieldNode = node.get(field); | |
if(fieldNode == null || fieldNode.isNull()) | |
return Optional.empty(); | |
return Optional.of(fieldNode.asText()); | |
} | |
public static Optional<Boolean> getBoolean(String field, JsonNode node) { | |
return get(field, node).map(Boolean::valueOf); | |
} | |
public static Optional<Integer> getInteger(String field, JsonNode node) { | |
return get(field, node).map(Integer::parseInt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment