Displaying articles with tag

Liquid error: undefined method `include?' for nil:NilClass

Posted by david, Sun Jan 06 16:39:00 UTC 2008

So, I decided that the push-style event interface (Push parser) was not the ideal for what I wanted to do. Push-style interfaces like SAX are efficient, but can be harder for people to wrap their heads around to get the behavior they want. I in particular want to write a JSON <-> POJO library on top of the core, and decided that it becomes a bit harder to keep track of it all in your head using an event interface.

So, I switched to a pull-style model, which I have named JSONCursor. Every time a pertinent event (data encountered or start/end of some structured type), the parser wills top and return an ‘event’ object explaining what it found.

I also decided to raise the level of the interface quite a bit, so you get a full String object rather than being triggered as before to handle a new bit of text or escaped character, and being responsible for accumulating it all yourself to handle the event.

One of the fun things this provides is that I was able to make JSONCursor implement Iterator<JSONEvent>. Of course, iterators are normally over data structures and not data being processed, so Java did not design iterator to be able to return errors. I currently wrap errors and raise RuntimeExceptions to get around this; I may decide at some point to make all of JSONException extend RuntimeException instead.

For now, since its relatively easy to have a pull parser emulate a push-parser, I still support a SAX-style interface as well. It was the easiest way to get things up and running and testable. I will probably drop this interface in the future just because of the additional size and confusion – or move it to another package, if there is one which would be relevant.

Liquid error: undefined method `include?' for nil:NilClass | Filed Under: | Tags:

Liquid error: undefined method `include?' for nil:NilClass

Posted by david, Tue Dec 25 19:10:00 UTC 2007

In addition to starting this blog, I just created a new project, JSON4Java. Its a fun little tinkering project that I started somewhat on accident.

I decided to use this as an opportunity to work more with a parser generator. I decided to use Ragel, since there is relatively little state needed to handle the JSON format. Ragel syntax had me stumped a few days – while writing the grammar was rather simple, embedding the actions can cause problems. In the end, I managed to tweak the thing until all of my test cases passed. Tests are absolutely vital for this sort of work!

I’m going to experiment now with adding a bit better error reporting, but also creating a somewhat SAX-ish interface on it to split out the generated parser from my business object for creating data. Ideally, the same interface could be used to both serialize and deserialize structures for JSON, and I’m hoping (if I get to that point) I can use Java annotations to allow mapping to more complex POJOs. I already split out the package into a json4java-core in anticipation for a json4java-pojo.

This sort of project also gives a level of freedom for experimentation and learning you just can’t get through typical commercial software development. I’ve finally got my unit testing ‘fu’ on, learning the new techniques in JUnit 4.4 (including assertThat); I’ve learned a bunch about maven; learned Ragel and implemented successfully a parser written in it. I’ve also started looking into newer Java features like Attributes, and around things like dependency injection and XML binding frameworks for ideas on allowing people to override functionality.

Liquid error: undefined method `include?' for nil:NilClass | Filed Under: | Tags: