Skip to content

zloyrusskiy/php_serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PhpSerializer

Module Version Hex Docs Total Download License Last Updated

PHP serialize/unserialize support for Elixir

Installation

The package can be installed by adding :php_serializer to your list of dependencies in mix.exs:

def deps do
  [
    {:php_serializer, "~> 2.0"}
  ]
end

Examples

serialize/1

PhpSerializer.serialize(123)
# "i:123;"
PhpSerializer.serialize([1,2,3])
# "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
PhpSerializer.serialize([1, :some_atom, %{1=> "a", "b" => 2}])
# "a:3:{i:0;i:1;i:1;s:9:\"some_atom\";i:2;a:2:{i:1;s:1:\"a\";s:1:\"b\";i:2;}}"

unserialize/1 By default mimics PHP behavior (ignoring excess part of input string)

PhpSerializer.unserialize("i:0;i:34;")
# {:ok, 0}
PhpSerializer.unserialize("a:3:{i:0;i:1;i:1;s:9:\"some_atom\";i:2;a:2:{i:1;s:1:\"a\";s:1:\"b\";i:2;}}")
# {:ok, [{0, 1}, {1, "some_atom"}, {2, [{1, "a"}, {"b", 2}]}]}

With options

PhpSerializer.unserialize("a:3:{i:0;i:1;i:1;s:9:\"some_atom\";i:2;a:2:{i:1;s:1:\"a\";s:1:\"b\";i:2;}}", array_to_map: true)
# {:ok, %{0 => 1, 1 => "some_atom", 2 => %{1 => "a", "b" => 2}}}
PhpSerializer.unserialize("i:0;i:34;", with_excess: true)
# {:ok, 0, "i:34;"}

strict mode

PhpSerializer.unserialize("i:0;")
# {:ok, 0}
PhpSerializer.unserialize("i:0;i:34;", strict: true)
# {:error, "excess characters found"}
PhpSerializer.unserialize("i:0;i:34;", strict: true, with_excess: true)
# {:error, "excess characters found", "i:34;"}

Copyright and License

Copyright (c) 2017 Alexander Fyodorov [email protected]

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.

About

PHP serialize/unserialize functional for Elixir lang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages