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

com.github.pkuliuqiang.EntityTransformFacade Maven / Gradle / Ivy

The newest version!
package com.github.pkuliuqiang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pkuliuqiang.parser.MyTypeUtils;
import com.github.pkuliuqiang.iterator.JSONIterator;
import com.github.pkuliuqiang.node.CompressedNode;
import com.github.pkuliuqiang.node.PatternNode;
import com.github.pkuliuqiang.parser.MyParserConfig;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.List;

/*
 * 实体转换工具类
 * @author liuqiangm
 */
public class EntityTransformFacade {

  /*
   * 将jsonObject转换为clazz类型的对象
   * @param jsonObject
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromJSONObject(JSONObject jsonObject, Class clazz) {
    return MyTypeUtils.cast(jsonObject, clazz, MyParserConfig.getGlobalInstance());
  }

  /*
   * 从源json对象中提取出json数组,并转换为目标对象列表
   * @param sourceJSONObject 源json对象
   * @param patternJSONObject 模式json
   * @param clazz 目标对象类型
   * @param 
   * @return
   */
  public static  List getTargetObjectListFromSourceJSONObject(JSONObject sourceJSONObject,
      JSONObject patternJSONObject,
      Class clazz) {
    PatternNode patternNode = PatternNode.getPatternNodeFromJSONObject(patternJSONObject);
    JSONIterator JSONIterator = new JSONIterator();
    JSONIterator.setPatternNode(patternNode);
    JSONIterator.iterator(sourceJSONObject, "r");
    CompressedNode compressedNode = JSONIterator.getCompressedNode();
    return compressedNode.getTargetJSONObject().getJSONArray("r").toJavaList(clazz);
  }

  /*
   * 从源json数组转换为目标对象数组,并进而转换为目标对象列表
   * @param sourceJSONArray 源json数组
   * @param patternJSONObject 模式json
   * @param clazz 目标对象类型
   * @param 
   * @return
   */
  public static  List getTargetObjectListFromSourceJSONArray(JSONArray sourceJSONArray,
      JSONObject patternJSONObject,
      Class clazz) {
    PatternNode patternNode = PatternNode.getPatternNodeFromJSONObject(patternJSONObject);
    JSONIterator JSONIterator = new JSONIterator();
    JSONIterator.setPatternNode(patternNode);
    JSONIterator.iterator(sourceJSONArray, "r.[");
    CompressedNode compressedNode = JSONIterator.getCompressedNode();
    JSONObject targetObject = compressedNode.getTargetJSONObject();
    return targetObject.getJSONArray("r").toJavaList(clazz);
  }

  /*
   * 从源json输入流中获取json并转换为目标对象
   * @param sourceInputStream 源json输入流
   * @param patternJSONObject 模式对象
   * @param clazz 目标对象类型
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceJSONInputStream(InputStream sourceInputStream,
      JSONObject patternJSONObject,
      Class clazz) {
    try {
      JSONObject sourceSubJSONObject = new JSONObject(
          JSONObject.parseObject(sourceInputStream, LinkedHashMap.class));
      return getTargetObjectFromSourceObject(sourceSubJSONObject, patternJSONObject, clazz);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /*
   * 从源json输入流和模式输入流中获取信息,转换为目标对象
   * @param sourceInputStream
   * @param patternInputStream
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceJSONInputStream(InputStream sourceInputStream,
      InputStream patternInputStream,
      Class clazz) {
    try {
      JSONObject patternJSONObject = new JSONObject(
          JSONObject.parseObject(patternInputStream, LinkedHashMap.class));
      return getTargetObjectFromSourceJSONInputStream(sourceInputStream, patternJSONObject, clazz);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /*
   * 从源json字符串和模式json转换为目标对象
   * @param sourceJSONStr
   * @param patternJSONObject
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceJSONStr(String sourceJSONStr,
      JSONObject patternJSONObject,
      Class clazz) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        sourceJSONStr.getBytes(Charset.forName("UTF-8")));
    return getTargetObjectFromSourceJSONInputStream(byteArrayInputStream, patternJSONObject, clazz);
  }

  /*
   * 将源对象根据pattern数组转换为目标对象
   * @param sourceObj
   * @param patternJSONObject
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceObject(Object sourceObj,
      JSONObject patternJSONObject,
      Class clazz) {
    JSONObject sourceJSONObject = (JSONObject) JSON.toJSON(sourceObj);
    JSONObject targetJSONObject = JSONTransformFacade
        .getJSONObjectFromSourceAndPattern(sourceJSONObject,
            patternJSONObject);
    return MyTypeUtils.cast(targetJSONObject, clazz, MyParserConfig.getGlobalInstance());
  }

  /*
   * 将源对象根据pattern输入流转换为目标对象
   * @param sourceObj
   * @param patternInputStream
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceObject(Object sourceObj,
      InputStream patternInputStream,
      Class clazz) {
    try {
      return getTargetObjectFromSourceObject(sourceObj,
          (JSONObject) JSONObject.parseObject(patternInputStream, JSONObject.class), clazz);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /*
   * 将源对象根据模式字符串转换为目标对象
   * @param sourceObj
   * @param patternStr
   * @param clazz
   * @param 
   * @return
   */
  public static  T getTargetObjectFromSourceObject(Object sourceObj, String patternStr,
      Class clazz) {
    return getTargetObjectFromSourceObject(sourceObj, JSONObject.parseObject(patternStr), clazz);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy