-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for custom serializer (#179)
* dev: support custom serializer * chores: revert minSdk to 21 * chores: fix version to 2.0.0 * refacto: Making change as non-breaking chores: downgrade version to 1.8.0 as change is no longer breaking + adding readme * chores: adding notice to implement kotlinx for default EmojiInitializer * chores: spotlessApply * chores: Adding example for Jackson and Gson + revert version.properties * chores: rename AEmojiInitializer to AbstractEmojiInitializer * doc: Doc for KotlinxDeserializer.kt --------- Co-authored-by: Yoobi <>
- Loading branch information
Showing
11 changed files
with
285 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/io/wax911/emojifysample/CustomEmojiInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.wax911.emojifysample | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import io.wax911.emojify.initializer.AbstractEmojiInitializer | ||
import io.wax911.emojify.initializer.IEmojiDeserializer | ||
import io.wax911.emojify.model.Emoji | ||
import okio.buffer | ||
import okio.source | ||
import java.io.InputStream | ||
|
||
class CustomEmojiInitializer: AbstractEmojiInitializer() { | ||
class MoshiDeserializer: IEmojiDeserializer { | ||
private val moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build() | ||
|
||
override fun decodeFromStream(inputStream: InputStream): List<Emoji> { | ||
val myType = Types.newParameterizedType(List::class.java, Emoji::class.java) | ||
return moshi.adapter<List<Emoji>>(myType).fromJson(inputStream.source().buffer()) ?: listOf() | ||
} | ||
} | ||
|
||
class JacksonDeserializer: IEmojiDeserializer { | ||
private val jackson = ObjectMapper() | ||
|
||
override fun decodeFromStream(inputStream: InputStream): List<Emoji> { | ||
val myType = jackson.typeFactory.constructCollectionType(List::class.java, Emoji::class.java) | ||
return jackson.readValue(inputStream, myType) | ||
} | ||
} | ||
|
||
class GsonDeserializer: IEmojiDeserializer { | ||
private val gson = Gson() | ||
|
||
override fun decodeFromStream(inputStream: InputStream): List<Emoji> { | ||
val myType = TypeToken.getParameterized(List::class.java, Emoji::class.java).type | ||
return gson.fromJson(inputStream.reader(), myType) | ||
} | ||
} | ||
|
||
override val serializer: IEmojiDeserializer = MoshiDeserializer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
emojify/src/main/kotlin/io/wax911/emojify/deserializer/KotlinxDeserializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2024 AniTrend | ||
* | ||
* 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 | ||
* | ||
* https://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 io.wax911.emojify.deserializer | ||
|
||
import io.wax911.emojify.initializer.IEmojiDeserializer | ||
import io.wax911.emojify.model.Emoji | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import java.io.InputStream | ||
|
||
/** | ||
* Default implementation for kotlinx-serialization | ||
*/ | ||
class KotlinxDeserializer : IEmojiDeserializer { | ||
private val json = Json { isLenient = true } | ||
|
||
override fun decodeFromStream(inputStream: InputStream): List<Emoji> { | ||
val deserializer = ListSerializer(Emoji.serializer()) | ||
return json.decodeFromStream(deserializer, inputStream) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
emojify/src/main/kotlin/io/wax911/emojify/initializer/AbstractEmojiInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2021 AniTrend | ||
* | ||
* 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 | ||
* | ||
* https://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 io.wax911.emojify.initializer | ||
|
||
import android.content.Context | ||
import android.content.res.AssetManager | ||
import androidx.startup.Initializer | ||
import io.wax911.emojify.EmojiManager | ||
import io.wax911.emojify.model.Emoji | ||
import kotlinx.serialization.SerializationException | ||
import java.io.IOException | ||
|
||
/** | ||
* Abstract the logic of Initializer<EmojiManager> so that | ||
*/ | ||
abstract class AbstractEmojiInitializer : Initializer<EmojiManager> { | ||
abstract val serializer: IEmojiDeserializer | ||
|
||
/** | ||
* Initializes emoji objects from an asset file in the library directory | ||
* | ||
* @param assetManager provide an assert manager | ||
* @param path location where emoji data can be found | ||
* | ||
* @throws IOException when the provided [assetManager] cannot open [path] | ||
* @throws SerializationException when an error occurs during deserialization | ||
*/ | ||
@Throws(IOException::class, SerializationException::class) | ||
fun initEmojiData( | ||
assetManager: AssetManager, | ||
path: String = DEFAULT_PATH, | ||
): List<Emoji> { | ||
return assetManager.open(path).use { inputStream -> | ||
serializer.decodeFromStream(inputStream) | ||
} | ||
} | ||
|
||
/** | ||
* Initializes and a component given the application [Context] | ||
* | ||
* @param context The application context. | ||
*/ | ||
override fun create(context: Context): EmojiManager { | ||
val emojiManagerDefault = EmojiManager(emptyList()) | ||
val result = | ||
runCatching { | ||
val emojis = initEmojiData(context.assets) | ||
EmojiManager(emojis) | ||
}.onFailure { it.printStackTrace() } | ||
return result.getOrNull() ?: emojiManagerDefault | ||
} | ||
|
||
/** | ||
* @return A list of dependencies that this [Initializer] depends on. This is | ||
* used to determine initialization order of [Initializer]s. | ||
* | ||
* For e.g. if a [Initializer] `B` defines another | ||
* [Initializer] `A` as its dependency, then `A` gets initialized before `B`. | ||
*/ | ||
override fun dependencies() = emptyList<Class<out Initializer<*>>>() | ||
|
||
companion object { | ||
/** | ||
* Default location with assets where emojis can be found | ||
*/ | ||
internal const val DEFAULT_PATH = "emoticons/emoji.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
emojify/src/main/kotlin/io/wax911/emojify/initializer/IEmojiDeserializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 AniTrend | ||
* | ||
* 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 | ||
* | ||
* https://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 io.wax911.emojify.initializer | ||
|
||
import io.wax911.emojify.model.Emoji | ||
import java.io.InputStream | ||
|
||
/** | ||
* Interface to implement if user wants to use a custom deserializer. | ||
* For more information on the necessary steps refer to README.md | ||
*/ | ||
interface IEmojiDeserializer { | ||
/** | ||
* Decodes the given [InputStream] to an object of type List<[Emoji]> | ||
*/ | ||
fun decodeFromStream(inputStream: InputStream): List<Emoji> | ||
} |
Oops, something went wrong.