com.venky.swf.integration.JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swf-db-io-json Show documentation
Show all versions of swf-db-io-json Show documentation
Succinct Web Framework JSON plugin
package com.venky.swf.integration;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import com.venky.core.string.StringUtil;
public class JSON extends FormatHelper{
private JSONObject root = null;
public JSON(InputStream in) {
try {
JSONObject input = (JSONObject)JSONValue.parseWithException(new InputStreamReader(in));
this.root = input;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public JSON(String name,boolean isPlural){
this.root = new JSONObject();
if (isPlural){
root.put(name, new JSONArray());
}else {
root.put(name, new JSONObject());
}
}
public JSON(JSONObject obj){
this.root = obj;
}
public JSONObject getRoot(){
return root;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject createChildElement(String name){
String pluralName = StringUtil.pluralize(name);
JSONArray children = (JSONArray)root.get(pluralName);
if (children == null){
children = new JSONArray();
root.put(pluralName, children);
}
JSONObject child = new JSONObject();
children.add(child);
return child;
}
@SuppressWarnings("unchecked")
@Override
public List getChildElements(String name){
String pluralName = StringUtil.pluralize(name);
JSONArray children = (JSONArray)root.get(pluralName);
if (children == null){
children = new JSONArray();
root.put(pluralName, children);
}
List ret = new ArrayList();
for (Object o : children){
if (o instanceof JSONObject){
ret.add((JSONObject)o);
}
}
return ret;
}
@SuppressWarnings("unchecked")
@Override
public void setAttribute(String name , String obj){
if (obj != null){
root.put(name,obj);
}
}
@SuppressWarnings("unchecked")
@Override
public JSONObject createElementAttribute(String name) {
JSONObject attr = getElementAttribute(name);
if (attr == null){
attr = new JSONObject();
root.put(name, attr);
}
return attr;
}
@Override
public JSONObject getElementAttribute(String name) {
JSONObject attr = (JSONObject)root.get(name);
return attr;
}
public String toString(){
return root.toString();
}
@Override
public Set getAttributes() {
Set attr = extractAttributes(root);
return attr;
}
private Set extractAttributes(JSONObject obj){
Set attr = new HashSet();
for (Object key : obj.keySet()){
Object value = obj.get(key);
if (value instanceof JSONObject){
continue;
}else if (value instanceof JSONArray){
continue;
}
attr.add(StringUtil.valueOf(key));
}
return attr;
}
@Override
public String getAttribute(String name) {
Object attr = root.get(name);
if (attr == null){
return null;
}else if (attr instanceof JSONObject){
return null;
}else if (attr instanceof JSONArray){
return null;
}else {
return StringUtil.valueOf(attr);
}
}
}