Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #24
- Loading branch information
Showing
7 changed files
with
105 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Releases | ||
|
||
* [0.3.1](changes_0.3.1.md) | ||
* [0.3.0](changes_0.3.0.md) | ||
* [0.2.0](changes_0.2.0.md) | ||
* [0.1.0](changes_0.1.0.md) |
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,20 @@ | ||
# Import Export UDF Common Scala 0.3.1, released 2021-10-14 | ||
|
||
Code name: Fixed JSON Mapper | ||
|
||
## Summary | ||
|
||
This release fixes parsing bugs in JSON mapper functionality. | ||
|
||
## Bug Fixes | ||
|
||
* #24: Fixed JSON mapper issues | ||
|
||
## Dependency Updates | ||
|
||
### Runtime Dependency Updates | ||
|
||
### Test Dependency Updates | ||
|
||
### Plugin Updates | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,67 @@ | ||
package com.exasol.common.json | ||
|
||
import com.fasterxml.jackson.core.`type`.TypeReference | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.MapperFeature | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.databind.json.{JsonMapper => BaseJsonMapper} | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
import com.fasterxml.jackson.module.scala.ClassTagExtensions | ||
|
||
/** | ||
* JSON parsing helper object class. | ||
*/ | ||
object JsonMapper { | ||
|
||
private[this] val mapper = BaseJsonMapper | ||
.builder() | ||
.addModule(DefaultScalaModule) | ||
.build() | ||
private[this] class ScalaJsonMapper(jsonMapper: BaseJsonMapper) | ||
extends BaseJsonMapper(jsonMapper) | ||
with ClassTagExtensions | ||
|
||
private[this] val mapper = { | ||
val builder = BaseJsonMapper | ||
.builder() | ||
.findAndAddModules() | ||
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS) | ||
.enable(JsonParser.Feature.ALLOW_COMMENTS) | ||
.defaultMergeable(true) | ||
new ScalaJsonMapper(builder.build()) | ||
} | ||
|
||
/** | ||
* Parses given value type into a JSON string. | ||
* | ||
* For parsing into pretty indented JSON use {@link toPrettyJson}. | ||
* | ||
* @param value a provided value type | ||
* @return JSON string | ||
*/ | ||
def toJson[T](value: T): String = | ||
mapper.writeValueAsString(value) | ||
|
||
/** | ||
* Parses given value into pretty JSON format. | ||
* | ||
* @param value a provided value type | ||
* @return pretty JSON string | ||
*/ | ||
def toPrettyJson[T](value: T): String = | ||
mapper.writer(SerializationFeature.INDENT_OUTPUT).writeValueAsString(value) | ||
|
||
/** | ||
* Parses JSON string into a type. | ||
* | ||
* @param jsonString a JSON string | ||
* @return parsed value | ||
*/ | ||
def fromJson[T: Manifest](jsonString: String): T = | ||
mapper.readValue[T](jsonString) | ||
|
||
/** | ||
* Parses JSON string into a type. | ||
* | ||
* @param jsonString a JSON string | ||
* @return parsed value | ||
*/ | ||
@deprecated("Use fromJson method instead.", "0.3.1") | ||
def parseJson[T: Manifest](jsonString: String): T = | ||
mapper.readValue[T](jsonString, new TypeReference[T]() {}) | ||
fromJson(jsonString) | ||
|
||
} |
29 changes: 27 additions & 2 deletions
29
src/test/scala/com/exasol/common/json/JsonMapperTest.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 |
---|---|---|
@@ -1,14 +1,39 @@ | ||
package com.exasol.common.json | ||
|
||
import java.util.LinkedHashMap | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class JsonMapperTest extends AnyFunSuite { | ||
|
||
test("parse to and from json to internal data type") { | ||
test("parses to/from JSON to internal Scala data type") { | ||
val jsonStr = """{"name":"John","age":30,"skills":["a","b","c"]}""" | ||
val map = JsonMapper.parseJson[Map[String, Any]](jsonStr) | ||
val map = JsonMapper.fromJson[Map[String, Any]](jsonStr) | ||
assert(map.get("skills") === Option(Seq("a", "b", "c"))) | ||
assert(JsonMapper.toJson(map) === jsonStr) | ||
} | ||
|
||
test("parses to/from JSON to JsonNode") { | ||
val jsonStr = | ||
"""|{ | ||
| "number" : 1, | ||
| "record" : { | ||
| "field1" : "value1", | ||
| "field2" : 23 | ||
| } | ||
|}""".stripMargin | ||
val jsonNode = JsonMapper.fromJson[JsonNode](jsonStr) | ||
assert(JsonMapper.toPrettyJson(jsonNode) === jsonStr) | ||
} | ||
|
||
test("parses to/from JSON to Java LinkedHashMap") { | ||
val jsonStr = """{"name":"John","age":"ten"}""" | ||
val expected = new LinkedHashMap[String, String](java.util.Map.of("name", "John", "age", "ten")) | ||
val parsedMap = JsonMapper.fromJson[LinkedHashMap[String, String]](jsonStr) | ||
assert(parsedMap === expected) | ||
assert(JsonMapper.toJson(parsedMap) === jsonStr) | ||
} | ||
|
||
} |