|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectorg.less4j.JSON
org.less4j.JSONR
public class JSONR
Compile simple JSON Regular patterns to evaluate and validate a JSON string against an extensible type model of JSON types, numeric ranges, regular string, formated dates, collections, relations, regular dictionaries and relevant namespaces.
A safe JSONR intepreter to evaluate and validate a UNICODE string as a limited tree of Java instances that matches a regular pattern of types and values.
To compile a JSONR pattern with the default set of extension types, for instance a dummy constant model:
JSONR pattern = new JSONR("["my model", "is a constant"]");
that validates only the first JSON array evaluated:
try {
Object value = pattern.eval("[\"my model\", \"is a constant\"]);
Object value = pattern.eval("[\"will this fail?\", true]);
} catch (JSONR.Error e) {
System.out.println(e.str());
}
but will raise a JSONR.Error for the second.
The JSONR class provides similar interfaces than
JSON in the form of instance methods to evaluate and
validate any JSON values:
Object map;
try {
JSONR pattern = new JSONR("{\"pass\": null}");
map = pattern.eval(
"{\"pass\": true}"
);
map = pattern.eval(
"{\"will this fail?\": true}"
);
} catch (JSONR.Error e) {
System.out.println(e.str());
}
extend arrays
JSONR pattern = new JSONR("[\".+\"]");
JSON.Array list = new JSON.Array();
JSONR.Error e = pattern.extend(
list, "[\"my model\": \"is a constant\"]"
);
if (e != null)
System.out.println(e.str());
or update maps
JSONR pattern = new JSONR("[\".+\"]");
JSON.Object map = new JSON.Object();
JSONR.Error e = pattern.update(
map, "{\"will this fail?\": true}"
);
if (e != null)
System.out.println(e.str());
eventually with safety limits
JSONR.Error e;
JSON.Array list = new JSON.Array();
JSONR pattern = new JSONR("[0]", 1, 7);
e = pattern.extend(list, "[1,2,3,4,5]"
if (e != null)
System.out.println(e.str());
e = pattern.extend(list, "[1,2,3,4,5,6,7,8,9,0]");
if (e != null)
System.out.println(e.str());
or throw a JSONR.Error and stop the evaluation.
Some application demand specialized data types, most notably date and time types. This implementation provides only one, the most common JSON extension type:
"yyyy-MM-ddTHH:mm:ss" |
a valid date and type value formated alike, as the defacto standard JavaScript 1.7 serialization of a date and time instance. |
Copyright © 2006 Laurent A.V. Szyster
| Nested Class Summary | |
|---|---|
static class |
JSONR.Error
A simple JSONR exception throwed for any type or value error found by the regular interpreter. |
static interface |
JSONR.Type
An interface to extend JSONR with application types. |
| Nested classes/interfaces inherited from class org.less4j.JSON |
|---|
JSON.Array, JSON.Object |
| Field Summary | |
|---|---|
static JSONR.Type |
BOOLEAN
|
static JSONR.Type |
DATETIME
Cast java.util.Calendar objects from JSON DateTime strings. |
static JSONR.Type |
DECIMAL
|
static JSONR.Type |
DOUBLE
|
static JSONR.Type |
INTEGER
|
static JSONR.Type |
STRING
|
JSONR.Type |
type
|
static java.util.HashMap |
TYPES
The built-in extension types, currently mapping only one bizarre yet relevant and unambiguous type name, the obvious: "yyyy-MM-ddTHH:mm:ss" to the JSONR.DATETIME type. |
| Fields inherited from class org.less4j.JSON |
|---|
containers, iterations |
| Constructor Summary | |
|---|---|
JSONR(JSONR.Type type)
|
|
JSONR(JSONR.Type type,
int containers,
int iterations)
|
|
JSONR(java.lang.Object regular)
|
|
JSONR(java.lang.Object regular,
int containers,
int iterations)
|
|
JSONR(java.lang.String pattern)
|
|
JSONR(java.lang.String pattern,
int containers,
int iterations)
|
|
| Method Summary | |
|---|---|
static JSONR.Type |
compile(java.lang.Object regular,
java.util.Map extensions)
|
static JSONR.Type |
compile(java.lang.String pattern)
|
static JSONR.Type |
compile(java.lang.String pattern,
java.util.Map extensions)
|
java.lang.Object |
eval(java.lang.String json)
Evaluates a JSON String as an untyped value, returns a
JSON.Object,
JSON.Array,
String,
BigDecimal,
BigInteger,
Double,
Boolean,
null or throws a JSON.Error if a syntax
error occured. |
JSON.Error |
extend(java.util.List a,
java.lang.String json)
Evaluates a JSON String and extends a List,
return null or a JSON.Error if the string
does not represent a valid array. |
JSON.Error |
update(java.util.Map o,
java.lang.String json)
Evaluates a JSON String and update a Map,
return null or a JSON.Error if the string
does not represent a valid object. |
| Methods inherited from class org.less4j.JSON |
|---|
array, encode, object, repr, strb, strb, strb, strb, strb, xjson |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final JSONR.Type BOOLEAN
public static final JSONR.Type DATETIME
public static final JSONR.Type DECIMAL
public static final JSONR.Type DOUBLE
public static final JSONR.Type INTEGER
public static final JSONR.Type STRING
public JSONR.Type type
public static java.util.HashMap TYPES
The built-in extension types, currently mapping only one bizarre yet relevant and unambiguous type name, the obvious:
"yyyy-MM-ddTHH:mm:ss"to the
JSONR.DATETIME type.
| Constructor Detail |
|---|
public JSONR(JSONR.Type type)
public JSONR(JSONR.Type type,
int containers,
int iterations)
public JSONR(java.lang.Object regular)
throws JSON.Error
JSON.Error
public JSONR(java.lang.Object regular,
int containers,
int iterations)
throws JSON.Error
JSON.Error
public JSONR(java.lang.String pattern)
throws JSON.Error
JSON.Error
public JSONR(java.lang.String pattern,
int containers,
int iterations)
throws JSON.Error
JSON.Error| Method Detail |
|---|
public static final JSONR.Type compile(java.lang.Object regular,
java.util.Map extensions)
public static final JSONR.Type compile(java.lang.String pattern)
throws JSON.Error
JSON.Error
public static final JSONR.Type compile(java.lang.String pattern,
java.util.Map extensions)
throws JSON.Error
JSON.Error
public java.lang.Object eval(java.lang.String json)
throws JSON.Error
JSONString as an untyped value, returns a
JSON.Object,
JSON.Array,
String,
BigDecimal,
BigInteger,
Double,
Boolean,
null or throws a JSON.Error if a syntax
error occured.
eval in class JSONjson - string to evaluate
JSON.Error
public JSON.Error extend(java.util.List a,
java.lang.String json)
JSONString and extends a List,
return null or a JSON.Error if the string
does not represent a valid array.
extend in class JSONa - the List to extendjson - String to evaluate
null or a JSON.Error
public JSON.Error update(java.util.Map o,
java.lang.String json)
JSONString and update a Map,
return null or a JSON.Error if the string
does not represent a valid object.
update in class JSONo - the Map to updatejson - String to evaluate
null or a JSON.Error
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||