net.arnx.jsonic
クラス JSON

java.lang.Object
  上位を拡張 net.arnx.jsonic.JSON

public class JSON
extends Object

The JSONIC JSON class provides JSON encoding and decoding as defined by RFC 4627.

The following example illustrates how to encode and decode. The code:

 // encodes a object into a json string.
 String s = JSON.encode(o);
 
 // decodes a json string into a object.
 Object o = JSON.decode(s);
 
 // decodes a json string into a typed object.
 Foo foo = JSON.decode(s, Foo.class);
 

Advanced topic:

 // formats a object into a json string with indents for debug.
 JSON json = new JSON();
 json.setPrettyPrint(true);
 String pretty = json.format(o);
 
 //uses Reader/InputStream
 Bar bar = (new JSON()).parse(new FileInputStream("bar.json"), Bar.class);
 Bar bar = (new JSON()).parse(new FileReader("bar.json"), Bar.class);
 

Summary of encoding rules for java type into json type

java type json type
java.util.Mapobject
java.lang.Object (public property or field)
java.lang.Object[]array
java.util.Collection
boolean[], short[], int[], long[], float[], double[]
java.lang.CharSequencestring
char[]
java.lang.Character
char
java.util.TimeZone
java.util.regex.Pattern
java.lang.reflect.Type
java.lang.reflect.Member
byte[]string (base64)
java.util.Localestring (language-country)
java.lang.Numbernumber
byte, short, int, long, float, double
java.util.Datenumber (milliseconds since 1970)
java.util.Calendar
java.lang.Booleantrue/false
boolean
nullnull

Summary of decoding rules for json type into java type

json type java type
objectjava.util.HashMap
arrayjava.util.ArrayList
stringjava.lang.String
numberjava.math.BigDecimal
true/falsejava.lang.Boolean
nullnull

バージョン:
0.9.7
作成者:
Hidekatsu Izuno
関連項目:
RFC 4627, the Apache License, Version 2.0

コンストラクタの概要
JSON()
           
 
メソッドの概要
protected
<T> T
convert(Object key, Object value, Class<? extends T> c, Type type)
          Converts Map/List/Number/String/Boolean/null to other Java Objects.
 Object convert(Object value, Type type)
           
protected
<T> T
convertChild(Object key, Object value, Class<? extends T> c, Type type)
          If you converts a lower level object in this method, You should call this method.
protected  Object create(Class<?> c)
           
static Object decode(String source)
          Decodes a json string into a object.
static
<T> T
decode(String source, Class<? extends T> cls)
          Decodes a json string into a typed object.
static Object decode(String source, Type type)
          Decodes a json string into a typed object.
static String encode(Object source)
          Encodes a object into a json string.
static String encode(Object source, boolean prettyPrint)
          Encodes a object into a json string.
 String format(Object source)
          Format a object into a json string.
 Appendable format(Object source, Appendable ap)
          Format a object into a json string.
protected  boolean ignore(Class<?> target, Member member)
           
 Object parse(CharSequence cs)
           
<T> T
parse(CharSequence s, Class<? extends T> cls)
           
 Object parse(CharSequence s, Type type)
           
 Object parse(InputStream in)
           
<T> T
parse(InputStream in, Class<? extends T> cls)
           
 Object parse(InputStream in, Type type)
           
 Object parse(Reader reader)
           
<T> T
parse(Reader reader, Class<? extends T> cls)
           
 Object parse(Reader reader, Type type)
           
 void setContext(Object value)
          Sets context for inner class.
 void setLocale(Locale locale)
          Sets locale for conversion or message.
 void setMaxDepth(int value)
          Sets maximum depth for the nest level.
 void setPrettyPrint(boolean value)
          Output json string is to human-readable format.
 
クラス java.lang.Object から継承されたメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

コンストラクタの詳細

JSON

public JSON()
メソッドの詳細

setContext

public void setContext(Object value)
Sets context for inner class.

パラメータ:
value - context object

setLocale

public void setLocale(Locale locale)
Sets locale for conversion or message.

パラメータ:
locale -

setPrettyPrint

public void setPrettyPrint(boolean value)
Output json string is to human-readable format.

パラメータ:
value - true to format human-readable, false to shorten.

setMaxDepth

public void setMaxDepth(int value)
Sets maximum depth for the nest level. default value is 32.

パラメータ:
value - maximum depth for the nest level.

encode

public static String encode(Object source)
Encodes a object into a json string.

パラメータ:
source - a object to encode.
戻り値:
a json string

encode

public static String encode(Object source,
                            boolean prettyPrint)
Encodes a object into a json string.

パラメータ:
source - a object to encode.
prettyPrint - output a json string with indent, space or break.
戻り値:
a json string

decode

public static Object decode(String source)
                     throws JSONParseException
Decodes a json string into a object.

パラメータ:
source - a json string to decode
戻り値:
a decoded object
例外:
JSONParseException - if the beginning of the specified string cannot be parsed.

decode

public static <T> T decode(String source,
                           Class<? extends T> cls)
                throws JSONParseException
Decodes a json string into a typed object.

パラメータ:
source - a json string to decode
cls - class for converting
戻り値:
a decoded object
例外:
JSONParseException - if the beginning of the specified string cannot be parsed.
JSONConvertException - if it cannot convert a class from a JSON value.

decode

public static Object decode(String source,
                            Type type)
                     throws JSONParseException
Decodes a json string into a typed object.

パラメータ:
source - a json string to decode
type - type specified generics parameters
戻り値:
a decoded object
例外:
JSONParseException - if the beginning of the specified string cannot be parsed.
JSONConvertException - if it cannot convert a class from a JSON value.

format

public String format(Object source)
Format a object into a json string.

パラメータ:
source - a object to encode.
戻り値:
a json string

format

public Appendable format(Object source,
                         Appendable ap)
                  throws IOException
Format a object into a json string.

パラメータ:
source - a object to encode.
ap - a destination. ex: StringBuilder, Writer, ...
戻り値:
a json string
例外:
IOException

parse

public Object parse(CharSequence cs)
             throws JSONParseException
例外:
JSONParseException

parse

public <T> T parse(CharSequence s,
                   Class<? extends T> cls)
        throws JSONParseException
例外:
JSONParseException

parse

public Object parse(CharSequence s,
                    Type type)
             throws JSONParseException
例外:
JSONParseException

parse

public Object parse(InputStream in)
             throws IOException,
                    JSONParseException
例外:
IOException
JSONParseException

parse

public <T> T parse(InputStream in,
                   Class<? extends T> cls)
        throws IOException,
               JSONParseException,
               JSONConvertException
例外:
IOException
JSONParseException
JSONConvertException

parse

public Object parse(InputStream in,
                    Type type)
             throws IOException,
                    JSONParseException,
                    JSONConvertException
例外:
IOException
JSONParseException
JSONConvertException

parse

public Object parse(Reader reader)
             throws IOException,
                    JSONParseException
例外:
IOException
JSONParseException

parse

public <T> T parse(Reader reader,
                   Class<? extends T> cls)
        throws IOException,
               JSONParseException
例外:
IOException
JSONParseException

parse

public Object parse(Reader reader,
                    Type type)
             throws IOException,
                    JSONParseException
例外:
IOException
JSONParseException

convert

public final Object convert(Object value,
                            Type type)
                     throws JSONConvertException
例外:
JSONConvertException

convertChild

protected final <T> T convertChild(Object key,
                                   Object value,
                                   Class<? extends T> c,
                                   Type type)
                        throws JSONConvertException
If you converts a lower level object in this method, You should call this method.

パラメータ:
key - property key object. If the parent is a array, it is Integer. otherwise it is String.
value - null or the instance of Map, List, Number, String or Boolean.
c - class for converting
type - generics type for converting. type equals to c if not generics.
戻り値:
a converted object
例外:
JSONConvertException - if conversion failed.

convert

protected <T> T convert(Object key,
                        Object value,
                        Class<? extends T> c,
                        Type type)
             throws Exception
Converts Map/List/Number/String/Boolean/null to other Java Objects. If you converts a lower level object in this method, You should call convertChild method.

パラメータ:
key - property key object. If root node is '$'. When the parent is a array, it is Integer, otherwise String.
value - null or the instance of Map, List, Number, String or Boolean.
c - class for converting
type - generics type for converting. type equals to c if not generics.
戻り値:
a converted object
例外:
Exception - if conversion failed.

ignore

protected boolean ignore(Class<?> target,
                         Member member)

create

protected Object create(Class<?> c)
                 throws Exception
例外:
Exception