|
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | ||||||||
概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
java.lang.Objectnet.arnx.jsonic.JSON
public class JSON
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);
java type | json type |
---|---|
java.util.Map | object |
java.lang.Object (public property or field) | |
java.lang.Object[] | array |
java.util.Collection | |
boolean[], short[], int[], long[], float[], double[] | |
java.lang.CharSequence | string |
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.Locale | string (language-country) |
java.lang.Number | number |
byte, short, int, long, float, double | |
java.util.Date | number (milliseconds since 1970) |
java.util.Calendar | |
java.lang.Boolean | true/false |
boolean | |
null | null |
json type | java type |
---|---|
object | java.util.HashMap |
array | java.util.ArrayList |
string | java.lang.String |
number | java.math.BigDecimal |
true/false | java.lang.Boolean |
null | null |
コンストラクタの概要 | |
---|---|
JSON()
|
メソッドの概要 | ||
---|---|---|
protected
|
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
|
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
|
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)
|
|
|
parse(CharSequence s,
Class<? extends T> cls)
|
|
Object |
parse(CharSequence s,
Type type)
|
|
Object |
parse(InputStream in)
|
|
|
parse(InputStream in,
Class<? extends T> cls)
|
|
Object |
parse(InputStream in,
Type type)
|
|
Object |
parse(Reader reader)
|
|
|
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 |
コンストラクタの詳細 |
---|
public JSON()
メソッドの詳細 |
---|
public void setContext(Object value)
value
- context objectpublic void setLocale(Locale locale)
locale
- public void setPrettyPrint(boolean value)
value
- true to format human-readable, false to shorten.public void setMaxDepth(int value)
value
- maximum depth for the nest level.public static String encode(Object source)
source
- a object to encode.
public static String encode(Object source, boolean prettyPrint)
source
- a object to encode.prettyPrint
- output a json string with indent, space or break.
public static Object decode(String source) throws JSONParseException
source
- a json string to decode
JSONParseException
- if the beginning of the specified string cannot be parsed.public static <T> T decode(String source, Class<? extends T> cls) throws JSONParseException
source
- a json string to decodecls
- class for converting
JSONParseException
- if the beginning of the specified string cannot be parsed.
JSONConvertException
- if it cannot convert a class from a JSON value.public static Object decode(String source, Type type) throws JSONParseException
source
- a json string to decodetype
- type specified generics parameters
JSONParseException
- if the beginning of the specified string cannot be parsed.
JSONConvertException
- if it cannot convert a class from a JSON value.public String format(Object source)
source
- a object to encode.
public Appendable format(Object source, Appendable ap) throws IOException
source
- a object to encode.ap
- a destination. ex: StringBuilder, Writer, ...
IOException
public Object parse(CharSequence cs) throws JSONParseException
JSONParseException
public <T> T parse(CharSequence s, Class<? extends T> cls) throws JSONParseException
JSONParseException
public Object parse(CharSequence s, Type type) throws JSONParseException
JSONParseException
public Object parse(InputStream in) throws IOException, JSONParseException
IOException
JSONParseException
public <T> T parse(InputStream in, Class<? extends T> cls) throws IOException, JSONParseException, JSONConvertException
IOException
JSONParseException
JSONConvertException
public Object parse(InputStream in, Type type) throws IOException, JSONParseException, JSONConvertException
IOException
JSONParseException
JSONConvertException
public Object parse(Reader reader) throws IOException, JSONParseException
IOException
JSONParseException
public <T> T parse(Reader reader, Class<? extends T> cls) throws IOException, JSONParseException
IOException
JSONParseException
public Object parse(Reader reader, Type type) throws IOException, JSONParseException
IOException
JSONParseException
public final Object convert(Object value, Type type) throws JSONConvertException
JSONConvertException
protected final <T> T convertChild(Object key, Object value, Class<? extends T> c, Type type) throws JSONConvertException
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 convertingtype
- generics type for converting. type equals to c if not generics.
JSONConvertException
- if conversion failed.protected <T> T convert(Object key, Object value, Class<? extends T> c, Type type) throws Exception
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 convertingtype
- generics type for converting. type equals to c if not generics.
Exception
- if conversion failed.protected boolean ignore(Class<?> target, Member member)
protected Object create(Class<?> c) throws Exception
Exception
|
|||||||||
前のクラス 次のクラス | フレームあり フレームなし | ||||||||
概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |