org.less4j
Class JSONR

java.lang.Object
  extended by org.less4j.JSON
      extended by org.less4j.JSONR

public class JSONR
extends JSON

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.

Synopsis

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.

JSON Regular Evaluation

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.

Extension Types

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.
As a decent implementation of JSONR, this one is extensible and provides a Java interface and enough singleton to do so easely.

Copyright © 2006 Laurent A.V. Szyster

Version:
0.30

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

BOOLEAN

public static final JSONR.Type BOOLEAN

DATETIME

public static final JSONR.Type DATETIME
Cast java.util.Calendar objects from JSON DateTime strings.


DECIMAL

public static final JSONR.Type DECIMAL

DOUBLE

public static final JSONR.Type DOUBLE

INTEGER

public static final JSONR.Type INTEGER

STRING

public static final JSONR.Type STRING

type

public JSONR.Type type

TYPES

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

JSONR

public JSONR(JSONR.Type type)

JSONR

public JSONR(JSONR.Type type,
             int containers,
             int iterations)

JSONR

public JSONR(java.lang.Object regular)
      throws JSON.Error
Throws:
JSON.Error

JSONR

public JSONR(java.lang.Object regular,
             int containers,
             int iterations)
      throws JSON.Error
Throws:
JSON.Error

JSONR

public JSONR(java.lang.String pattern)
      throws JSON.Error
Throws:
JSON.Error

JSONR

public JSONR(java.lang.String pattern,
             int containers,
             int iterations)
      throws JSON.Error
Throws:
JSON.Error
Method Detail

compile

public static final JSONR.Type compile(java.lang.Object regular,
                                       java.util.Map extensions)

compile

public static final JSONR.Type compile(java.lang.String pattern)
                                throws JSON.Error
Throws:
JSON.Error

compile

public static final JSONR.Type compile(java.lang.String pattern,
                                       java.util.Map extensions)
                                throws JSON.Error
Throws:
JSON.Error

eval

public java.lang.Object eval(java.lang.String json)
                      throws JSON.Error
Description copied from class: 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.

Overrides:
eval in class JSON
Parameters:
json - string to evaluate
Returns:
an untyped Object
Throws:
JSON.Error

extend

public JSON.Error extend(java.util.List a,
                         java.lang.String json)
Description copied from class: JSON
Evaluates a JSON String and extends a List, return null or a JSON.Error if the string does not represent a valid array.

Overrides:
extend in class JSON
Parameters:
a - the List to extend
json - String to evaluate
Returns:
null or a JSON.Error

update

public JSON.Error update(java.util.Map o,
                         java.lang.String json)
Description copied from class: JSON
Evaluates a JSON String and update a Map, return null or a JSON.Error if the string does not represent a valid object.

Overrides:
update in class JSON
Parameters:
o - the Map to update
json - String to evaluate
Returns:
null or a JSON.Error