|
| 1 | +package io.audd.internal; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | + |
| 6 | +/** |
| 7 | + * Shared, leniently-configured {@link ObjectMapper} for all response parsing. |
| 8 | + * |
| 9 | + * <p>A successful API response must never fail to parse because a field is |
| 10 | + * absent or a different type than expected. The enterprise endpoint, for |
| 11 | + * example, legitimately returns matches with no {@code score} (and no |
| 12 | + * {@code isrc}/{@code upc}/{@code label}). The mapper here is configured so |
| 13 | + * that missing, unknown, and null-for-primitive fields all degrade to |
| 14 | + * sensible defaults instead of throwing.</p> |
| 15 | + * |
| 16 | + * <p>This does not weaken the error contract: a {@code status=error} body is |
| 17 | + * still turned into a typed exception before any model is decoded, and a body |
| 18 | + * that is not valid JSON at all still surfaces as a serialization error.</p> |
| 19 | + */ |
| 20 | +public final class Json { |
| 21 | + private static final ObjectMapper MAPPER = newLenientMapper(); |
| 22 | + |
| 23 | + private Json() {} |
| 24 | + |
| 25 | + /** The shared lenient mapper. Jackson {@link ObjectMapper} is thread-safe once configured. */ |
| 26 | + public static ObjectMapper mapper() { |
| 27 | + return MAPPER; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Build a mapper that tolerates the natural shape variation of AudD |
| 32 | + * responses: |
| 33 | + * <ul> |
| 34 | + * <li>unknown keys are ignored (typed models also absorb them via |
| 35 | + * {@code @JsonAnySetter}, but this protects any plain POJO too);</li> |
| 36 | + * <li>a JSON {@code null} for a primitive field becomes the primitive's |
| 37 | + * default rather than throwing;</li> |
| 38 | + * <li>a missing {@code @JsonCreator} property is allowed;</li> |
| 39 | + * <li>scalars accepted for single-element arrays and vice versa, so a |
| 40 | + * field that is sometimes an object and sometimes a list still |
| 41 | + * decodes.</li> |
| 42 | + * </ul> |
| 43 | + */ |
| 44 | + public static ObjectMapper newLenientMapper() { |
| 45 | + return new ObjectMapper() |
| 46 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 47 | + .configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false) |
| 48 | + .configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false) |
| 49 | + .configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false) |
| 50 | + .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true) |
| 51 | + .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) |
| 52 | + .configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true) |
| 53 | + .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) |
| 54 | + .configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true); |
| 55 | + } |
| 56 | +} |
0 commit comments