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
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
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
-
Method Summary
Modifier and TypeMethodDescriptiondiff
(JsonTreeType source, JsonTreeType target) Creates a JSON patch of the difference between two JSON tree representationsformat
(JsonTreeType jsonTree) Formats a JSON tree representation of an object to a JSON String.Parses a JSON text into a JSON tree representation used by the underlying JSON library.patch
(JsonTreeType source, JsonTreeType jsonPatch) Updates a JSON tree representation with a JSON patchtoJsonTree
(Object pojo) Serializes a plain Java object (POJO) to a JSON tree representation used by the underlying JSON library.toObject
(JsonTreeType jsonTree, Class userType) Deserializes a JSON tree representation to a plain Java object
-
Method Details
-
toJsonTree
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
Deserializes a JSON tree representation to a plain Java object- Parameters:
jsonTree
- a JSON tree representation of the objectuserType
- the type of the object to deserialize into- Returns:
- a new object of type userType
- Throws:
Exception
- if deserialization fails
-
parse
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
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
Creates a JSON patch of the difference between two JSON tree representations- Parameters:
source
- a JSON tree representation of the source objecttarget
- 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
Updates a JSON tree representation with a JSON patch- Parameters:
source
- a JSON tree representation of the source objectjsonPatch
- a JSON tree representation of the JSON patch- Returns:
- the updated, patched object
- Throws:
Exception
- if the patch fails
-