net.arnx.jsonic
クラス JSON

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

public class JSON
extends java.lang.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);
 
 // invokes method by a json array.
 JSON json = new JSON();
 Object result = json.invoke(data, "method", "[true, 1]");
 

Summary of encoding rules for java type into json type

java type json type
java.util.Mapobject
java.lang.Serializable (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
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
作成者:
Hidekatsu Izuno
関連項目:
RFC 4627, the Apache License, Version 2.0

コンストラクタの概要
JSON()
           
 
メソッドの概要
protected  java.lang.Object convert(java.lang.Package pk, java.lang.Object value, java.lang.Class c, java.lang.reflect.Type type)
           
protected  java.lang.Object create(java.lang.Class<?> c)
           
static java.lang.Object decode(java.lang.String source)
          Decodes a json string into a object.
static
<T> T
decode(java.lang.String source, java.lang.Class<? extends T> c)
          Decodes a json string into a typed object.
static java.lang.String encode(java.lang.Object source)
          Encodes a object into a json string.
static java.lang.String encode(java.lang.Object source, boolean prettyPrint)
          Encodes a object into a json string.
 java.lang.String format(java.lang.Object source)
           
 java.lang.StringBuilder format(java.lang.Object source, java.lang.StringBuilder sb)
           
protected  void handleConvertError(java.lang.String key, java.lang.Object value, java.lang.Class c, java.lang.reflect.Type type, java.lang.Exception e)
           
protected  void handleParseError(java.lang.String message, java.lang.CharSequence s, int pos, int line, int col)
           
 java.lang.Object invoke(java.lang.Object o, java.lang.String methodName, java.lang.CharSequence json)
          Invokes the targeted method for the specified object, with the specified json parameters.
protected  boolean limit(java.lang.reflect.Method method)
           
 java.lang.Object parse(java.lang.CharSequence source)
           
<T> T
parse(java.lang.CharSequence source, java.lang.Class<? extends T> c)
           
 void setExtendedMode(boolean value)
          Sets extended mode.
 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()
メソッドの詳細

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.

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

setExtendedMode

public void setExtendedMode(boolean value)
Sets extended mode.

パラメータ:
value - true to enable extension mode, false to disable

encode

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

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

encode

public static java.lang.String encode(java.lang.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 java.lang.Object decode(java.lang.String source)
                               throws java.text.ParseException
Decodes a json string into a object.

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

decode

public static <T> T decode(java.lang.String source,
                           java.lang.Class<? extends T> c)
                throws java.lang.Exception
Decodes a json string into a typed object.

パラメータ:
source - a json string to decode
c - class for converting
戻り値:
a decoded object
例外:
java.text.ParseException - if the beginning of the specified string cannot be parsed.
java.lang.Exception

format

public java.lang.String format(java.lang.Object source)

format

public java.lang.StringBuilder format(java.lang.Object source,
                                      java.lang.StringBuilder sb)

parse

public java.lang.Object parse(java.lang.CharSequence source)
                       throws java.text.ParseException
例外:
java.text.ParseException

parse

public <T> T parse(java.lang.CharSequence source,
                   java.lang.Class<? extends T> c)
        throws java.lang.Exception
例外:
java.lang.Exception

invoke

public java.lang.Object invoke(java.lang.Object o,
                               java.lang.String methodName,
                               java.lang.CharSequence json)
                        throws java.lang.Exception
Invokes the targeted method for the specified object, with the specified json parameters.

パラメータ:
o - the object the underlying method is invoked from
methodName - the invoked method name
json - the parameters used for the method call. json should be array, or appended '[' and ']'.
例外:
java.lang.Exception

handleParseError

protected void handleParseError(java.lang.String message,
                                java.lang.CharSequence s,
                                int pos,
                                int line,
                                int col)
                         throws java.text.ParseException
例外:
java.text.ParseException

convert

protected java.lang.Object convert(java.lang.Package pk,
                                   java.lang.Object value,
                                   java.lang.Class c,
                                   java.lang.reflect.Type type)
                            throws java.lang.Exception
例外:
java.lang.Exception

handleConvertError

protected void handleConvertError(java.lang.String key,
                                  java.lang.Object value,
                                  java.lang.Class c,
                                  java.lang.reflect.Type type,
                                  java.lang.Exception e)
                           throws java.lang.Exception
例外:
java.lang.Exception

create

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

limit

protected boolean limit(java.lang.reflect.Method method)