|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
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 = JSON.decode(new FileInputStream("bar.json"), Bar.class);
Bar bar = JSON.decode(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 | |
| java.net.URI | |
| java.net.URL | |
| 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.LinkedHashMap |
| array | java.util.ArrayList |
| string | java.lang.String |
| number | java.math.BigDecimal |
| true/false | java.lang.Boolean |
| null | null |
| Nested Class Summary | |
|---|---|
class |
JSON.Context
|
static class |
JSON.Mode
JSON processing mode |
| Field Summary | |
|---|---|
static Class<? extends JSON> |
prototype
Setup your custom class for using static method. |
| Constructor Summary | |
|---|---|
JSON()
|
|
JSON(int maxDepth)
|
|
JSON(JSON.Mode mode)
|
|
| Method Summary | ||
|---|---|---|
Object |
convert(Object value,
Type type)
|
|
protected
|
create(JSON.Context context,
Class<? extends T> c)
|
|
static
|
decode(InputStream in)
Decodes a json stream into a object. |
|
static
|
decode(InputStream in,
Class<? extends T> cls)
Decodes a json stream into a object. |
|
static
|
decode(InputStream in,
Type type)
Decodes a json stream into a object. |
|
static
|
decode(Reader reader)
Decodes a json stream into a object. |
|
static
|
decode(Reader reader,
Class<? extends T> cls)
Decodes a json stream into a object. |
|
static
|
decode(Reader reader,
Type type)
Decodes a json stream into a object. |
|
static
|
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
|
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 void |
encode(Object source,
Appendable appendable)
Encodes a object into a json string. |
|
static void |
encode(Object source,
Appendable appendable,
boolean prettyPrint)
Encodes a object into a json string. |
|
static String |
encode(Object source,
boolean prettyPrint)
Encodes a object into a json string. |
|
static void |
encode(Object source,
OutputStream out)
Encodes a object into a json string. |
|
static void |
encode(Object source,
OutputStream out,
boolean prettyPrint)
Encodes a object into a json string. |
|
static String |
escapeScript(Object source)
Escapes a object into JavaScript format. |
|
static void |
escapeScript(Object source,
Appendable appendable)
Escapes a object into JavaScript format. |
|
static void |
escapeScript(Object source,
OutputStream out)
Escapes a object into JavaScript format. |
|
String |
format(Object source)
Format a object into a json string. |
|
Appendable |
format(Object source,
Appendable ap)
Format a object into a json string. |
|
OutputStream |
format(Object source,
OutputStream out)
Format a object into a json string. |
|
int |
getMaxDepth()
Gets maximum depth for the nest level. |
|
JSON.Mode |
getMode()
Gets JSON interpreter mode. |
|
protected boolean |
ignore(JSON.Context context,
Class<?> target,
Member member)
Ignore this property. |
|
protected String |
normalize(String name)
|
|
|
parse(CharSequence cs)
|
|
|
parse(CharSequence s,
Class<? extends T> cls)
|
|
|
parse(CharSequence s,
Type type)
|
|
|
parse(InputStream in)
|
|
|
parse(InputStream in,
Class<? extends T> cls)
|
|
|
parse(InputStream in,
Type type)
|
|
|
parse(Reader reader)
|
|
|
parse(Reader reader,
Class<? extends T> cls)
|
|
|
parse(Reader reader,
Type type)
|
|
protected
|
postparse(JSON.Context context,
Object value,
Class<? extends T> cls,
Type type)
Converts Map, List, Number, String, Boolean or null to other Java Objects after parsing. |
|
protected Object |
preformat(JSON.Context context,
Object value)
Converts Any Java Object to JSON recognizable Java object before format. |
|
void |
setContext(Object value)
Sets context for inner class. |
|
void |
setDateFormat(String format)
Sets default Date format. |
|
void |
setEnumStyle(NamingStyle style)
Sets default Case style for Enum. |
|
void |
setLocale(Locale locale)
Sets locale for formatting, converting and selecting message. |
|
void |
setMaxDepth(int value)
Sets maximum depth for the nest level. |
|
void |
setMode(JSON.Mode mode)
Sets JSON interpreter mode. |
|
void |
setNumberFormat(String format)
Sets default Number format. |
|
void |
setPrettyPrint(boolean value)
Output json string is to human-readable format. |
|
void |
setPropertyStyle(NamingStyle style)
Sets default Case style for the property name of JSON object. |
|
void |
setSuppressNull(boolean value)
If this property is true, the member of null value in JSON object is ignored. |
|
void |
setTimeZone(TimeZone timeZone)
Sets timeZone for formatting and converting. |
|
static void |
validate(CharSequence cs)
Validates a json text |
|
static void |
validate(InputStream in)
Validates a json stream |
|
static void |
validate(Reader reader)
Validates a json stream |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static volatile Class<? extends JSON> prototype
| Constructor Detail |
|---|
public JSON()
public JSON(int maxDepth)
public JSON(JSON.Mode mode)
| Method Detail |
|---|
public static String encode(Object source)
throws JSONException
source - a object to encode.
JSONException - if error occurred when formating.
public static String encode(Object source,
boolean prettyPrint)
throws JSONException
source - a object to encode.prettyPrint - output a json string with indent, space or break.
JSONException - if error occurred when formating.
public static void encode(Object source,
OutputStream out)
throws IOException,
JSONException
source - a object to encode.out - a destination to output a json string.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static void encode(Object source,
OutputStream out,
boolean prettyPrint)
throws IOException,
JSONException
source - a object to encode.out - a destination to output a json string.prettyPrint - output a json string with indent, space or break.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static void encode(Object source,
Appendable appendable)
throws IOException,
JSONException
source - a object to encode.appendable - a destination to output a json string.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static void encode(Object source,
Appendable appendable,
boolean prettyPrint)
throws IOException,
JSONException
source - a object to encode.appendable - a destination to output a json string.prettyPrint - output a json string with indent, space or break.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static String escapeScript(Object source)
throws JSONException
source - a object to encode.
JSONException - if error occurred when formating.
public static void escapeScript(Object source,
OutputStream out)
throws IOException,
JSONException
source - a object to encode.out - a destination to output a json string.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static void escapeScript(Object source,
Appendable appendable)
throws IOException,
JSONException
source - a object to encode.appendable - a destination to output a json string.
IOException - if I/O Error occurred.
JSONException - if error occurred when formating.
public static <T> T decode(String source)
throws JSONException
source - a json string to decode
JSONException - if error occurred when parsing.
public static <T> T decode(String source,
Class<? extends T> cls)
throws JSONException
source - a json string to decodecls - class for converting
JSONException - if error occurred when parsing.
public static <T> T decode(String source,
Type type)
throws JSONException
source - a json string to decodetype - type for converting
JSONException - if error occurred when parsing.
public static <T> T decode(InputStream in)
throws IOException,
JSONException
in - a json stream to decode
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static <T> T decode(InputStream in,
Class<? extends T> cls)
throws IOException,
JSONException
in - a json stream to decodecls - class for converting
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static <T> T decode(InputStream in,
Type type)
throws IOException,
JSONException
in - a json stream to decodetype - type for converting
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static <T> T decode(Reader reader)
throws IOException,
JSONException
reader - a json stream to decode
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static <T> T decode(Reader reader,
Class<? extends T> cls)
throws IOException,
JSONException
reader - a json stream to decodecls - class for converting
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static <T> T decode(Reader reader,
Type type)
throws IOException,
JSONException
reader - a json stream to decodetype - type for converting
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static void validate(CharSequence cs)
throws JSONException
cs - source a json string to decode
JSONException - if error occurred when parsing.
public static void validate(InputStream in)
throws IOException,
JSONException
in - source a json string to decode
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.
public static void validate(Reader reader)
throws IOException,
JSONException
reader - source a json string to decode
IOException - if I/O error occurred.
JSONException - if error occurred when parsing.public void setContext(Object value)
value - context objectpublic void setLocale(Locale locale)
locale - locale for formatting, converting and selecting messagepublic void setTimeZone(TimeZone timeZone)
timeZone - timeZone for formatting and converting.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 int getMaxDepth()
public void setSuppressNull(boolean value)
value - true to ignore the member of null value in JSON object.public void setMode(JSON.Mode mode)
mode - JSON interpreter modepublic JSON.Mode getMode()
public void setDateFormat(String format)
format - default Date formatpublic void setNumberFormat(String format)
format - default Number formatpublic void setPropertyStyle(NamingStyle style)
style - default Case style for keys of JSON object.public void setEnumStyle(NamingStyle style)
style - default Case style for Enum.public String format(Object source)
source - a object to encode.
public OutputStream format(Object source,
OutputStream out)
throws IOException
source - a object to encode.out - a destination to output a json string.
IOException
public Appendable format(Object source,
Appendable ap)
throws IOException
source - a object to encode.ap - a destination. example: StringBuilder, Writer, ...
IOException
protected Object preformat(JSON.Context context,
Object value)
throws Exception
context - current context.value - source a object to format.
Exception - if conversion failed.
public <T> T parse(CharSequence cs)
throws JSONException
JSONException
public <T> T parse(CharSequence s,
Class<? extends T> cls)
throws JSONException
JSONException
public <T> T parse(CharSequence s,
Type type)
throws JSONException
JSONException
public <T> T parse(InputStream in)
throws IOException,
JSONException
IOException
JSONException
public <T> T parse(InputStream in,
Class<? extends T> cls)
throws IOException,
JSONException
IOException
JSONException
public <T> T parse(InputStream in,
Type type)
throws IOException,
JSONException
IOException
JSONException
public <T> T parse(Reader reader)
throws IOException,
JSONException
IOException
JSONException
public <T> T parse(Reader reader,
Class<? extends T> cls)
throws IOException,
JSONException
IOException
JSONException
public <T> T parse(Reader reader,
Type type)
throws IOException,
JSONException
IOException
JSONException
public Object convert(Object value,
Type type)
throws JSONException
JSONException
protected <T> T postparse(JSON.Context context,
Object value,
Class<? extends T> cls,
Type type)
throws Exception
context - current context.value - null or the instance of Map, List, Number, String or Boolean.cls - class for convertingtype - generics type for converting. type equals to c if not generics.
Exception - if conversion failed.protected String normalize(String name)
protected boolean ignore(JSON.Context context,
Class<?> target,
Member member)
context - current contexttarget - target classmember - target member
protected <T> T create(JSON.Context context,
Class<? extends T> c)
throws Exception
Exception
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||