This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
convert middleware jackson to kotlin #359
Open
brianPlummer
wants to merge
1
commit into
feature/kotlin_conversion
Choose a base branch
from
feature/middlewareJacksonKotlinB
base: feature/kotlin_conversion
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
.../com/nytimes/android/external/store3/middleware/jackson/JacksonBufferedSourceAdapter.java
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
...va/com/nytimes/android/external/store3/middleware/jackson/JacksonBufferedSourceAdapter.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,16 @@ | ||
package com.nytimes.android.external.store3.middleware.jackson | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.nytimes.android.external.fs3.BufferedSourceAdapter | ||
import okio.BufferedSource | ||
import okio.Okio | ||
import java.io.ByteArrayInputStream | ||
import javax.inject.Inject | ||
|
||
/** | ||
* An implementation of [BufferedSourceAdapter] that uses [ObjectMapper] to convert Java values to JSON. | ||
*/ | ||
class JacksonBufferedSourceAdapter<Parsed> @Inject | ||
constructor(private val objectMapper: ObjectMapper) : BufferedSourceAdapter<Parsed> { | ||
override fun toJson(value: Parsed): BufferedSource = Okio.buffer(Okio.source(ByteArrayInputStream(objectMapper.writeValueAsBytes(value)))) | ||
} |
149 changes: 0 additions & 149 deletions
149
...ain/java/com/nytimes/android/external/store3/middleware/jackson/JacksonParserFactory.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
.../main/java/com/nytimes/android/external/store3/middleware/jackson/JacksonParserFactory.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,73 @@ | ||
package com.nytimes.android.external.store3.middleware.jackson | ||
|
||
import com.fasterxml.jackson.core.JsonFactory | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.KotlinModule | ||
import com.nytimes.android.external.store3.base.Parser | ||
|
||
import java.io.Reader | ||
import java.lang.reflect.Type | ||
|
||
import okio.BufferedSource | ||
|
||
/** | ||
* Factory which returns various Jackson [Parser] implementations. | ||
*/ | ||
object JacksonParserFactory { | ||
|
||
/** | ||
* Returns a new Parser which parses from a String to the specified type, using | ||
* the provided [JsonFactory] instance. | ||
*/ | ||
fun <T> createStringParser(jsonFactory: JsonFactory, type: Type): Parser<String, T> = JacksonStringParser(jsonFactory, type) | ||
|
||
/** | ||
* Returns a new Parser which parses from a String to the specified type, using | ||
* the provided [ObjectMapper] instance. | ||
*/ | ||
fun <T> createStringParser(objectMapper: ObjectMapper, type: Type): Parser<String, T> = JacksonStringParser(objectMapper, type) | ||
|
||
/** | ||
* Returns a new Parser which parses from a String to the specified type, using | ||
* a new default [ObjectMapper] instance. | ||
*/ | ||
fun <T> createStringParser(type: Class<T>): Parser<String, T> = createStringParser(ObjectMapper().registerModule(KotlinModule()), type) | ||
|
||
/** | ||
* Returns a new Parser which parses from [BufferedSource] to the specified type, using | ||
* the provided [JsonFactory] instance. | ||
*/ | ||
fun <T> createSourceParser(jsonFactory: JsonFactory, | ||
type: Type): Parser<BufferedSource, T> = JacksonSourceParser(jsonFactory, type) | ||
/** | ||
* Returns a new Parser which parses from [BufferedSource] to the specified type, using | ||
* the provided [ObjectMapper] instance. | ||
*/ | ||
fun <T> createSourceParser(objectMapper: ObjectMapper, | ||
type: Type): Parser<BufferedSource, T> = JacksonSourceParser(objectMapper, type) | ||
|
||
/** | ||
* Returns a new Parser which parses from [BufferedSource] to the specified type, using | ||
* a new default configured [ObjectMapper] instance. | ||
*/ | ||
fun <T> createSourceParser(type: Type): Parser<BufferedSource, T> = createSourceParser(ObjectMapper().registerModule(KotlinModule()), type) | ||
|
||
/** | ||
* Returns a new Parser which parses from [Reader] to the specified type, using | ||
* the provided [JsonFactory] instance. | ||
*/ | ||
fun <T> createReaderParser(jsonFactory: JsonFactory, | ||
type: Type): Parser<Reader, T> = JacksonReaderParser(jsonFactory, type) | ||
/** | ||
* Returns a new Parser which parses from [Reader] to the specified type, using | ||
* the provided [ObjectMapper] instance. | ||
*/ | ||
fun <T> createReaderParser(objectMapper: ObjectMapper, | ||
type: Type): Parser<Reader, T> = JacksonReaderParser(objectMapper, type) | ||
|
||
/** | ||
* Returns a new Parser which parses from [Reader] to the specified type, using | ||
* a new default configured [ObjectMapper] instance. | ||
*/ | ||
fun <T> createReaderParser(type: Type): Parser<Reader, T> = createReaderParser(ObjectMapper().registerModule(KotlinModule()), type) | ||
} |
44 changes: 0 additions & 44 deletions
44
...main/java/com/nytimes/android/external/store3/middleware/jackson/JacksonReaderParser.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
...c/main/java/com/nytimes/android/external/store3/middleware/jackson/JacksonReaderParser.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,33 @@ | ||
package com.nytimes.android.external.store3.middleware.jackson | ||
|
||
|
||
import com.fasterxml.jackson.core.JsonFactory | ||
import com.fasterxml.jackson.databind.JavaType | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.KotlinModule | ||
import com.nytimes.android.external.store3.base.Parser | ||
import com.nytimes.android.external.store3.util.ParserException | ||
import io.reactivex.annotations.NonNull | ||
import java.io.Reader | ||
import java.lang.reflect.Type | ||
import javax.inject.Inject | ||
|
||
class JacksonReaderParser<Parsed> : Parser<Reader, Parsed> { | ||
|
||
private val objectMapper: ObjectMapper | ||
private val parsedType: JavaType | ||
|
||
constructor(jsonFactory: JsonFactory, type: Type) { | ||
objectMapper = ObjectMapper(jsonFactory).registerModule(KotlinModule()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add |
||
parsedType = objectMapper.constructType(type) | ||
} | ||
|
||
@Inject | ||
constructor(objectMapper: ObjectMapper, type: Type) { | ||
this.objectMapper = objectMapper | ||
this.parsedType = objectMapper.constructType(type) | ||
} | ||
|
||
@Throws(ParserException::class) | ||
override fun apply(@NonNull reader: Reader): Parsed = objectMapper.readValue(reader, parsedType) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Java class was also containing a logic that would propagate a thrown exception! Is that deleted in purpose on the Kotlin conversion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! @tiwiz mentioned failing fast, not sure 100% if that is the desired direction. @digitalbuddha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would be a breaking change imo, leave the same as before pls