All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.helper.DeserializeHelper Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.helper;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.nichetoolkit.rest.error.supply.JsonDeserializeException;

import java.io.IOException;
import java.util.*;

/**
 * DeserializeHelper
 * 

The deserialize helper class.

* @author Cyan ([email protected]) * @since Jdk1.8 */ public class DeserializeHelper { /** * deserializerBean *

The deserializer bean method.

* @param parser {@link com.fasterxml.jackson.core.JsonParser}

The parser parameter is JsonParser type.

* @return {@link java.util.Map}

The deserializer bean return object is Map type.

* @throws JsonDeserializeException {@link io.github.nichetoolkit.rest.error.supply.JsonDeserializeException}

The json deserialize exception is JsonDeserializeException type.

* @see com.fasterxml.jackson.core.JsonParser * @see java.util.Map * @see io.github.nichetoolkit.rest.error.supply.JsonDeserializeException */ public static Map deserializerBean(JsonParser parser) throws JsonDeserializeException { try { JsonNode jsonNode = parser.getCodec().readTree(parser); Map beanMap = new HashMap<>(); if (!jsonNode.isArray()) { buildBeanMap(jsonNode, beanMap); } return beanMap; } catch (IOException exception) { throw new JsonDeserializeException(exception.getMessage()); } } /** * deserializerList *

The deserializer list method.

* @param parser {@link com.fasterxml.jackson.core.JsonParser}

The parser parameter is JsonParser type.

* @return {@link java.util.List}

The deserializer list return object is List type.

* @throws JsonDeserializeException {@link io.github.nichetoolkit.rest.error.supply.JsonDeserializeException}

The json deserialize exception is JsonDeserializeException type.

* @see com.fasterxml.jackson.core.JsonParser * @see java.util.List * @see io.github.nichetoolkit.rest.error.supply.JsonDeserializeException */ public static List deserializerList(JsonParser parser) throws JsonDeserializeException { try { List resultList = new ArrayList<>(); JsonNode jsonNode = parser.getCodec().readTree(parser); if (jsonNode.isArray()) { for (JsonNode objectNode : jsonNode) { resultList.add(objectNode); } } return resultList; } catch (IOException exception) { throw new JsonDeserializeException(exception.getMessage()); } } /** * deserializerBeanList *

The deserializer bean list method.

* @param parser {@link com.fasterxml.jackson.core.JsonParser}

The parser parameter is JsonParser type.

* @return {@link java.util.List}

The deserializer bean list return object is List type.

* @throws JsonDeserializeException {@link io.github.nichetoolkit.rest.error.supply.JsonDeserializeException}

The json deserialize exception is JsonDeserializeException type.

* @see com.fasterxml.jackson.core.JsonParser * @see java.util.List * @see io.github.nichetoolkit.rest.error.supply.JsonDeserializeException */ public static List> deserializerBeanList(JsonParser parser) throws JsonDeserializeException { try { List> resultList = new ArrayList<>(); JsonNode jsonNode = parser.getCodec().readTree(parser); if (jsonNode.isArray()) { for (JsonNode objectNode : jsonNode) { Map beanMap = new HashMap<>(); buildBeanMap(objectNode, beanMap); resultList.add(beanMap); } } return resultList; } catch (IOException exception) { throw new JsonDeserializeException(exception.getMessage()); } } /** * deserializerMap *

The deserializer map method.

* @param parser {@link com.fasterxml.jackson.core.JsonParser}

The parser parameter is JsonParser type.

* @return {@link java.util.Map}

The deserializer map return object is Map type.

* @throws JsonDeserializeException {@link io.github.nichetoolkit.rest.error.supply.JsonDeserializeException}

The json deserialize exception is JsonDeserializeException type.

* @see com.fasterxml.jackson.core.JsonParser * @see java.util.Map * @see io.github.nichetoolkit.rest.error.supply.JsonDeserializeException */ public static Map deserializerMap(JsonParser parser) throws JsonDeserializeException { try { JsonNode jsonNode = parser.getCodec().readTree(parser); Map resultMap = new HashMap<>(); if (!jsonNode.isArray()) { for (Iterator> iter = jsonNode.fields(); iter.hasNext(); ) { Map.Entry entry = iter.next(); JsonNode valueNode = entry.getValue(); resultMap.put(entry.getKey(), valueNode); } } return resultMap; } catch (IOException exception) { throw new JsonDeserializeException(exception.getMessage()); } } /** * deserializerBeanMap *

The deserializer bean map method.

* @param parser {@link com.fasterxml.jackson.core.JsonParser}

The parser parameter is JsonParser type.

* @return {@link java.util.Map}

The deserializer bean map return object is Map type.

* @throws JsonDeserializeException {@link io.github.nichetoolkit.rest.error.supply.JsonDeserializeException}

The json deserialize exception is JsonDeserializeException type.

* @see com.fasterxml.jackson.core.JsonParser * @see java.util.Map * @see io.github.nichetoolkit.rest.error.supply.JsonDeserializeException */ public static Map> deserializerBeanMap(JsonParser parser) throws JsonDeserializeException { try { JsonNode jsonNode = parser.getCodec().readTree(parser); Map> dataMap = new HashMap<>(); if (!jsonNode.isArray()) { for (Iterator> iter = jsonNode.fields(); iter.hasNext(); ) { Map.Entry entry = iter.next(); Map beanMap = new HashMap<>(); JsonNode valueNode = entry.getValue(); buildBeanMap(valueNode, beanMap); dataMap.put(entry.getKey(), beanMap); } } return dataMap; } catch (IOException exception) { throw new JsonDeserializeException(exception.getMessage()); } } /** * buildBeanMap *

The build bean map method.

* @param jsonNode {@link com.fasterxml.jackson.databind.JsonNode}

The json node parameter is JsonNode type.

* @param beanMap {@link java.util.Map}

The bean map parameter is Map type.

* @see com.fasterxml.jackson.databind.JsonNode * @see java.util.Map */ public static void buildBeanMap(JsonNode jsonNode, Map beanMap) { for (Iterator> iter = jsonNode.fields(); iter.hasNext(); ) { Map.Entry entry = iter.next(); JsonNode valueNode = entry.getValue(); if (valueNode.isNull()) { continue; } if (valueNode.isTextual()) { beanMap.put(entry.getKey(), valueNode.asText()); } else if (valueNode.isFloat() || valueNode.isDouble()) { beanMap.put(entry.getKey(), valueNode.asDouble()); } else if (valueNode.isInt()) { beanMap.put(entry.getKey(), valueNode.asInt()); } else if (valueNode.isLong()) { beanMap.put(entry.getKey(), valueNode.asLong()); } else if (valueNode.isBoolean()) { beanMap.put(entry.getKey(), valueNode.asBoolean()); } else { beanMap.put(entry.getKey(), valueNode); } } } }