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 = 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 |
| 修飾子とタイプ | クラスと説明 |
|---|---|
class |
JSON.Context |
static class |
JSON.Mode
JSON processing mode
|
| 修飾子とタイプ | フィールドと説明 |
|---|---|
static Class<? extends JSON> |
prototype
Setup your custom class for using static method. default: net.arnx.jsonic.JSON
|
| 修飾子とタイプ | メソッドと説明 |
|---|---|
Object |
convert(Object value,
Type type) |
protected <T> T |
create(JSON.Context context,
Class<? extends T> c) |
static <T> T |
decode(InputStream in)
Decodes a json stream into a object.
|
static <T> T |
decode(InputStream in,
Class<? extends T> cls)
Decodes a json stream into a object.
|
static <T> T |
decode(InputStream in,
Type type)
Decodes a json stream into a object.
|
static <T> T |
decode(Reader reader)
Decodes a json stream into a object.
|
static <T> T |
decode(Reader reader,
Class<? extends T> cls)
Decodes a json stream into a object.
|
static <T> T |
decode(Reader reader,
Type type)
Decodes a json stream into a object.
|
static <T> T |
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 <T> T |
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.
|
Appendable |
format(JSON.Context context,
Object source,
Appendable ap) |
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 depth.
|
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) |
<T> T |
parse(CharSequence cs) |
<T> T |
parse(CharSequence s,
Class<? extends T> cls) |
<T> T |
parse(CharSequence s,
Type type) |
<T> T |
parse(InputStream in) |
<T> T |
parse(InputStream in,
Class<? extends T> cls) |
<T> T |
parse(InputStream in,
Type type) |
<T> T |
parse(Reader reader) |
<T> T |
parse(Reader reader,
Class<? extends T> cls) |
<T> T |
parse(Reader reader,
Type type) |
protected <T> T |
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 depth.
|
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 |
setSupperssEmptyArray(boolean value) |
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
|
public JSON()
public JSON(int maxDepth)
public JSON(JSON.Mode mode)
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 decodeJSONException - 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 convertingJSONException - if error occurred when parsing.public static <T> T decode(String source, Type type) throws JSONException
source - a json string to decodetype - type for convertingJSONException - if error occurred when parsing.public static <T> T decode(InputStream in) throws IOException, JSONException
in - a json stream to decodeIOException - 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 convertingIOException - 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 convertingIOException - 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 decodeIOException - 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 convertingIOException - 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 convertingIOException - 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 decodeJSONException - if error occurred when parsing.public static void validate(InputStream in) throws IOException, JSONException
in - source a json string to decodeIOException - 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 decodeIOException - 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 depth.public int getMaxDepth()
public void setSuppressNull(boolean value)
value - true to ignore the member of null value in JSON object.public void setSupperssEmptyArray(boolean value)
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.IOExceptionpublic Appendable format(Object source, Appendable ap) throws IOException
source - a object to encode.ap - a destination. example: StringBuilder, Writer, ...IOExceptionpublic Appendable format(JSON.Context context, Object source, Appendable ap) throws IOException
IOExceptionprotected 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
JSONExceptionpublic <T> T parse(CharSequence s, Class<? extends T> cls) throws JSONException
JSONExceptionpublic <T> T parse(CharSequence s, Type type) throws JSONException
JSONExceptionpublic <T> T parse(InputStream in) throws IOException, JSONException
public <T> T parse(InputStream in, Class<? extends T> cls) throws IOException, JSONException
public <T> T parse(InputStream in, Type type) throws IOException, JSONException
public <T> T parse(Reader reader) throws IOException, JSONException
public <T> T parse(Reader reader, Class<? extends T> cls) throws IOException, JSONException
public <T> T parse(Reader reader, Type type) throws IOException, JSONException
public Object convert(Object value, Type type) throws JSONException
JSONExceptionprotected <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 boolean ignore(JSON.Context context, Class<?> target, Member member)
context - current contexttarget - target classmember - target memberprotected <T> T create(JSON.Context context, Class<? extends T> c) throws Exception
ExceptionCopyright © 2017. All rights reserved.