Skip to content

Commit

Permalink
Structured tag api (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored May 25, 2024
1 parent df19ecf commit fb22392
Show file tree
Hide file tree
Showing 11 changed files with 1,173 additions and 14 deletions.
5 changes: 5 additions & 0 deletions planetiler-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
<artifactId>jackson-dataformat-csv</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
Expand Down
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ public boolean hasTag(String key) {
}


@Override
public Object getTag(String key, Object defaultValue) {
Object val = tags.get(key);
if (val == null) {
return defaultValue;
}
return val;
}

@Override
public Map<String, Object> tags() {
return tags;
Expand Down
Loading

0 comments on commit fb22392

Please sign in to comment.