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

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

There is a newer version: 1.1.0
Show 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

* @author Cyan ([email protected]) * @version v1.0.0 */ public class DeserializeHelper { 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()); } } 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()); } } 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()); } } 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()); } } 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()); } } 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); } } } }