Skip to content

Repository files navigation

SerializeKit

SerializeKit reads and writes values compatible with PHP's native serialize() and unserialize() format.

Swift Package Manager License

Requirements

  • Swift 5.8+ (Xcode 15+)
  • iOS 16+, macOS 13+, watchOS 9+, tvOS 16+

Installation (Package.swift)

dependencies: [
    .package(url: "https://github.com/0xWDG/SerializeKit.git", branch: "main"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "SerializeKit", package: "SerializeKit"),
    ]),
]

Usage

Swift literals convert directly to PHPValue. PHP arrays retain their ordered integer or string keys.

import SerializeKit

let value: PHPValue = [
    "name": "Wesley",
    "active": true,
    "scores": [10, 20, 30],
]

let serialized = try PHPSerializer.serializeString(value)
// a:3:{s:4:"name";s:6:"Wesley";s:6:"active";b:1;s:6:"scores";a:3:{i:0;i:10;i:1;i:20;i:2;i:30;}}

let restored = try PHPSerializer.unserialize(serialized)

Codable

Codable models can be serialized directly. CodingKeys, nested models, optionals, arrays, dictionaries, Data, and Date are supported.

struct User: Codable {
    let id: Int64
    let name: String
    let roles: [String]
}

let user = User(id: 42, name: "Wesley", roles: ["admin"])
let data = try PHPSerializer.serialize(user)
let restored = try PHPSerializer.unserialize(User.self, from: data)

Data is encoded as a binary PHP string. Date uses seconds relative to Apple's reference date, matching its default Codable representation. Codable keyed containers use their coding keys, while unkeyed containers use sequential integer keys.

PHP strings are binary strings and can contain invalid UTF-8 or null bytes. Use the Data API for lossless binary values:

import Foundation
import SerializeKit

let binary = PHPValue.string(PHPString(data: Data([0x00, 0xFF])))
let data = PHPSerializer.serialize(binary)
let restored = try PHPSerializer.unserialize(data)

Supported values are null, booleans, 64-bit integers, doubles, binary strings, arrays, objects, and PHP enumerations. Objects are represented as inert class/property data; SerializeKit never loads a PHP class or executes object hooks. PHP references and legacy custom-serialized objects are rejected.

Contact

πŸ¦‹ @0xWDG 🐘 mastodon.social/@0xWDG 🐦 @0xWDG 🧡 @0xWDG 🌐 wesleydegroot.nl πŸ€– Discord

Interested learning more about Swift? Check out my blog.

Releases

Sponsor this project

Packages

Contributors

Languages