public final class Json extends Object
Modifier and Type | Method and Description |
---|---|
static String |
encode(Object object)
Encodes the given object as JSON.
|
static String |
escapeJS(String string,
boolean escapeSingleQuote)
Escapes the given string according the JavaScript code rules.
|
static String |
formatRFC1123(Date date)
Formats the given
Date to a string in RFC1123 format. |
static Date |
parseRFC1123(String string)
Parses the given string in RFC1123 format to a
Date object. |
public static String encode(Object object)
Boolean
, Number
,
CharSequence
and Date
. If the given object type does not match any of them, then it will attempt
to inspect the object as a javabean whereby the public properties (with public getters) will be encoded as a JS
object. It also supports Collection
s, Map
s and arrays of them, even nested ones. The Date
is formatted in RFC 1123 format, so you can if necessary just pass it straight to new Date()
in
JavaScript.object
- The object to be encoded as JSON.IllegalArgumentException
- When the given object or one of its properties cannot be inspected as a bean.public static String escapeJS(String string, boolean escapeSingleQuote)
string
- The string to be escaped according the JavaScript code rules.escapeSingleQuote
- Whether to escape single quotes as well or not. Set to false
if you want to
escape it for usage in JSON.public static String formatRFC1123(Date date)
Date
to a string in RFC1123 format. This format is used in HTTP headers and in
JavaScript Date
constructor.date
- The Date
to be formatted to a string in RFC1123 format.public static Date parseRFC1123(String string) throws ParseException
Date
object.string
- The string in RFC1123 format to be parsed to a Date
object.Date
.ParseException
- When the given string is not in RFC1123 format.Copyright © 2020 The Apache Software Foundation. All rights reserved.