-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common: fix number deserialization in API Request Enrichment (close #383
- Loading branch information
1 parent
2b7d9d9
commit 1857866
Showing
8 changed files
with
88 additions
and
23 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
40 changes: 40 additions & 0 deletions
40
modules/common/src/main/scala/io/circe/jackson/enrich/JacksonParser.scala
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,40 @@ | ||
package io.circe.jackson.enrich | ||
|
||
import java.io.File | ||
|
||
import cats.data.ValidatedNel | ||
import io.circe.{Decoder, Error, Json, Parser, ParsingFailure} | ||
|
||
import scala.util.control.NonFatal | ||
|
||
trait JacksonParser extends Parser { this: WithJacksonMapper => | ||
final def parse(input: String): Either[ParsingFailure, Json] = | ||
try Right(mapper.readValue(jsonStringParser(input), classOf[Json])) | ||
catch { | ||
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error)) | ||
} | ||
|
||
final def parseFile(file: File): Either[ParsingFailure, Json] = | ||
try Right(mapper.readValue(jsonFileParser(file), classOf[Json])) | ||
catch { | ||
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error)) | ||
} | ||
|
||
final def parseByteArray(bytes: Array[Byte]): Either[ParsingFailure, Json] = | ||
try Right(mapper.readValue(jsonBytesParser(bytes), classOf[Json])) | ||
catch { | ||
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error)) | ||
} | ||
|
||
final def decodeByteArray[A: Decoder](bytes: Array[Byte]): Either[Error, A] = | ||
finishDecode[A](parseByteArray(bytes)) | ||
|
||
final def decodeByteArrayAccumulating[A: Decoder](bytes: Array[Byte]): ValidatedNel[Error, A] = | ||
finishDecodeAccumulating[A](parseByteArray(bytes)) | ||
|
||
final def decodeFile[A: Decoder](file: File): Either[Error, A] = | ||
finishDecode[A](parseFile(file)) | ||
|
||
final def decodeFileAccumulating[A: Decoder](file: File): ValidatedNel[Error, A] = | ||
finishDecodeAccumulating[A](parseFile(file)) | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/common/src/main/scala/io/circe/jackson/enrich/WithJacksonMapper.scala
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,18 @@ | ||
package io.circe.jackson.enrich | ||
|
||
import java.io.{File, Writer} | ||
|
||
import com.fasterxml.jackson.core.{JsonFactory, JsonParser} | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.snowplowanalytics.snowplow.enrich.common.utils.CirceUtils | ||
|
||
class WithJacksonMapper { | ||
final val mapper: ObjectMapper = CirceUtils.mapper | ||
private[this] final val jsonFactory: JsonFactory = new JsonFactory(mapper) | ||
|
||
protected final def jsonStringParser(input: String): JsonParser = jsonFactory.createParser(input) | ||
protected final def jsonFileParser(file: File): JsonParser = jsonFactory.createParser(file) | ||
protected final def jsonBytesParser(bytes: Array[Byte]): JsonParser = | ||
jsonFactory.createParser(bytes) | ||
protected final def jsonGenerator(out: Writer) = jsonFactory.createGenerator(out) | ||
} |
3 changes: 3 additions & 0 deletions
3
modules/common/src/main/scala/io/circe/jackson/enrich/package.scala
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,3 @@ | ||
package io.circe.jackson | ||
|
||
package object enrich extends WithJacksonMapper with JacksonParser with JacksonCompat {} |
18 changes: 18 additions & 0 deletions
18
modules/common/src/test/resources/iglu-schemas/schemas/com.acme/geo/jsonschema/1-0-0
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,18 @@ | ||
{ | ||
"$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#", | ||
"description": "Schema for acme stuff", | ||
"self": { | ||
"vendor": "com.acme", | ||
"name": "geo", | ||
"format": "jsonschema", | ||
"version": "1-0-0" | ||
}, | ||
"type": "object", | ||
"properties": { | ||
"latitude": { | ||
"type": "number" | ||
} | ||
}, | ||
"required": ["latitude"], | ||
"additionalProperties": false | ||
} |
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