<?
class TJSONBuilder extends Countable {
    /**
     * JSON collection object
     * @var unknown_type
     */
    private $json = null;
    /**
     * Number of items added to json collection
     * @var unknown_type
     */
    private $count = 0;
    /**
     * Initializes a new instance of the TJSONBuilder class
     */
    public function __construct() {
        $this->json = new stdClass();
    }
    /**
     * Insert new node
     * @param string $json_string
     * @param boolean $assoc
     * @param integer $deep
     */
    public function add($json_string, $assoc = false, $deep = 5) {
        $id = null;
        $decode = json_decode($json_string, $assoc, $deep);

        foreach ($decode as $k => $v) {
            $id = $k;
            break;
        }

        if (isset($id)) {
            $this->json->{$id} = $decode->{$id};
            ++$this->count;
        }
    }
    /**
     * Clear nodes
     */
    public function clear() {
        $this->json = new stdClass();
        $this->count = 0;
    }
    /**
     * Get json class
     * @return Ambiguous
     */
    public function get() {
        return $this->json;
    }
    /**
     * Dump as json string
     * @param constant $json_options
     * @return string
     */
    public function toString($json_options = JSON_FORCE_OBJECT) {
        return json_encode($this->json, $json_options);
    }
    /**
     * Get number of nodes
     * @return int
     */
    public function count() {
        return $this->count;
    }
}