|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
public static interface JSONR.Type
An interface to extend JSONR with application types.
Custom type classes must implement the value,
eval and copy methods:
import org.less4j.JSONR;
import java.text.SimpleDateFormat;
public static class TypeDateTime implements JSONR.Type {
private static final SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String DATETIME_VALUE_ERROR =
"DateTime value error";
public Object value (Object instance) throws JSONR.Error {
return eval((String) JSONR.STRING.value(instance));
}
public Object eval (String string) throws JSONR.Error {
try {
string.replace('T', ' ');
Calendar dt = Calendar.getInstance();
dt.setTime(format.parse(string));
return dt;
} catch (Exception e) {
throw new JSONR.Error(DATETIME_VALUE_ERROR);
}
}
public static final JSONR.Type singleton = new TypeDateTime();
public Type copy() {return singleton;}
}
can be mapped to this name
to cast a JSON string like"yyyy-MM-ddTHH:mm:ss"
into the appropriate"2006-07-04T12:08:56"
java.util.Date instance.
Copyright © 2006 Laurent A.V. Szyster
| Field Summary | |
|---|---|
static JSONR.Type |
singleton
This public singleton is null by
default and needs only to be assigned an instance of the
type class if you expect it to be reused in other types. |
| Method Summary | |
|---|---|
JSONR.Type |
copy()
Make a "deep" copy of the Type, something that
can safely be passed to a distinct thread. |
java.lang.Object |
eval(java.lang.String string)
Evaluate a JSON string and validate it as regular |
java.lang.Object |
value(java.lang.Object instance)
|
| Field Detail |
|---|
static final JSONR.Type singleton
singleton is null by
default and needs only to be assigned an instance of the
type class if you expect it to be reused in other types.
| Method Detail |
|---|
JSONR.Type copy()
Type, something that
can safely be passed to a distinct thread.
Note that is is only required by applications that expect to
alter their JSONR patterns after compilation, something quite
unsual. Applications that don't (the overwhelming majority)
can consider a JSONR.Type as thread-safe.
Type
java.lang.Object eval(java.lang.String string)
throws JSON.Error
string - to evaluate as a regular type and value
Object, or
JSON.Error - if the string is irregular
java.lang.Object value(java.lang.Object instance)
throws JSONR.Error
instance - to validate as a regular type and value
Object, or
java.lang.Error - if the type or value is irregular
JSONR.Error
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||