-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove unnecessary serialization functions
- Loading branch information
1 parent
c666da0
commit 84c56dd
Showing
17 changed files
with
75 additions
and
158 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
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
49 changes: 15 additions & 34 deletions
49
...ace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/EmbraceSerializer.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 |
---|---|---|
@@ -1,66 +1,47 @@ | ||
package io.embrace.android.embracesdk.internal | ||
|
||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonIOException | ||
import com.google.gson.reflect.TypeToken | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonWriter | ||
import io.embrace.android.embracesdk.comms.api.EmbraceUrl | ||
import io.embrace.android.embracesdk.comms.api.EmbraceUrlAdapter | ||
import io.embrace.android.embracesdk.internal.utils.threadLocal | ||
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger | ||
import java.io.BufferedWriter | ||
import java.io.Reader | ||
import java.lang.reflect.Type | ||
import java.nio.charset.Charset | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
/** | ||
* A wrapper around Gson to allow for thread-safe serialization. | ||
*/ | ||
internal class EmbraceSerializer { | ||
|
||
private val gson by threadLocal { | ||
private val impl by threadLocal { | ||
GsonBuilder() | ||
.registerTypeAdapter(EmbraceUrl::class.java, EmbraceUrlAdapter()) | ||
.create() | ||
} | ||
|
||
fun <T> toJson(src: T): String { | ||
return gson.toJson(src) ?: throw JsonIOException("Failed converting object to JSON.") | ||
return impl.toJson(src) | ||
} | ||
|
||
fun <T> toJson(src: T, type: Type): String { | ||
return gson.toJson(src, type) | ||
?: throw JsonIOException("Failed converting object to JSON.") | ||
} | ||
|
||
fun <T> toJson(any: T, clazz: Class<T>, writer: BufferedWriter): Boolean { | ||
return try { | ||
gson.toJson(any, clazz, JsonWriter(writer)) | ||
true | ||
} catch (e: Exception) { | ||
InternalStaticEmbraceLogger.logDebug("cannot write to bufferedWriter", e) | ||
false | ||
fun <T> toJson(any: T, clazz: Class<T>, outputStream: OutputStream) { | ||
outputStream.writer().buffered().use { | ||
impl.toJson(any, clazz, JsonWriter(it)) | ||
} | ||
} | ||
|
||
fun <T> fromJson(json: String, type: Type): T { | ||
return gson.fromJson(json, type) | ||
} | ||
|
||
fun <T> fromJson(json: String, clz: Class<T>): T { | ||
return gson.fromJson(json, clz) | ||
return impl.fromJson(json, clz) | ||
} | ||
|
||
fun <T> fromJson(reader: Reader, clz: Class<T>): T { | ||
return gson.fromJson(reader, clz) | ||
} | ||
|
||
fun <T> fromJson(json: String): T { | ||
return gson.fromJson(json, object : TypeToken<T>() {}.type) | ||
fun <T> fromJson(inputStream: InputStream, clz: Class<T>): T { | ||
inputStream.bufferedReader().use { | ||
return impl.fromJson(JsonReader(it), clz) | ||
} | ||
} | ||
|
||
fun <T> bytesFromPayload(payload: T, clazz: Class<T>): ByteArray? { | ||
val json = toJson(payload, clazz) | ||
return json.toByteArray(Charset.forName("UTF-8")) | ||
fun <T> fromJsonWithTypeToken(json: String): T { | ||
return impl.fromJson(json, object : TypeToken<T>() {}.type) | ||
} | ||
} |
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
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
Oops, something went wrong.