package com.smtscript.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* 开发使用类:生成返回的json数据的类
*
* 此类主要用于生成返回前台所需要的json格式的数据,
* BPSJsonWriter的对象可以从tranArg.getOutJson()中获得。
*
*/
public class JsonWriter
{
/**
* 根是否是数组模式
*/
private boolean _isArrayMode = false;
/**
* 根的json对象
*/
private List _jsonStack = new ArrayList();
/**
* 构造函数
*
* @param isArrayMode true 根为数组模式,false 根为hash模式,
* 从tranArg.getOutJson()取到的都是hash模式
*/
public JsonWriter(boolean isArrayMode)
{
_isArrayMode = isArrayMode;
clear();
}
/**
* 将当前json数据清空
*/
public void clear()
{
_jsonStack.clear();
_jsonStack.add(0, _isArrayMode ? Json.array() : Json.object());
}
/**
* 获得最终的json数据
*
* @return json数据字符串
*/
public String getFullJson()
{
return getRootJson().toString();
}
public Json getRootJson()
{
return _jsonStack.get(_jsonStack.size() - 1);
}
/**
* 增加原始的完整json片段
*
* @param raw 要增加的json片段
*/
public void addRawString(String raw)
{
Json jsonCur = _jsonStack.get(0);
if(jsonCur.isArray())
{
addRawJson(Json.read(String.format("[%s]", raw)));
}
else
{
addRawJson(Json.read(String.format("{%s}", raw)));
}
}
public Json getCurJson()
{
return _jsonStack.get(0);
}
public void addRawJson(Json jsonObject)
{
Json jsonCur = _jsonStack.get(0);
if(jsonCur.isArray())
{
List jsonRaw = jsonObject.asJsonList();
for(int i = 0; i < jsonRaw.size(); i ++)
{
jsonCur.add(jsonRaw.get(i));
}
}
else if(jsonCur.isObject())
{
Map jsonRaw = jsonObject.asJsonMap();
for(Entry entry : jsonRaw.entrySet())
{
String key = entry.getKey();
Json value = entry.getValue();
jsonCur.set(key, value);
}
}
else
throw new RuntimeException("unknow current json type");
}
/**
* 在某个key下插入一个完整的json片段
*
* @param key key名(如果是数组模式,key=null)
* @param raw json片段
*/
public Json addKeyRaw(String key, String raw)
{
return addKeyRaw(key, Json.read(raw));
}
public Json addKeyRaw(String key, Json json)
{
return addKeyValue(key, json);
}
/**
* 在某个key下插入一个值
*
* @param key key名(如果是数组模式,key=null)
* @param value 要插入的值
*/
public Json addKeyValue(String key, Object value)
{
return addKeyValue(key, value, true);
}
/**
* 在某个key下插入一个值
*
* @param key key名(如果是数组模式,key=null)
* @param value 要插入的值
*/
protected Json addKeyValue(String key, Object value, boolean overwrite)
{
Json jsonRet = null;
Json jsonCur = _jsonStack.get(0);
if(value instanceof JsonWriter)
{
value = ((JsonWriter)value).getRootJson().dup();
}
if(jsonCur.isArray())
{
if(key != null && key.length() > 0)
throw new RuntimeException("current is array mode");
jsonCur.add(value);
jsonRet = jsonCur.at(jsonCur.asJsonList().size() - 1);
}
else if(jsonCur.isObject())
{
if(key == null)
throw new RuntimeException("current is map mode");
if(overwrite || !jsonCur.has(key))
jsonCur.set(key, value);
jsonRet = jsonCur.at(key);
}
return jsonRet;
}
/**
* 开始进行数组插入
*
* @param key key名(如果是数组模式,key=null)
*/
public void beginArray(String key)
{
Json jsonCur = addKeyValue(key, Json.array(), false);
_jsonStack.add(0, jsonCur);
}
/**
* 结束数组插入
*/
public void endArray()
{
Json json = _jsonStack.remove(0);
if(!json.isArray())
throw new RuntimeException("current is not array");
}
/**
* 开始hash插入
*
* @param key key名(如果是数组模式,key=null)
*/
public void beginMap(String key)
{
Json jsonCur = addKeyValue(key, Json.object(), false);
_jsonStack.add(0, jsonCur);
}
/**
* 结束hash插入
*/
public void endMap()
{
Json json = _jsonStack.remove(0);
if(!json.isObject())
throw new RuntimeException("current is not map");
}
/**
* 转到根路径开始操作
*/
public void gotoRoot()
{
Json jsonRoot = this.getRootJson();
_jsonStack.clear();
_jsonStack.add(jsonRoot);
}
}