JSONBuilder

Released 4 years ago , Last update 4 years ago

A json_encode wrapper that allows javascript functions

This small component eases server side configuration of javascript components. The developer can write js functions inside PHP arrays wrapping them in a thin class and then serialize the array using the JSONBuilder class. This class act as a wrapper to the built-in json_encode PHP function that uses pcre regexps to recognize js functions and conveniently unquote and unescape them.

This component is unit tested (tests are in the distribution) and requires PHP 5.3+ with PCRE and json_encode support.

5.0
  • 5 1
  • 4 0
  • 3 0
  • 2 0
  • 1 0
1 Reviews Read Reviews

Pricing

FREE

GNU GPL

The GNU GPL is an open-source license.

Using the JSONBuilder

Install

To install this component just drop it inside your project. If you are using PHP namespaces and autoloading you should take in count that all classes of the component are inside the root namespace A2PLab\Component\JSONBuilder.

Usage

To use this component include the JSONBuilder and the JSONFunction classes (refer to your autoloading method for this). Then you can create a PHP array containing javascript functions doing:

<?php
  $config = array(
      'firstValue' => 'value1', // A value
      'secondValue => true, // Another value (boolean)
      'firstHandler' => new JSONFunction('handlerRef'), // A function (in this case the name of a callback)
      'secondHandler' => new JSONFunction('function (event) { alert("event fired!"); }'), // Another function (inline function)
  );

To get the JSON from an array you just do:

$builder = new JSONBuilder();
$json = $builder->buildJSON($config);

The buildJSON method accepts as second parameter flags to give to the json_encode built-in function.

2 licenses, starting from From » FREE View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


Or enter your name and Email