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

com.ajaxjs.keyvalue.MappingHelper Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package com.ajaxjs.keyvalue;

import java.util.List;
import java.util.Map;

import com.ajaxjs.js.JsonHelper;

/**
 * 为 key-value 结构的数据提供数据转换的中间件
 * 
 * @author Frank Cheung
 *
 */
public class MappingHelper {

	private Map map;

	private Object bean;

	private String json;

	private boolean addJsonPerfix;

	public MappingHelper(Map map) {
		this.map = map;
	}

	public MappingHelper(Object bean) {
		this.bean = bean;
	}

	public MappingHelper(String json) {
		this.json = json;
	}

	public MappingHelper() {
	}

	/**
	 * 添加控制器能识别 “json::”
	 * 
	 * @return 当前实例以便链式调用
	 */
	public MappingHelper addJsonPerfix() {
		addJsonPerfix = true;
		return this;
	}

	/**
	 * 
	 * @param obj
	 * @return 当前实例以便链式调用
	 */
	public MappingHelper setObject(Object obj) {
		this.bean = obj;
		return this;
	}

	/**
	 * 转换为 JSON 字符串输出
	 * 
	 * @return JSON 字符串
	 */
	public String toJson() {
		if (map != null) {
			json = MappingJson.stringifyMap(map);
		} else if (bean != null) {
			json = BeanUtil.beanToJson(bean);
		} else if (bean != null) {
			json = MappingJson.stringifySimpleObject(bean);
		}

		if (json != null && addJsonPerfix)
			return "json::" + json;

		return json;
	}

	public Map toMap() {
		if (json != null) {
			return JsonHelper.parseMap(json);
		}

		return null;
	}

	/**
	 * 操作成功,返回 msg 信息
	 */
	public static final String json_ok = "json::{\"isOk\": true, \"msg\" : \"%s\"}";

	/**
	 * 操作成功,返回 msg 信息,可扩展字段的
	 */
	public static final String json_ok_extension = "json::{\"isOk\": true, \"msg\" : \"%s\", %s}";

	/**
	 * 操作失败,返回 msg 信息
	 */
	public static final String json_not_ok = "json::{\"isOk\": false, \"msg\" : \"%s\"}";

	/**
	 * 输出 JSON OK
	 * 
	 * @param msg
	 * @return
	 */
	public static String jsonOk(String msg) {
		return String.format(json_ok, msg);
	}

	/**
	 * 输出 JSON No OK
	 * 
	 * @param msg
	 * @return
	 */
	public static String jsonNoOk(String msg) {
		return String.format(json_not_ok, msg);
	}

	/**
	 * 把 Bean 转换为 JSON
	 * 
	 * @param bean bean
	 * @return JSON 结果
	 */
	public static String outputBeanAsJson(Object bean) {
		if (bean != null)
			return "json::{\"result\":" + BeanUtil.beanToJson(bean) + "}";
		else
			return "json::{\"result\": null}";
	}

	/**
	 * 把 Map 转换为 JSON 数组
	 * 
	 * @param result Map
	 * @return JSON 结果
	 */
	public static String outputMapAsJson(Map result) {
		if (result != null)
			return "json::{\"result\":" + MappingJson.stringifyMap(result) + "}";
		else
			return "json::{\"result\": null}";
	}

	/**
	 * 把 Map 集合转换为 JSON 数组
	 * 
	 * @param result Map 集合
	 * @return JSON 结果
	 */
	public static String outputListMapAsJson(List> result) {
		if (result != null && result.size() > 0)
			return "json::{\"result\":" + MappingJson.stringifyListMap(result) + "}";
		else
			return "json::{\"result\": null}";
	}

	/**
	 * 把 Bean 集合转换为 JSON 数组
	 * 
	 * @param result BaseMolde 集合
	 * @return JSON 结果
	 */
	public static String outputListBeanAsJson(List result) {
		if (result != null && result.size() > 0) {
			String[] str = new String[result.size()];

			for (int i = 0; i < result.size(); i++)
				str[i] = BeanUtil.beanToJson(result.get(i));

			return "json::{\"result\":[" + String.join(",", str) + "]}";
		} else
			return "json::{\"result\": null}";
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Object getBean() {
		return bean;
	}

	public void setBean(Object bean) {
		this.bean = bean;
	}

	public Object getObj() {
		return bean;
	}

	public void setObj(Object obj) {
		this.bean = obj;
	}

	public String getJson() {
		return json;
	}

	public MappingHelper setJson(String json) {
		this.json = json;
		return this;
	}

	@SuppressWarnings("unchecked")
	public static  T getFromJson(String json, String key, Class clz) {
		Map map = new MappingHelper().setJson(json).toMap();

		if (map != null && map.get(key) != null) {
			return (T) map.get(key);
		}

		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy