Monday, 25 May 2015

JSONPath and JSON Schema

JSONPath provides a JSON analogue to XPath.  JSONPaths come in one of several forms, including a JQuery-like pattern:

JSONPaths can be "definite" (i.e. single-valued):
$.id
or "indefinite" (i.e. multivalued):
$..id

They're cross-platform, but I've been using a Jayway library:
<dependency>
  <groupId>com.jayway.jsonpath</groupId>
  <artifactId>json-path</artifactId>
  <version>2.0.0</version>
</dependency>

This is based around a class JsonPath with straightforward static methods.  This adds a compilation overhead, so if you're using the same expression repeatedly you should look into the "compile" method, which gives a nonstatic version.

JSON schema is much less intuitive, and seems to have surprisingly little support in Java: http://json-schema.org/implementations.html
There's a useful online tool to generate a schema for a given JSON example:
http://jsonschema.net/#/

This seems to be the best publicized JSON schema library:
https://github.com/fge/json-schema-validator/

To build a schema object, you first need to convert it into Jackson's JSON types:
// Marshall schema
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(config.getJsonSchema());

// Build schema object
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
jsonSchema = factory.getJsonSchema(actualObj);

Once the schema object is built, validating documents against it requires a similar set of steps:

ObjectMapper mapper = new ObjectMapper();
JsonNode jn = mapper.readTree(entity.toString());
ProcessingReport report = jsonSchema.validate(jn);
if (!report.isSuccess()) {
  throw new IllegalArgumentException(
  "Entity did not conform to configured schema.");
}

Helpfully, json-schema-validator comes with a set of example classes, which show it in action.

Sunday, 17 May 2015

JFreeChart


Allows creation of quick, simple Swing-based graphs.  I'm using this because I want to do some quick numerical modelling and validate the results.

Available in Maven:
http://mvnrepository.com/artifact/org.jfree/jfreechart/1.0.19

Vogella have a nice example:
http://www.vogella.com/tutorials/JFreeChart/article.html

Would recommend this for examples of specific charts:
http://www.java2s.com/Code/Java/Chart/CatalogChart.htm
http://www.java2s.com/Code/Java/Chart/JFreeChartLineChartDemo6.htm

In most applications you'd probably use web services and D3 or some other JS library.  It would probably be better to write your data to CSV and open it in Excel/OOffice.

Monday, 4 May 2015

Ruby on Rails

It all looks a lot like Angular, but comes with quite detailed templates.

Quickstart:
http://guides.rubyonrails.org/getting_started.html

CourseRA:
https://class.coursera.org/webapplications-002/

Built on good MVC principles.

HOME/app/views for the view pages. They're written as HTML fragments.
config/routes.rb contains routing instructions.

It has a concept of "resource", and will auto-set up CRUD methods for these for you.  This makes it a bit like a CMS.

Dynamic view content is via a JSP-like servlet syntax.

Database storage is done using automagical mapping and active records, which abstract away the database layer.
You define a model for your data objects, and Ruby writes a "migration" script to set it up in the DB.
Default is Sqlite, but others are avaiable: http://rubylearning.com/satishtalim/ruby_mysql_tutorial.html

Helpful environment divisions are set up in the database YML file, to allow updates to be applied to dev/preprod/prod.