A Clojure library to deserialize PHP as generated by
serialize
into Clojure
data structures and vice versa.
Current version: 0.4.1
Supported Clojure versions: 1.4, 1.5, 1.5.1
(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\";}"
As PHP's Arrays are actually ordered maps, converting an array such as:
array(
"name" => "Bob",
"age" => 42
)
Will result in an ordered map equivalent to the following:
{"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") hence
the use of the ordered-map
type.
Arrays with consecutive indices starting at 0 such as
array(
0 => "a",
1 => "b",
2 => "c"
)
Will be converted into vectors like so:
["a" "b" "c"]
php-clj is available on Clojars, add the following to your Leiningen dependencies:
[php-clj "0.4.1"]
- Arto Bendiken's
php-s11n
; - Brad Koch's Stack Overflow discussion "Parsing serialized PHP data with BNF using Instaparse".
Copyright © 2014 Paul Mucur.
Distributed under the Eclipse Public License.