public class ImaJson
extends Object
The object can be reused by calling parse with a new string.
The ImaJson class also provides a set of helper methods to create JSON.
Modifier and Type | Class and Description |
---|---|
class |
ImaJson.Entry
A JSON entry.
|
static class |
ImaJson.JObject
JSON object entry type.
|
Modifier and Type | Field and Description |
---|---|
static String |
COPYRIGHT |
Constructor and Description |
---|
ImaJson()
Create a JSON parser object.
|
Modifier and Type | Method and Description |
---|---|
static StringBuffer |
escape(StringBuffer to,
String from)
Copy a string to an output JSON buffer with escapes.
|
static String |
fromMap(Map<String,Object> map)
Create JSON object from a Map.
|
boolean |
getBoolean(String name,
boolean deflt)
Get a boolean from a named JSON item.
|
ImaJson.Entry[] |
getEntries()
Get the array of entries after a parse.
|
ImaJson.Entry |
getEntry(int i) |
int |
getEntryCount()
Get the count of entries after a parse.
|
int |
getInt(String name,
int default_value)
Get an integer from a named JSON item.
|
int |
getLine()
Return the line position after the parse.
|
int |
getPosition()
Return the position after the parse.
|
String |
getString(String name)
Get a string from a named JSON item.
|
Object |
getValue(int entnum)
Return the value of an item as an object.
|
void |
parse(File file) |
int |
parse(String jsonstr)
Parse a JSON message.
|
static void |
put(StringBuffer buf,
Object obj)
Put out a Java object in JSON form.
|
static void |
putString(StringBuffer buf,
String str)
Put out a set of bytes with JSON string escapes.
|
void |
setAllowComments(boolean allowComments)
Allow comments.
|
String[] |
toArray(int entnum)
Return a string array from a parsed JSON array.
|
Map<String,Object> |
toMap(int entnum)
Make a Java map object from a JSON object.
|
String |
toString() |
public static final String COPYRIGHT
public void setAllowComments(boolean allowComments)
allowComments
- If true allow commentspublic int getPosition()
public int getLine()
public int getEntryCount()
public ImaJson.Entry getEntry(int i)
public ImaJson.Entry[] getEntries()
public int parse(String jsonstr)
jsonstr
- A JSON string containing an object or arraypublic void parse(File file) throws Exception
Exception
public static StringBuffer escape(StringBuffer to, String from)
Escape the double quote, backslash, and all C0 control characters (0x00 - 0x1F).
public static void putString(StringBuffer buf, String str)
In JSON all strings are surrounded by double quotes, and the characters double quote, backslash, and control characters in the C0 range (0x00 - 0x1F) are required to be escaped.
buf
- The buffer to write tostr
- The String to writepublic static void put(StringBuffer buf, Object obj)
The Java object can be null, or can be an instance of String, Number, or Boolean. Number includes all of the common numeric types.
buf
- The string buffer to write toobj
- The object to write (null, String, Number, or Boolean).public String getString(String name)
This is used to find simple items within a single level JSON object. It finds the item with the specified name within the outermost object in the parsed JSON object. If this item is not an string, it attempts to convert it to a string. If the item is missing or cannot be converted to a string, return null.
name
- The name of the propertypublic int getInt(String name, int default_value)
This is used to find simple items within a single level JSON object. It finds the item with the specified name within the outermost object in the parsed JSON object. If this item is not an integer, it attempts to convert it to integer. If the item is missing or cannot be converted to integer, return the default value.
name
- The name of the propertydefault_value
- The default value to return if the named property is missing or cannot be
converted to a integer.public static String fromMap(Map<String,Object> map)
map
- The map to convert to a JSON objectpublic Object getValue(int entnum)
entnum
- The entry number in the parsed objectpublic String[] toArray(int entnum)
The specified entry in the parsed JSON object must be an array. The value of each object in the array is returned as an array of Strings. The items in the array are converted as required but must not be objects or arrays.
entnum
- The entry number within the parsed object.public boolean getBoolean(String name, boolean deflt)
This is used to find simple items within a single level JSON object. It finds the item with the specified name within the outermost object in the parsed JSON object. If this item is not a boolean, it attempts to convert it to boolean. If the item is missing or cannot be converted to boolean, return the default value.
name
- The name of the propertydefault_value
- The default value to return if the named property is missing or cannot be
converted to a boolean.public Map<String,Object> toMap(int entnum)
entnum
- The entry number in the parsed Java object. This must be a JSON object.public String toString()
toString
in class Object
Copyright © Contributors to the Eclipse Foundation 2014-2021