Java tutorial
/* * Copyright (c) 2014 HollowSoft @IgorMorais * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.hollowsoft.library.utility.utility; import java.io.IOException; import java.io.InputStream; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * @author Igor Morais * @author mor41s.1gor@gmail.com */ public final class JsonParser { /** * Default private constructor. */ private JsonParser() { } /** * * @param entity * @return * @throws JsonProcessingException */ public static <T> String toJson(final T entity) throws JsonProcessingException { if (entity == null) { throw new IllegalArgumentException("The entity cannot be null."); } return new ObjectMapper().writer().writeValueAsString(entity); } /** * * @param json * @return * @throws JsonProcessingException * @throws IOException */ public static <T> JsonNode toJsonNode(final String json) throws JsonProcessingException, IOException { if (json == null) { throw new IllegalArgumentException("The json cannot be null."); } return new ObjectMapper().reader().readTree(json); } /** * * @param inputStream * @return * @throws JsonProcessingException * @throws IOException */ public static <T> JsonNode toJsonNode(final InputStream inputStream) throws JsonProcessingException, IOException { if (inputStream == null) { throw new IllegalArgumentException("The inputStream cannot be null."); } return new ObjectMapper().reader().readTree(inputStream); } /** * * @param entity * @return */ public static <T> JsonNode toJsonNode(final T entity) { if (entity == null) { throw new IllegalArgumentException("The entity cannot be null."); } return new ObjectMapper().valueToTree(entity); } /** * * @param json * @return * @throws JsonProcessingException * @throws IOException */ public static <T> T toObject(final String json) throws JsonProcessingException, IOException { if (json == null) { throw new IllegalArgumentException("The inputStream cannot be null."); } return new ObjectMapper().reader().readValue(json); } /** * * @param inputStream * @return * @throws JsonProcessingException * @throws IOException */ public static <T> T toObject(final InputStream inputStream) throws JsonProcessingException, IOException { if (inputStream == null) { throw new IllegalArgumentException("The inputStream cannot be null."); } return new ObjectMapper().reader().readValue(inputStream); } /** * * @param jsonNode * @return * @throws JsonProcessingException * @throws IOException */ public static <T> T toObject(final JsonNode jsonNode) throws JsonProcessingException, IOException { if (jsonNode == null) { throw new IllegalArgumentException("The jsonNode cannot be null."); } return new ObjectMapper().reader().readValue(jsonNode); } /** * * @param jsonParser * @return * @throws JsonProcessingException * @throws IOException */ public static <T> T toObject(final com.fasterxml.jackson.core.JsonParser jsonParser) throws JsonProcessingException, IOException { if (jsonParser == null) { throw new IllegalArgumentException("The jsonParser cannot be null."); } return new ObjectMapper().reader().readValue(jsonParser); } }