Giter Club home page Giter Club logo

php_serializer's Introduction

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.

php_serializer's People

Contributors

jlgeering avatar kianmeng avatar ojizero avatar zloyrusskiy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.