-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEMPORARY FIX: allow "values" to by of type array or object in input api
- this is to conform with the old api - the old api accepted the value (no error), but didn't store what was expected: "value": [1,2,true] stored "value": "true" ... - here, at least we both accept AND store the correct value
- Loading branch information
Showing
3 changed files
with
49 additions
and
2 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
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/ch/derlin/bbdata/input/RawStringDeserializer.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,43 @@ | ||
package ch.derlin.bbdata.input | ||
|
||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonDeserializer | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
|
||
|
||
/** | ||
* | ||
* Problem: in the old api, people got used to send arrays or objects as "value", | ||
* while new version is expecting a string. | ||
* | ||
* This mapper always deserialize into strings. | ||
* The behavior is the same for values enclosed in quotes or basic values, it only changes for | ||
* raw arrays ("value": [...]) or objects (e.g. "value": {...}). | ||
* | ||
* Example: | ||
* - "value": [1, 2, false ] => "[1,2,false]" | ||
* - "value": {"a": 1, "b" : "x"} => "{\"a\":1,\"b\":\"x\"}", | ||
* - "value": "[1, 2,false]" => "[1, 2,false]" | ||
* - "value": 1 => "1.0" | ||
* | ||
* BEWARE: if not using quotes, the array or object MUST BE A CORRECT json array/object | ||
* | ||
* date: 30.09.20 | ||
* @author Lucy Linder <[email protected]> | ||
*/ | ||
class RawStringDeserializer : JsonDeserializer<String?>() { | ||
|
||
// basic objectMapper: no prettyPrint, nothing... | ||
private val objectMapper = ObjectMapper() | ||
|
||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext?): String? { | ||
// use default objectMapper for deserialization, so we keep the usual settings | ||
val node: JsonNode = (if (p.codec is ObjectMapper) p.codec else objectMapper).readTree(p) | ||
// Do not use the default objectMapper for serialization | ||
// since it may be configured with prettyPrint = true | ||
val ret = if (node.isTextual) node.asText() else objectMapper.writeValueAsString(node) | ||
return ret | ||
} | ||
} |