-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,173 additions
and
14 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
46 changes: 46 additions & 0 deletions
46
planetiler-core/src/main/java/com/onthegomap/planetiler/reader/JsonConversion.java
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,46 @@ | ||
package com.onthegomap.planetiler.reader; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import java.io.UncheckedIOException; | ||
|
||
/** | ||
* Utilities for converting between JSON strings and java objects using Jackson utilities. | ||
* <p> | ||
* {@link ObjectMapper} are expensive to construct, but not thread safe, so this class reuses the same object mapper | ||
* within each thread but does not share between threads. | ||
*/ | ||
class JsonConversion { | ||
private JsonConversion() {} | ||
|
||
@SuppressWarnings("java:S5164") // ignore not calling remove() on mappers since number of threads is limited | ||
private static final ThreadLocal<ObjectMapper> MAPPERS = ThreadLocal.withInitial(() -> JsonMapper.builder() | ||
.addModule( | ||
new JavaTimeModule().addSerializer(Struct.class, new StructSerializer()) | ||
) | ||
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) | ||
.build()); | ||
|
||
public static String writeValueAsString(Object o) { | ||
try { | ||
return o == null ? null : MAPPERS.get().writeValueAsString(o); | ||
} catch (JsonProcessingException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
public static <T> T convertValue(Object o, Class<T> clazz) { | ||
return o == null ? null : MAPPERS.get().convertValue(o, clazz); | ||
} | ||
|
||
public static <T> T readValue(String string, Class<T> clazz) { | ||
try { | ||
return string == null ? null : MAPPERS.get().readValue(string, clazz); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.