Parsing a JSON data stream

While browsing StackOverflow I accidentally stumbled upon this question.
The issue seemed like a simple but interesting one, and since google had no answers I thought it would be fun to give it a shot. The first iteration of my solution involved reading in strings of indefinite length, but it quickly turned into a overly-complex state machine so I stepped back and rewrote it with one-character-at-a-time input. You can find on github, under JSONCharInputReader.

JSONCharInputReader was designed to read in a JSON array, not object.
In other words, this will work:

[1, 3, 4, {"var": "val"}, ["array_item", "array_item"], ...

but the following will not:

{"key": ["array", 1, 2], "key2": "value", ...

To parse a JSON stream, create a JSONCharInputReader object and pass in your own implementation of the JSONChunkProcessor interface. JSONChunkProcessor defines one function:

interface JSONChunkProcessor {
    public function process($jsonChunk);
}

Clients implementing this can expect $jsonChunk to be valid JSON (as long as the JSON data being read in is valid itself). If the above array was to be read in, the decoded $jsonChunks passed to the processor would be as follows:


// Decoding 1
int(1)

// Decoding  3
int(3)

// Decoding  4
int(4)

// Decoding  {"var": "val"}
object(stdClass)#3 (1) {
  ["var"]=>
  string(3) "val"
}

// Decoding  ["array_item", "array_item"]
array(2) {
  [0]=>
  string(10) "array_item"
  [1]=>
  string(10) "array_item"
}

See example.php for a sample implementation of the reader, which can read JSON from the terminal by executing it with:

cat | php example.php

One notable limitation is that the $jsonChunks passed to JSONChunkProcessor will be first-dimension level elements of the incoming JSON array data stream. In other words, large objects or arrays will only be processed once the reader receives all of their data.

PHP code completion for anything in Eclipse + PDT

While I prefer to make quick changes to my code in a lightweight text editor, any lengthy coding session usually involves loading up an IDE in order to have access to all of the extra time-saving features. Code completion is one of those hard-to-live-without features that IDEs provide.. here’s a quick rundown showing how to get it working with any library or class (visible to your buildpath) to work under Eclipse + PDT.

The idea is very simple, although it is a bit of a trick as you have to apply it to any/every project you work on. Using PHPDoc’s @property you can reference any variable in a class (even if it hasn’t been declared) and associate it with a PHP Type. PDT does the rest!

For example, this is how you can add code completion to a CodeIgniter project’s controller classes:

Create a custom base controller (ie. MY_Controller — see the CI guide for more info) and at the top, in the class’ PHPDoc, add @property tags for any variable you want to use. Now any controllers you create, that extend MY_Controller, will automatically have code completion enabled for the variables you tag. In the following example, $load, $session and $output will be available.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Base Controller Class
 * @property CI_Loader $load
 * @property CI_Session $session
 * @property CI_Output $output
 */
 
abstract class MY_Controller extends CI_Controller
{
// ...
}

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.