org.less4j
Interface JSONR.Type

Enclosing class:
JSONR

public static interface JSONR.Type

An interface to extend JSONR with application types.

Synopsis

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
"yyyy-MM-ddTHH:mm:ss"
to cast a JSON string like
"2006-07-04T12:08:56"
into the appropriate java.util.Date instance.

Copyright © 2006 Laurent A.V. Szyster

Version:
0.30

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

singleton

static final 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 Detail

copy

JSONR.Type copy()
Make a "deep" copy of the 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.

Returns:
an unsynchronized copy as a Type

eval

java.lang.Object eval(java.lang.String string)
                      throws JSON.Error
Evaluate a JSON string and validate it as regular

Parameters:
string - to evaluate as a regular type and value
Returns:
a regular Object, or
Throws:
JSON.Error - if the string is irregular

value

java.lang.Object value(java.lang.Object instance)
                       throws JSONR.Error
Parameters:
instance - to validate as a regular type and value
Returns:
a regular Object, or
Throws:
java.lang.Error - if the type or value is irregular
JSONR.Error