org.directwebremoting.json.JsonUtil Maven / Gradle / Ivy
package org.directwebremoting.json;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import org.directwebremoting.json.parse.JsonParseException;
import org.directwebremoting.json.parse.JsonParser;
import org.directwebremoting.json.parse.JsonParserFactory;
import org.directwebremoting.json.parse.impl.IgnoreJsonDecoder;
import org.directwebremoting.json.parse.impl.ReflectionJsonDecoder;
import org.directwebremoting.json.parse.impl.SimpleJsonDecoder;
import org.directwebremoting.json.serialize.JsonSerializer;
import org.directwebremoting.json.serialize.JsonSerializerFactory;
/**
* Various utilities to make parsing and reading JSON easier
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class JsonUtil
{
/**
* Get a record of any errors in parsing the input string
* @param input ...
* @return ...
*/
public static String getErrors(String input)
{
return getErrors(new StringReader(input));
}
/**
* Get a record of any errors in parsing the input document
* @param reader ...
* @return ...
*/
public static String getErrors(Reader reader)
{
try
{
JsonParser parser = JsonParserFactory.get();
parser.parse(reader, new IgnoreJsonDecoder());
return null;
}
catch (JsonParseException ex)
{
return ex.toString();
}
}
/**
* Convert the input string into a set of basic types
* @param input ...
* @return ...
* @throws JsonParseException ...
*/
public static Map toSimpleObject(String input) throws JsonParseException
{
return toSimpleObject(new StringReader(input));
}
/**
* Convert the input document into a set of basic types
* @param reader ...
* @return ...
* @throws JsonParseException ...
*/
@SuppressWarnings("unchecked")
public static Map toSimpleObject(Reader reader) throws JsonParseException
{
JsonParser parser = JsonParserFactory.get();
return (Map) parser.parse(reader, new SimpleJsonDecoder());
}
/**
* Convert the input string into a set of basic types
* @param input ...
* @return ...
* @throws JsonParseException ...
*/
public static List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy