Commit 49fc7277 authored by Phillip Webb's avatar Phillip Webb

Lazy initialize JacksonJsonParser ObjectMapper

Update `JacksonJsonParser` to that the `ObjectMapper` is only
initialized on first use. This performance optimization helps with
startup times if nothing uses the parser.

Fixes gh-8074
parent cfd5a73b
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -34,12 +34,12 @@ public class JacksonJsonParser implements JsonParser { ...@@ -34,12 +34,12 @@ public class JacksonJsonParser implements JsonParser {
private static final TypeReference<?> LIST_TYPE = new ListTypeReference(); private static final TypeReference<?> LIST_TYPE = new ListTypeReference();
private final ObjectMapper objectMapper = new ObjectMapper(); private ObjectMapper objectMapper; // Late binding
@Override @Override
public Map<String, Object> parseMap(String json) { public Map<String, Object> parseMap(String json) {
try { try {
return this.objectMapper.readValue(json, MAP_TYPE); return getObjectMapper().readValue(json, MAP_TYPE);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalArgumentException("Cannot parse JSON", ex); throw new IllegalArgumentException("Cannot parse JSON", ex);
...@@ -49,13 +49,20 @@ public class JacksonJsonParser implements JsonParser { ...@@ -49,13 +49,20 @@ public class JacksonJsonParser implements JsonParser {
@Override @Override
public List<Object> parseList(String json) { public List<Object> parseList(String json) {
try { try {
return this.objectMapper.readValue(json, LIST_TYPE); return getObjectMapper().readValue(json, LIST_TYPE);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalArgumentException("Cannot parse JSON", ex); throw new IllegalArgumentException("Cannot parse JSON", ex);
} }
} }
private ObjectMapper getObjectMapper() {
if (this.objectMapper == null) {
this.objectMapper = new ObjectMapper();
}
return this.objectMapper;
}
private static class MapTypeReference extends TypeReference<Map<String, Object>> { private static class MapTypeReference extends TypeReference<Map<String, Object>> {
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment