Skip to content

Commit

Permalink
Implement filter considerations
Browse files Browse the repository at this point in the history
  • Loading branch information
masato1230 committed Dec 28, 2022
1 parent cd3d450 commit 886c8d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package jp.co.tokubai.docpuree.serializer

import com.cookpad.puree.PureeFilter
import com.cookpad.puree.PureeSerializer
import com.google.gson.Gson
import jp.co.tokubai.docpuree.model.SerializedClassInfo
import jp.co.tokubai.docpuree.source.LogHistorySource

class DocPureeGsonSerializer(private val gson: Gson) : PureeSerializer {
class DocPureeGsonSerializer(
private val gson: Gson,
private val filters: List<PureeFilter>,
) : PureeSerializer {

override fun serialize(log: Any): String {
val json = gson.toJson(log)
LogHistorySource.serializedClassHistory.add(SerializedClassInfo(log.javaClass, json))

var filteredJson = json
filters.forEach { filter ->
filteredJson = filter.apply(filteredJson)
}
LogHistorySource.serializedClassHistory.add(SerializedClassInfo(log.javaClass, filteredJson))

return json
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package jp.co.tokubai.docpuree.serializer

import com.cookpad.puree.PureeFilter
import com.cookpad.puree.PureeSerializer
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import jp.co.tokubai.docpuree.model.SerializedClassInfo
import jp.co.tokubai.docpuree.source.LogHistorySource

class DocPureeMoshiSerializer : PureeSerializer {
class DocPureeMoshiSerializer(private val filters: List<PureeFilter>) : PureeSerializer {

private val moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()

override fun serialize(value: Any): String {
val adapter = moshi.adapter(value.javaClass)
val json = adapter.toJson(value)
LogHistorySource.serializedClassHistory.add(SerializedClassInfo(value.javaClass, json))

var filteredJson = json
filters.forEach { filter ->
filteredJson = filter.apply(filteredJson)
}

LogHistorySource.serializedClassHistory.add(SerializedClassInfo(value.javaClass, filteredJson))

return json
}
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/jp/co/tokubai/docpuree/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class App : Application() {
val gson = GsonBuilder().create()

return PureeConfiguration.Builder(context)
.pureeSerializer(DocPureeGsonSerializer(gson))
.pureeSerializer(DocPureeGsonSerializer(gson, emptyList()))
.register(
logClass = OnClickFirst::class.java,
output = output,
Expand Down

0 comments on commit 886c8d6

Please sign in to comment.