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

com.github.yydf.struts.mapper.JSONMap Maven / Gradle / Ivy

The newest version!
package com.github.yydf.struts.mapper;

import java.util.HashMap;
import java.util.Map;

import com.github.yydf.struts.util.Assert;
import com.github.yydf.struts.wrapper.JSONWrapper;

/**
 * 自定义map,用来存储转换前的json数据
 * 
 * @author YYDF 2018-12-07
 * @since 1.7
 */
public class JSONMap {

	private final HashMap jsonMap = new HashMap<>();
	private final JSONWrapper wrapper = new JSONWrapper();

	/**
	 * 默认成功返回
	 * 
	 * @return jsonMap
	 */
	public static JSONMap success() {
		return new JSONMap().put("success", true).put("errcode", 0).put("errmsg", "ok");
	}

	/**
	 * 默认失败返回
	 * 
	 * @param errcode
	 *            错误码
	 * @param errmsg
	 *            错误描述
	 * @return jsonMap
	 */
	public static JSONMap error(int errcode, String errmsg) {
		return new JSONMap().put("success", false).put("errcode", errcode).put("errmsg", errmsg);
	}

	/**
	 * 添加单个对象
	 * 
	 * @param key
	 *            键
	 * @param object
	 *            值
	 * @return 当前jsonMap
	 */
	public JSONMap put(String key, Object object) {
		Assert.notNull(key);
		jsonMap.put(key, object);
		return this;
	}

	/**
	 * 添加map对象
	 * 
	 * @param data
	 *            集合
	 * @return 当前jsonMap
	 */
	public JSONMap putAll(Map data) {
		Assert.notNull(data);
		jsonMap.putAll(data);
		return this;
	}

	@Override
	public String toString() {
		return wrapper.write(jsonMap);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy