Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

convert middleware jackson to kotlin #359

Open
wants to merge 1 commit into
base: feature/kotlin_conversion
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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

constructor(private val objectMapper: ObjectMapper) : BufferedSourceAdapter<Parsed> {
override fun toJson(value: Parsed): BufferedSource = Okio.buffer(Okio.source(ByteArrayInputStream(objectMapper.writeValueAsBytes(value))))
}

This file was deleted.

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)
}

This file was deleted.

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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add this. here as well, to match the @Inject annotated constructor as well?

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)
}
Loading