RandomSec/main/src/com/google/refine/expr/functions/strings/ParseJson.java
David Huynh 1de5e7c00e Renamed package gel to grel.
Replaced gel with grel in other places in the code base while maintaining backward compatibility.
Changed layout in expression preview dialog to accommodate long GREL name.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1442 7d457c2a-affb-35e4-300a-418c747d4874
2010-10-07 05:19:35 +00:00

40 lines
1.2 KiB
Java

package com.google.refine.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONTokener;
import org.json.JSONWriter;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
public class ParseJson implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) {
Object o1 = args[0];
if (o1 != null) {
try {
return new JSONTokener(o1.toString()).nextValue();
} catch (JSONException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed: " + e.getMessage());
}
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Parses a string as JSON");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("JSON object");
writer.endObject();
}
}