chickadee » php-s11n

PHP serialization

This egg provides serialization/unserialization of Scheme values in a format compatible with the ubiquitous PHP scripting language. This can be useful for communicating with PHP applications (such as the popular Drupal or WordPress) over a socket or HTTP connection, or for directly reading and writing existing serialized PHP application data in files or DBMS columns.

Data types

PHP's native data types are rather limited, meaning that only some of the basic Scheme types can be serialized into and from this format. Scheme values are mapped to PHP equivalents (or substitutes) as follows:

Scheme PHP
void NULL
boolean boolean
integer integer (platform-dependent, but assume 32-bit signed)
flonum float (platform-dependent, but assume 64-bit double precision)
string string
character string
symbol string
vector array (contiguous numerical indexing from 0..n-1)
alist array (associative, numeric or string keys)
hash-table array (associative, numeric or string keys)
N/A object

As the table above demonstrates, Scheme types such as characters and symbols must be coerced into strings since PHP lacks the requisite equivalent types, and Scheme's numeric types may be truncated or lose precision when serialized into PHP format. In other words, PHP's native types form a minimal subset of Scheme's, so while unserializing PHP data in Scheme will always work, you have to be mindful when serializing out of Scheme.

Special consideration is due PHP's arrays. In PHP, there are no distinct indexed and associative array types; instead, there is a single array type, which can contain both integer and string indices. This means PHP's arrays are actually ordered maps of keys to values, and can serve both as vectors and as hash tables depending on what you put into them.

There is also a special caveat in that while keys can be integers and strings both, those strings which can be read as integers are automagically coerced into a numeric type (e.g. "7" becomes 7). php-s11n-write follows this behavior so as to produce output as identical as possible to what PHP's native serialize() function would spew out.

The obvious Scheme equivalent to PHP's associative arrays is the association list (i.e., lists of the form ((k1 . v1) (k2 . v2) ...)), which is what php-s11n-read will return by default when unserializing a PHP array.

There is one exception to the above: if all the indices of the array being unserialized are numerical and form a contiguous range from 0..n-1, then the PHP array will be returned as a Scheme vector. This allows dealing with vector-like PHP arrays in a half-way sane manner. As php-s11n-write also takes care to serialize Scheme vectors into PHP arrays indexed using this very same method, it follows that a read-write equivalence holds for Scheme vectors.

If this default behavior is undesirable, array handling in the serialization and unserialization process can be fully customized and extended through the parameters php-s11n-writer and php-s11n-array-reader. (See the Examples section, below.)

Not wishing to be tied into any particular add-on object system for Scheme, this egg does not by default handle serialization and unserialization of PHP object types. Attempting to unserialize a PHP object will raise an error. However, the parameters php-s11n-writer and php-s11n-object-reader allow you to bolt on application-specific object type support in a seamless and facile manner.

Examples

Serializing to PHP format

#;1> (use php-s11n)

#;2> (php-serialize #t)
"b:1;"

#;3> (php-serialize 3.1415)
"d:3.1415;"

#;4> (php-serialize '#("a" "b" "c"))
"a:3:{i:0;s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";}"

#;5> (php-serialize '((first_name . "Random") (last_name . "Hacker")))
"a:2:{s:10:\"first_name\";s:6:\"Random\";s:9:\"last_name\";s:6:\"Hacker\";}"

#;6> (php-serialize (void))
"N;"

Unserializing from PHP format

#;1> (use php-s11n)

#;2> (php-unserialize "a:3:{i:1;s:1:\"a\";i:2;s:1:\"b\";i:3;s:1:\"c\";}")
((1 . "a") (2 . "b") (3 . "c"))

#;3> (php-unserialize (php-serialize '((a . orange) (b . banana) (c . apple))))
(("a" . "orange") ("b" . "banana") ("c" . "apple"))

#;4> (php-unserialize (php-serialize '#(1 2 3 5 8 13 21 34)))
#(1 2 3 5 8 13 21 34)

Mapping PHP associative arrays to Scheme key-value lists

Because the interpretation of PHP's associative arrays can be ambigious and application-dependent, the php-s11n egg provides a number of parameters for hooking into and extending the serialization/unserialization process.

As an example of how to use this, say that you wanted to represent associative arrays using key-value lists as provided by the kvlists egg. First, you would define a custom writer and reader as follows:

(use php-s11n kvlists)

(define (php-s11n-write-array/kvlist value port)
  (if (kvlist? value)
      (php-s11n-write (kvlist->alist value))
      (error 'php-s11n-write "unable to serialize object" value)))

(define (php-s11n-read-array/kvlist port)
  (alist->kvlist (map (lambda (pair)
                        (cons (string->symbol (->string (car pair)))
                              (cdr pair)))
                      (php-s11n-read-array/alist port))))

(php-s11n-writer php-s11n-write-array/kvlist)
(php-s11n-array-reader php-s11n-read-array/kvlist)

Now, after loading the above code, you could use key-value lists like follows:

#;1> (php-unserialize (php-serialize '(a: orange b: banana c: apple)))
(a: "orange" b: "banana" c: "apple")

Author

Arto Bendiken

Requires

Serialization and unserialization

php-serialize

(php-serialize VALUE) procedure

Returns a string containing the serialized PHP representation of the Scheme value VALUE. This is a convenience wrapper around php-s11n-write, the latter doing all the actual work.

php-unserialize

(php-unserialize STRING) procedure

Reads a serialized PHP literal from STRING and returns it as a Scheme value. This is a convenience wrapper around php-s11n-read, the latter doing all the actual work.

Parameters

php-s11n-writer

php-s11n-writer parameter

Sets the procedure to call for serializing any Scheme values not included in the default data type mappings built into the php-s11n egg. The procedure will be invoked with two arguments, VALUE and PORT.

The parameter defaults to #f, raising an error when trying to serialize an unsupported data type.

php-s11n-reader

php-s11n-reader parameter

Sets a procedure to call for unserializing any unknown data types encountered in the input. The procedure will be invoked with one argument, PORT. Note that you should never need to handle this case unless PHP's serialization format substantially changes.

The parameter defaults to #f, raising an error if the serialized input contains unknown data type specifiers.

php-s11n-array-reader

php-s11n-array-reader parameter

Sets a procedure to call for unserializing any PHP array encountered in the input. The procedure will be invoked with one argument, PORT.

The parameter defaults to php-s11n-read-array.

php-s11n-object-reader

php-s11n-object-reader parameter

Sets a procedure to call for unserializing any PHP object encountered in the input. The procedure will be invoked with one argument, PORT.

The parameter defaults to php-s11n-read-object.

Output

php-s11n-write

(php-s11n-write VALUE [PORT]) procedure

Writes a serialized PHP representation of VALUE to PORT, which defaults to the value of (current-output-port).

Input

php-s11n-read

(php-s11n-read [PORT]) procedure

Reads a serialized PHP literal from PORT and returns it as a Scheme value. PORT defaults to the value of (current-input-port).

php-s11n-read-null

(php-s11n-read-null PORT) procedure

Reads a serialized PHP NULL literal from PORT, returning (void).

php-s11n-read-boolean

(php-s11n-read-boolean PORT) procedure

Reads a serialized PHP boolean literal from PORT, returning #t or #f.

php-s11n-read-integer

(php-s11n-read-integer PORT) procedure

Reads a serialized PHP integer literal from PORT, returning a Scheme integer. The string representation of the integer is converted to a number using string->number.

php-s11n-read-float

(php-s11n-read-float PORT) procedure

Reads a serialized PHP float literal from PORT, returning a Scheme flonum or the value +nan. The string representation of the integer is converted to a number using string->number.

php-s11n-read-string

(php-s11n-read-string PORT) procedure

Reads a serialized PHP string literal from PORT, returning a Scheme string.

php-s11n-read-array

(php-s11n-read-array PORT) procedure

Reads a serialized PHP array from PORT, returning either a Scheme vector or association list.

php-s11n-read-array/alist

(php-s11n-read-array/alist PORT) procedure

Reads a serialized PHP associative array from PORT, returning a Scheme association list.

php-s11n-read-array/hash-table

(php-s11n-read-array/hash-table PORT) procedure

Reads a serialized PHP associative array from PORT, returning a SRFI-69 hash table object.

php-s11n-read-object

(php-s11n-read-object PORT) procedure

Reads a serialized PHP object from PORT, raising an error. Use the parameter php-s11n-object-reader to override this functionality if you need to unserialize PHP object types.

License

 Copyright (c) 2006-2007 Arto Bendiken.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 IN THE SOFTWARE.

Version history

1.0.0
Initial release of the php-s11n egg.

Contents »