Skip to content

Commit 0d6505c

Browse files
authored
ObjectMapper can be reset to default (#1252)
1 parent e627d50 commit 0d6505c

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

play-json/jvm/src/main/scala/play/api/libs/json/jackson/JacksonJson.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ private[play] case class JacksonJson(jsonConfig: JsonConfig) {
286286
.streamReadConstraints(jsonConfig.streamReadConstraints)
287287
.streamWriteConstraints(jsonConfig.streamWriteConstraints)
288288
.build()
289-
private[play] var mapper: ObjectMapper = JsonMapper
289+
private var currentMapper: ObjectMapper = null
290+
private val defaultMapper: ObjectMapper = JsonMapper
290291
.builder(jsonFactory)
291292
.addModules(
292293
new ParameterNamesModule(),
@@ -301,21 +302,27 @@ private[play] case class JacksonJson(jsonConfig: JsonConfig) {
301302
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
302303
.build()
303304

305+
private[play] def mapper(): ObjectMapper = if (currentMapper == null) {
306+
defaultMapper
307+
} else {
308+
currentMapper
309+
}
310+
304311
private[play] def setObjectMapper(mapper: ObjectMapper): Unit = {
305-
this.mapper = mapper
312+
this.currentMapper = mapper
306313
}
307314

308315
private def stringJsonGenerator(out: java.io.StringWriter) =
309316
jsonFactory.createGenerator(out)
310317

311318
def parseJsValue(data: Array[Byte]): JsValue =
312-
mapper.readValue(jsonFactory.createParser(data), classOf[JsValue])
319+
mapper().readValue(jsonFactory.createParser(data), classOf[JsValue])
313320

314321
def parseJsValue(input: String): JsValue =
315-
mapper.readValue(jsonFactory.createParser(input), classOf[JsValue])
322+
mapper().readValue(jsonFactory.createParser(input), classOf[JsValue])
316323

317324
def parseJsValue(stream: InputStream): JsValue =
318-
mapper.readValue(jsonFactory.createParser(stream), classOf[JsValue])
325+
mapper().readValue(jsonFactory.createParser(stream), classOf[JsValue])
319326

320327
private def withStringWriter[T](f: StringWriter => T): T = {
321328
val sw = new StringWriter()
@@ -341,7 +348,7 @@ private[play] case class JacksonJson(jsonConfig: JsonConfig) {
341348
gen.enable(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature)
342349
}
343350

344-
mapper.writeValue(gen, jsValue)
351+
mapper().writeValue(gen, jsValue)
345352
sw.flush()
346353
sw.getBuffer.toString
347354
}
@@ -350,19 +357,19 @@ private[play] case class JacksonJson(jsonConfig: JsonConfig) {
350357
val gen = stringJsonGenerator(sw).setPrettyPrinter(
351358
new DefaultPrettyPrinter()
352359
)
353-
val writer: ObjectWriter = mapper.writerWithDefaultPrettyPrinter()
360+
val writer: ObjectWriter = mapper().writerWithDefaultPrettyPrinter()
354361

355362
writer.writeValue(gen, jsValue)
356363
sw.flush()
357364
sw.getBuffer.toString
358365
}
359366

360367
def jsValueToBytes(jsValue: JsValue): Array[Byte] =
361-
mapper.writeValueAsBytes(jsValue)
368+
mapper().writeValueAsBytes(jsValue)
362369

363370
def jsValueToJsonNode(jsValue: JsValue): JsonNode =
364-
mapper.valueToTree(jsValue)
371+
mapper().valueToTree(jsValue)
365372

366373
def jsonNodeToJsValue(jsonNode: JsonNode): JsValue =
367-
mapper.treeToValue(jsonNode, classOf[JsValue])
374+
mapper().treeToValue(jsonNode, classOf[JsValue])
368375
}

0 commit comments

Comments
 (0)