
Enables PHP serialization format support, facilitating encoding and decoding of various types like strings, integers, and custom classes. Supports comprehensive type compatibility, excluding sets and partial sealed class support.
This is a Kotlin Multiplatform library that provides PHP serialization format support for kotlinx.serialization.
Add the following dependency to your build.gradle.kts or build.gradle file:
dependencies {
implementation("com.jsoizo:kotlinx-serialization-php:0.2.1")
}encoding
@Serializable
data class User(val id: Int, val name: String)
val user = User(id = 100, name = "John")
val encoded = PHP.encodeToString(user)
// "O:4:\"User\":2:{s:2:\"id\";i:100;s:4:\"name\";s:4:\"John\";}"decoding
val encoded = "O:4:\"User\":2:{s:2:\"id\";i:100;s:4:\"name\";s:4:\"John\";}"
val decodedUser = PHP.decodeFromString<User>(encoded)
// User(id=100, name="John")To customize the behavior, build a configured instance with the PHP {} builder:
val php = PHP {
// Skip fields in the PHP data that the Kotlin class does not declare
// (default: false, unknown fields raise SerializationException)
ignoreUnknownKeys = true
// Register contextual/custom serializers
serializersModule = SerializersModule { /* ... */ }
}
val decoded = php.decodeFromString<User>(encoded)PHP serializes protected properties as "\0*\0name" and private ones as
"\0ClassName\0name"; these are automatically matched to the Kotlin property
by their bare name when decoding.
To see more usage and examples, please refer to the kotlinx.serialization documentation
Kotlin types that can be serialized to and deserialized from PHP serialization format.
| Kotlin type | Supported | PHP Serialization type |
|---|---|---|
String |
✅ | s |
Char |
✅ | s |
Int |
✅ | i |
Long |
✅ | i |
Float |
✅ | d |
Double |
✅ | d |
Boolean |
✅ | b |
List |
✅ | a |
Map |
✅ | a |
Set |
🚫 | |
class with @Serializable |
✅ | O |
enum class |
✅ | E |
sealed class |
O |
|
Nullable |
✅ | N |
Float.NaN and infinities are mapped to PHP's NAN / INF / -INF in both
directions. Map keys must be Int, Long, String or Char because PHP
array keys can only be integers or strings; other key types fail with
SerializationException.
App\Models\User.R: / r: (references) or C: (classes implementing
Serializable) cannot be decoded.a:2:{i:5;...;i:9;...;}) lose their indices.
Decode such data into a Map instead."123" to
integers when serializing; such keys cannot be decoded into a
Map<String, V> (use Map<Long, V> when keys may be numeric).This is a Kotlin Multiplatform library that provides PHP serialization format support for kotlinx.serialization.
Add the following dependency to your build.gradle.kts or build.gradle file:
dependencies {
implementation("com.jsoizo:kotlinx-serialization-php:0.2.1")
}encoding
@Serializable
data class User(val id: Int, val name: String)
val user = User(id = 100, name = "John")
val encoded = PHP.encodeToString(user)
// "O:4:\"User\":2:{s:2:\"id\";i:100;s:4:\"name\";s:4:\"John\";}"decoding
val encoded = "O:4:\"User\":2:{s:2:\"id\";i:100;s:4:\"name\";s:4:\"John\";}"
val decodedUser = PHP.decodeFromString<User>(encoded)
// User(id=100, name="John")To customize the behavior, build a configured instance with the PHP {} builder:
val php = PHP {
// Skip fields in the PHP data that the Kotlin class does not declare
// (default: false, unknown fields raise SerializationException)
ignoreUnknownKeys = true
// Register contextual/custom serializers
serializersModule = SerializersModule { /* ... */ }
}
val decoded = php.decodeFromString<User>(encoded)PHP serializes protected properties as "\0*\0name" and private ones as
"\0ClassName\0name"; these are automatically matched to the Kotlin property
by their bare name when decoding.
To see more usage and examples, please refer to the kotlinx.serialization documentation
Kotlin types that can be serialized to and deserialized from PHP serialization format.
| Kotlin type | Supported | PHP Serialization type |
|---|---|---|
String |
✅ | s |
Char |
✅ | s |
Int |
✅ | i |
Long |
✅ | i |
Float |
✅ | d |
Double |
✅ | d |
Boolean |
✅ | b |
List |
✅ | a |
Map |
✅ | a |
Set |
🚫 | |
class with @Serializable |
✅ | O |
enum class |
✅ | E |
sealed class |
O |
|
Nullable |
✅ | N |
Float.NaN and infinities are mapped to PHP's NAN / INF / -INF in both
directions. Map keys must be Int, Long, String or Char because PHP
array keys can only be integers or strings; other key types fail with
SerializationException.
App\Models\User.R: / r: (references) or C: (classes implementing
Serializable) cannot be decoded.a:2:{i:5;...;i:9;...;}) lose their indices.
Decode such data into a Map instead."123" to
integers when serializing; such keys cannot be decoded into a
Map<String, V> (use Map<Long, V> when keys may be numeric).