An open API service indexing awesome lists of open source software.

https://github.com/mudge/php-clj

Deserialize PHP into Clojure data structures and back again.
https://github.com/mudge/php-clj

Last synced: about 1 year ago
JSON representation

Deserialize PHP into Clojure data structures and back again.

Awesome Lists containing this project

README

          

# php-clj [![Build Status](https://travis-ci.org/mudge/php-clj.png?branch=master)](https://travis-ci.org/mudge/php-clj)

A Clojure library to deserialize PHP as generated by
[`serialize`](http://php.net/manual/en/function.serialize.php) into Clojure
data structures and vice versa.

**Current version**: 0.4.1
**Supported Clojure versions:** 1.4, 1.5, 1.5.1

## Usage

```clojure
(ns foo.bar
(:require [php_clj.core :refer [php->clj clj->php]]))

(php->clj "s:18:\"Café Scientifique\";")
;; => "Café Scientifique"

(php->clj "i:1337;")
;; => 1337

(php->clj "a:2:{s:4:\"Wöo\";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:3:\"Bar\";b:0;}")
;; => #ordered/map (["Wöo" [1 2 3]] ["Bar" false])

(clj->php "Café")
;; => "s:5:\"Café\";"

(clj->php {"name" "Bob"})
;; => "a:1:{s:4:\"name\";s:3:\"Bob\";}"
```

## A Note on PHP Arrays

As [PHP's Arrays](http://www.php.net/manual/en/language.types.array.php) are
actually ordered maps, converting an array such as:

```php
array(
"name" => "Bob",
"age" => 42
)
```

Will result in an [ordered
map](https://github.com/flatland/ordered) equivalent to the following:

```clojure
{"name" "Bob", "age" 42}
```

Note that Clojure's standard map implementation does not retain
insertion order (and [ArrayMaps are only suitable for "very small
maps"](http://clojure.org/data_structures#Data%20Structures-ArrayMaps)) hence
the use of the `ordered-map` type.

Arrays with consecutive indices starting at 0 such as

```php
array(
0 => "a",
1 => "b",
2 => "c"
)
```

Will be converted into vectors like so:

```clojure
["a" "b" "c"]
```

## Installation

php-clj is available on [Clojars](https://clojars.org/php-clj), add the
following to your [Leiningen](https://github.com/technomancy/leiningen)
dependencies:

```clojure
[php-clj "0.4.1"]
```

## References

* [Arto Bendiken's `php-s11n`](http://wiki.call-cc.org/eggref/4/php-s11n);
* [Brad Koch's Stack Overflow discussion "Parsing serialized PHP data with BNF
using
Instaparse"](http://stackoverflow.com/questions/18518499/parsing-serialized-php-data-with-bnf-using-instaparse).

## License

Copyright © 2014 Paul Mucur.

Distributed under the [Eclipse Public
License](http://www.eclipse.org/legal/epl-v10.html).