...
Code Block | ||
---|---|---|
| ||
public class GsonToJackson { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); String response = "{\"request\": { \"firstName\": \"Sso mock user\",\"phone\": \"9663933554\"," + " \"phoneVerified\": true,\"channel\": \"INFRA\", \"orgExternalId\": \"intra111\"," + "\"externalIds\":" + " [{ \"id\": \"sso_mock_user\", \"provider\": \"INFRA\",\"idType\": \"INFRA\" }] }}"; try { JsonNode res = mapper.readTree(response); JsonNode request = res.get("request"); if (request != null) { JsonNode externalIds = request.get("externalIds"); System.out.println("Using JackSon : value of externalIds " + mapper.writeValueAsString(externalIds)); JsonNode externalId = externalIds.get(0); System.out.println("Using JackSon : value of externalId " + mapper.writeValueAsString(externalId)); System.out.println("Using JackSon : value of provider in externalId is : " + externalId.get("provider").asText()); } } catch (Exception e) { System.out.println("Exception caught"); } JsonParser parser = new JsonParser(); JsonObject json = (JsonObject) parser.parse(response); Object request = json.get("request"); if (request != null) { JsonObject value = (JsonObject) parser.parse(request.toString()); JsonArray externalIds = (JsonArray) value.get("externalIds"); System.out.println("Using Gson : value of externalIds " + externalIds.toString()); JsonObject externalId = (JsonObject) externalIds.get(0); System.out.println("Using Gson : value of externalId " + externalId.toString()); System.out.println("Using Gson : value of provider in externalId is : " + externalId.get("provider").getAsString()); } } } |
...
Code Block |
---|
Using JackSon : value of externalIds [{"id":"sso_mock_user","provider":"INFRA","idType":"INFRA"}] Using JackSon : value of externalId {"id":"sso_mock_user","provider":"INFRA","idType":"INFRA"} Using JackSon : value of provider in externalId is : INFRA Using Gson : value of externalIds [{"id":"sso_mock_user","provider":"INFRA","idType":"INFRA"}] Using Gson : value of externalId {"id":"sso_mock_user","provider":"INFRA","idType":"INFRA"} Using Gson : value of provider in externalId is : INFRA |
Snippet 2 :
Code Block | ||
---|---|---|
| ||
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.gson.JsonObject; public class GsonToJackson_2 { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); JsonObject gson = new JsonObject(); gson.addProperty("revocation_reason", "REVOCATION_REASON"); gson.addProperty("revocation", false); gson.addProperty("reason", "REASON"); System.out.println("Using Gson : Object to Json String conversion :"+ gson.toString()); JsonNode jackson = mapper.createObjectNode(); ((ObjectNode) jackson).put("revocation_reason", "REVOCATION_REASON"); ((ObjectNode) jackson).put("revocation", false); ((ObjectNode) jackson).put("reason", "REASON"); String jjsonString = mapper.writeValueAsString(jackson); System.out.println("Using Jackson : Object to Json String conversion : "+ jjsonString); } } |
...