This PHP library provides a simple, dependency-free serialization of a PHP variable value / datastructure to strings which can be interpreted as a JavaScript value.

The most typical use case will be the automated generation of JavaScript code in cases where the amount of data is notably and therefore serializing to JSON would suffer from the deserialization penalty and/or the larger size of JSON data.

The following datatypes can be handled:

  • null
  • string
  • float
  • int
  • array
  • object – if the object implements the JsonSerializable interface, has a public toArray() method or has a public __toString() method. (The mentioned order is exactly the order in which the code performs the checks.)

For example, consider this PHP data structure:

    'foo'    => 'bar',
    'nested' => [
        'pi'     => 3.14,
        'key'    => null,
        'abc'    => "String \" with 'quotes'",
        'My key' => 'Hello world',
        'bar'    => [
            'key'  => 'A',
            'code' => 65,

After serialization through this library, you will get …

{foo: 'bar', nested: {pi: 3.14, key: null, abc: 'String " with \'quotes\'', 'My key': 'Hello world', bar: {key: 'A', code: 65}}}

…, which is a value you can directly insert into JavaScript code and work with it.