Added Path serializer, set public access for the different serializers used (which should be shared)

master Version_2.4
Robinson 2021-08-22 21:48:43 -06:00
parent 1f4923e189
commit 944b451da2
7 changed files with 91 additions and 12 deletions

View File

@ -11,7 +11,7 @@ Maven Info
<dependency>
<groupId>com.dorkbox</groupId>
<artifactId>Serializers</artifactId>
<version>2.3</version>
<version>2.4</version>
</dependency>
</dependencies>
```

View File

@ -38,7 +38,7 @@ object Extras {
// set for the project
const val description = "Kryo based serializers"
const val group = "com.dorkbox"
const val version = "2.3"
const val version = "2.4"
// set as project.ext
const val name = "Serializers"

View File

@ -25,6 +25,10 @@ import java.io.File
* Serialize the path of a file instead of the File object
*/
class FileSerializer : Serializer<File>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, file: File) {
output.writeString(file.path)
}

View File

@ -8,6 +8,9 @@ import java.net.Inet4Address
import java.net.InetAddress
class Inet4AddressSerializer : Serializer<Inet4Address>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, inetAddress: Inet4Address) {
output.writeBytes(inetAddress.address)

View File

@ -8,6 +8,9 @@ import java.net.Inet6Address
import java.net.InetAddress
class Inet6AddressSerializer : Serializer<Inet6Address>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, inetAddress: Inet6Address) {
output.writeBytes(inetAddress.address) // 16 bytes

View File

@ -0,0 +1,42 @@
/*
* Copyright 2021 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.serializers
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.nio.file.Path
import java.nio.file.Paths
/**
* Serialize the path of a file instead of the Path object
*/
class PathSerializer : Serializer<Path>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, path: Path) {
output.writeString(path.toString())
}
override fun read(kryo: Kryo, input: Input, type: Class<out Path>): Path {
val path = input.readString()
return Paths.get(path)
}
}

View File

@ -16,23 +16,45 @@
package dorkbox.serializers
import com.esotericsoftware.kryo.Kryo
import java.io.File
import java.io.IOException
import java.math.BigDecimal
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.URI
import java.nio.file.Path
import java.util.*
import java.util.regex.Pattern
@Suppress("MemberVisibilityCanBePrivate")
object SerializationDefaults {
/**
* Gets the version number.
*/
const val version = "2.3"
const val version = "2.4"
init {
// Add this project to the updates system, which verifies this class + UUID + version information
dorkbox.updates.Updates.add(SerializationDefaults::class.java, "316353f5338341a8a3edc01d702703f8", version)
}
val regexSerializer by lazy { RegexSerializer() }
val uriSerializer by lazy { URISerializer() }
val uuidSerializer by lazy { UUIDSerializer() }
val collectionsSingletonMapSerializer by lazy { CollectionsSingletonMapSerializer() }
val collectionsSingletonListSerializer by lazy { CollectionsSingletonListSerializer() }
val collectionsSingletonSetSerializer by lazy { CollectionsSingletonSetSerializer() }
val enumSetSerializer by lazy { EnumSetSerializer() }
val enumMapSerializer by lazy { EnumMapSerializer() }
val arraysAsListSerializer by lazy { ArraysAsListSerializer() }
val inet4AddressSerializer by lazy { Inet4AddressSerializer() }
val inet6AddressSerializer by lazy { Inet6AddressSerializer() }
val fileSerializer by lazy { FileSerializer() }
val pathSerializer by lazy { PathSerializer() }
/**
* Allows for the kryo registration of sensible defaults in a common, well-used way.
*/
@ -74,9 +96,9 @@ object SerializationDefaults {
kryo.register(HashMap::class.java)
kryo.register(HashSet::class.java)
kryo.register(EnumSet::class.java, EnumSetSerializer())
kryo.register(EnumMap::class.java, EnumMapSerializer())
kryo.register(Arrays.asList("").javaClass, ArraysAsListSerializer())
kryo.register(EnumSet::class.java, enumSetSerializer)
kryo.register(EnumMap::class.java, enumMapSerializer)
kryo.register(Arrays.asList("").javaClass, arraysAsListSerializer)
kryo.register(emptyList<Any>().javaClass)
kryo.register(emptySet<Any>().javaClass)
@ -88,13 +110,18 @@ object SerializationDefaults {
kryo.register(Collections.emptyNavigableSet<Any>().javaClass)
kryo.register(Collections.emptyNavigableMap<Any, Any>().javaClass)
kryo.register(Collections.singletonMap("", "").javaClass, CollectionsSingletonMapSerializer())
kryo.register(listOf("").javaClass, CollectionsSingletonListSerializer())
kryo.register(setOf("").javaClass, CollectionsSingletonSetSerializer())
kryo.register(Collections.singletonMap("", "").javaClass, collectionsSingletonMapSerializer)
kryo.register(listOf("").javaClass, collectionsSingletonListSerializer)
kryo.register(setOf("").javaClass, collectionsSingletonSetSerializer)
kryo.register(Pattern::class.java, RegexSerializer())
kryo.register(URI::class.java, URISerializer())
kryo.register(UUID::class.java, UUIDSerializer())
kryo.register(Pattern::class.java, regexSerializer)
kryo.register(URI::class.java, uriSerializer)
kryo.register(UUID::class.java, uuidSerializer)
kryo.register(Inet4Address::class.java, inet4AddressSerializer)
kryo.register(Inet6Address::class.java, inet6AddressSerializer)
kryo.register(File::class.java, fileSerializer)
kryo.register(Path::class.java, pathSerializer)
UnmodifiableCollectionsSerializer.registerSerializers(kryo)
SynchronizedCollectionsSerializer.registerSerializers(kryo)