|
| 1 | +package io.swagger.transform.util; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.JsonNode; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 9 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 10 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +/** |
| 18 | + * Jackson utilities used by the migration and validation code |
| 19 | + * |
| 20 | + * <p>The mapper configuration matches the one previously provided by |
| 21 | + * jackson-coreutils, so that migrated documents are serialized the same way |
| 22 | + * (in particular, floating point numbers are read as {@link |
| 23 | + * java.math.BigDecimal}s and written in plain notation).</p> |
| 24 | + */ |
| 25 | +public final class JacksonSupport { |
| 26 | + private static final ObjectMapper MAPPER = newMapper(); |
| 27 | + private static final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter(); |
| 28 | + |
| 29 | + private JacksonSupport() { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Return a newly configured {@link ObjectMapper} |
| 34 | + * |
| 35 | + * @return a new mapper |
| 36 | + */ |
| 37 | + public static ObjectMapper newMapper() { |
| 38 | + return new ObjectMapper() |
| 39 | + .setNodeFactory(JsonNodeFactory.instance) |
| 40 | + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 41 | + .enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN) |
| 42 | + .enable(SerializationFeature.INDENT_OUTPUT); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Return the node factory used by mappers created by this class |
| 47 | + * |
| 48 | + * @return the node factory |
| 49 | + */ |
| 50 | + public static JsonNodeFactory nodeFactory() { |
| 51 | + return JsonNodeFactory.instance; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Pretty print a JSON value |
| 56 | + * |
| 57 | + * @param node the value |
| 58 | + * @return the pretty printed value |
| 59 | + */ |
| 60 | + public static String prettyPrint(final JsonNode node) { |
| 61 | + Objects.requireNonNull(node, "node must not be null"); |
| 62 | + try { |
| 63 | + return WRITER.writeValueAsString(node); |
| 64 | + } catch (JsonProcessingException e) { |
| 65 | + throw new IllegalStateException("cannot serialize JSON value", e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Read a JSON value from a classpath resource |
| 71 | + * |
| 72 | + * @param resource the resource path; must start with {@code /} |
| 73 | + * @return the JSON value |
| 74 | + * @throws IOException resource not found, or invalid JSON |
| 75 | + */ |
| 76 | + public static JsonNode fromResource(final String resource) |
| 77 | + throws IOException { |
| 78 | + Objects.requireNonNull(resource, "resource must not be null"); |
| 79 | + if (!resource.startsWith("/")) { |
| 80 | + throw new IllegalArgumentException("resource path does not start with a '/'"); |
| 81 | + } |
| 82 | + URL url = JacksonSupport.class.getResource(resource); |
| 83 | + if (url == null) { |
| 84 | + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 85 | + if (loader != null) { |
| 86 | + url = loader.getResource(resource.substring(1)); |
| 87 | + } |
| 88 | + } |
| 89 | + if (url == null) { |
| 90 | + throw new IOException("resource " + resource + " not found"); |
| 91 | + } |
| 92 | + try (InputStream in = url.openStream()) { |
| 93 | + return MAPPER.readTree(in); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments