|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.json; |
| 7 | + |
| 8 | +import java.lang.reflect.Type; |
| 9 | + |
| 10 | +import io.jooby.Reified; |
| 11 | + |
| 12 | +/** |
| 13 | + * Contract for decoding (deserializing) JSON strings into Java objects. |
| 14 | + * |
| 15 | + * <p>This functional interface provides the core deserialization strategy for Jooby. It is designed |
| 16 | + * to be implemented by specific JSON library integrations (such as Jackson, Gson, Moshi, etc.) to |
| 17 | + * adapt their internal parsing mechanics to Jooby's standard architecture. |
| 18 | + * |
| 19 | + * <p><strong>Important Note:</strong> Jooby core itself <em>does not</em> implement these |
| 20 | + * interfaces. These contracts act as a bridge and are designed to be implemented exclusively by |
| 21 | + * dedicated JSON modules (such as {@code jooby-jackson}, {@code jooby-gson}, or {@code |
| 22 | + * jooby-avaje-json}), etc. |
| 23 | + * |
| 24 | + * @since 4.5.0 |
| 25 | + * @author edgar |
| 26 | + */ |
| 27 | +@FunctionalInterface |
| 28 | +public interface JsonDecoder { |
| 29 | + |
| 30 | + /** |
| 31 | + * Decodes a JSON string into the specified Java {@link java.lang.reflect.Type}. |
| 32 | + * |
| 33 | + * <p>This is the primary decoding method that all underlying JSON libraries must implement. It |
| 34 | + * accepts a raw reflection {@code Type}, making it capable of handling both simple classes and |
| 35 | + * complex parameterized/generic types (e.g., {@code List<MyObject>}). |
| 36 | + * |
| 37 | + * @param json The JSON payload as a string. |
| 38 | + * @param type The target Java reflection type to deserialize into. |
| 39 | + * @return The deserialized Java object instance. |
| 40 | + * @param <T> The expected generic type of the returned object. |
| 41 | + */ |
| 42 | + <T> T decode(String json, Type type); |
| 43 | + |
| 44 | + /** |
| 45 | + * Decodes a JSON string into the specified Java {@link Class}. |
| 46 | + * |
| 47 | + * <p>This is a convenience method for deserializing simple, non-generic types. It delegates |
| 48 | + * directly to {@link #decode(String, Type)}. |
| 49 | + * |
| 50 | + * @param json The JSON payload as a string. |
| 51 | + * @param type The target Java class to deserialize into. |
| 52 | + * @return The deserialized Java object instance. |
| 53 | + * @param <T> The expected generic type of the returned object. |
| 54 | + */ |
| 55 | + default <T> T decode(String json, Class<T> type) { |
| 56 | + return decode(json, (java.lang.reflect.Type) type); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Decodes a JSON string into the specified Jooby {@link Reified} type. |
| 61 | + * |
| 62 | + * <p>This is a convenience method for deserializing complex, generic types while avoiding type |
| 63 | + * erasure. It extracts the underlying reflection type from the {@code Reified} token and |
| 64 | + * delegates to {@link #decode(String, Type)}. |
| 65 | + * |
| 66 | + * @param json The JSON payload as a string. |
| 67 | + * @param type The Reified type token capturing the target type. |
| 68 | + * @return The deserialized Java object instance. |
| 69 | + * @param <T> The expected generic type of the returned object. |
| 70 | + */ |
| 71 | + default <T> T decode(String json, Reified<T> type) { |
| 72 | + return decode(json, type.getType()); |
| 73 | + } |
| 74 | +} |
0 commit comments