Interface JsonHandler<JsonTreeType>

Type Parameters:
JsonTreeType - the type of the internal tree structure used by the underlying JSON library
All Known Implementing Classes:
GsonJsonHandler, JacksonJsonHandler

public interface JsonHandler<JsonTreeType>

Handler for parsing, patching and serializing Objects to JSON

The following example implements a JsonHandler using the Jackson JSON library

 

import com.caplin.datasource.messaging.json.JsonHandler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.diff.JsonDiff;

import java.util.logging.Level;
import java.util.logging.Logger;

public class JacksonJsonHandler implements JsonHandler {
        private Logger logger;
        private ObjectMapper objectMapper;
        
        public JacksonJsonHandler(Logger logger, ObjectMapper objectMapper) {
                this.logger = logger;
                this.objectMapper = objectMapper;
        }
        
        public JacksonJsonHandler(Logger logger) {
                this(logger, new ObjectMapper());
        }
        
        public JacksonJsonHandler() {
                this(Logger.getLogger(JacksonJsonHandler.class.getName()));
        }
        
        @Override
        public JsonNode toJsonTree(Object userObject) {
        logger.log(Level.FINER, "JacksonJsonHandler.toJsonElement {0}", userObject);
                return objectMapper.valueToTree(userObject);
        }
        
        @Override
        public Object toObject(JsonNode jsonNode, Class userType) throws Exception {
        logger.log(Level.FINER, "JacksonJsonHandler.toObject {0} {1}", new Object[] {jsonNode, userType.getName()});
                return objectMapper.treeToValue(jsonNode, userType);
        }
        
        @Override
        public JsonNode parse(String jsonText) throws Exception {
        logger.log(Level.FINER, "JacksonJsonHandler.parse {0}", jsonText);
                return objectMapper.readTree(jsonText);
        }
        
        @Override
        public String format(JsonNode jsonNode) throws Exception {
        logger.log(Level.FINER, "JacksonJsonHandler.format {0}", jsonNode);
                return objectMapper.writeValueAsString(jsonNode);
        }
        
        @Override
        public JsonNode diff(JsonNode source, JsonNode target) {
        logger.log(Level.FINER, "JacksonJsonHandler.diff {0} {1}", new Object[]{source, target});
                JsonNode patchNode = JsonDiff.asJson(source, target);
                return patchNode;
        }
        
        @Override
        public JsonNode patch(JsonNode source, JsonNode diff) throws Exception {
        logger.log(Level.FINER, "JacksonJsonHandler.patch {0} {1}", new Object[] { source, diff});
                JsonPatch patch = JsonPatch.fromJson(diff);
                JsonNode patched = patch.apply(source);
                return patched;
        }
}

The following example implements a JsonHandler using the GSON JSON library

 

import com.caplin.datasource.messaging.json.JsonHandler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.tananaev.jsonpatch.JsonPatchFactory;
import com.tananaev.jsonpatch.gson.AbsOperationDeserializer;
import com.tananaev.jsonpatch.gson.JsonPathDeserializer;
import com.tananaev.jsonpatch.gson.JsonPathSerializer;

import java.util.logging.Level;
import java.util.logging.Logger;

public class GsonJsonHandler implements JsonHandler {
        private Logger logger;
        private Gson gsonSerializer;
        private Gson gsonDeserializer;
        private JsonPatchFactory patchFactory;
        private GsonBuilder gsonBuilder;
        
        public GsonJsonHandler() {
                this(Logger.getLogger(GsonJsonHandler.class.getName()));
        }
        
        public GsonJsonHandler(Logger logger) {
                this.logger = logger;
                this.patchFactory = new JsonPatchFactory();
                
                this.gsonBuilder = new GsonBuilder();
                this.gsonBuilder.registerTypeAdapter(com.tananaev.jsonpatch.JsonPath.class, new JsonPathSerializer());
                this.gsonSerializer = gsonBuilder.create();
                
                this.gsonBuilder = new GsonBuilder();
                this.gsonBuilder.registerTypeAdapter(com.tananaev.jsonpatch.operation.AbsOperation.class, new AbsOperationDeserializer());
                this.gsonBuilder.registerTypeAdapter(com.tananaev.jsonpatch.JsonPath.class, new JsonPathDeserializer());
                this.gsonDeserializer = gsonBuilder.create();
        }
        
        @Override
        public JsonElement toJsonTree(Object userObject) {
        logger.log(Level.FINER, "GsonJsonHandler.toJsonElement {0}", userObject);
                return gsonSerializer.toJsonTree(userObject);
        }
        
        @Override
        public Object toObject(JsonElement jsonElement, Class userType) throws Exception {
        logger.log(Level.FINER, "GsonJsonHandler.toObject {0} {1}", new Object[] {jsonElement, userType.getName()});
                return gsonSerializer.fromJson((JsonElement)jsonElement, userType);
        }
        
        @Override
        public JsonElement parse(String jsonText) throws Exception {
        logger.log(Level.FINER, "GsonJsonHandler.parse {0}", jsonText);
                return JsonParser.parseString(jsonText);
        }
        
        @Override
        public String format(JsonElement jsonElement) throws Exception {
        logger.log(Level.FINER, "GsonJsonHandler.format {0}", jsonElement);
                return gsonSerializer.toJson(jsonElement);
        }
        
        @Override
        public JsonElement diff(JsonElement source, JsonElement target) {
        logger.log(Level.FINER, "GsonJsonHandler.diff {0} {1}", new Object[]{source, target});
                com.tananaev.jsonpatch.JsonPatch patchNode = patchFactory.create(source, target);
                return gsonSerializer.toJsonTree(patchNode);
        }
        
        @Override
        public JsonElement patch(JsonElement source, JsonElement diff) throws Exception {
        logger.log(Level.FINER, "GsonJsonHandler.patch {0} {1}", new Object[] { source, diff});
                com.tananaev.jsonpatch.JsonPatch patch = gsonDeserializer.fromJson(diff, com.tananaev.jsonpatch.JsonPatch.class);
                JsonElement patched = patch.apply(source);
                return patched;
        }
}

  • Method Details

    • toJsonTree

      JsonTreeType toJsonTree(Object pojo) throws Exception
      Serializes a plain Java object (POJO) to a JSON tree representation used by the underlying JSON library.
      Parameters:
      pojo - a plain Java object
      Returns:
      a JSON tree representation of the object
      Throws:
      Exception - if conversion fails
    • toObject

      Object toObject(JsonTreeType jsonTree, Class userType) throws Exception
      Deserializes a JSON tree representation to a plain Java object
      Parameters:
      jsonTree - a JSON tree representation of the object
      userType - the type of the object to deserialize into
      Returns:
      a new object of type userType
      Throws:
      Exception - if deserialization fails
    • parse

      JsonTreeType parse(String jsonText) throws Exception
      Parses a JSON text into a JSON tree representation used by the underlying JSON library.
      Parameters:
      jsonText - the JSON text to parse
      Returns:
      a JSON tree representation of the JSON text
      Throws:
      Exception - if the parsing fails
    • format

      String format(JsonTreeType jsonTree) throws Exception
      Formats a JSON tree representation of an object to a JSON String.
      Parameters:
      jsonTree - a JSON tree representation of an object
      Returns:
      the JSON String
      Throws:
      Exception - if formatting fails
    • diff

      JsonTreeType diff(JsonTreeType source, JsonTreeType target) throws Exception
      Creates a JSON patch of the difference between two JSON tree representations
      Parameters:
      source - a JSON tree representation of the source object
      target - a JSON tree representation of the target object
      Returns:
      a JSON tree representation of the JSON patch needed to patch source to create target.
      Throws:
      Exception - if the difference operation fails
    • patch

      JsonTreeType patch(JsonTreeType source, JsonTreeType jsonPatch) throws Exception
      Updates a JSON tree representation with a JSON patch
      Parameters:
      source - a JSON tree representation of the source object
      jsonPatch - a JSON tree representation of the JSON patch
      Returns:
      the updated, patched object
      Throws:
      Exception - if the patch fails