|
| 1 | +package com.mindee.v1.input; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.mindee.MindeeException; |
| 5 | +import com.mindee.v1.parsing.common.AsyncPredictResponse; |
| 6 | +import com.mindee.v1.parsing.common.Inference; |
| 7 | +import com.mindee.v1.parsing.common.PredictResponse; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Path; |
| 13 | + |
| 14 | +/** |
| 15 | + * A Mindee response saved locally. |
| 16 | + */ |
| 17 | +public class LocalResponse extends com.mindee.input.LocalResponse { |
| 18 | + |
| 19 | + public LocalResponse(InputStream input) { |
| 20 | + super(input); |
| 21 | + } |
| 22 | + |
| 23 | + public LocalResponse(String input) { |
| 24 | + super(input); |
| 25 | + } |
| 26 | + |
| 27 | + public LocalResponse(File input) throws IOException { |
| 28 | + super(input); |
| 29 | + } |
| 30 | + |
| 31 | + public LocalResponse(Path input) throws IOException { |
| 32 | + super(input); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Deserialize this local JSON payload into a specific {@link AsyncPredictResponse}. |
| 37 | + * subtype: {@code InferenceResponse}, {@code JobResponse}. |
| 38 | + * |
| 39 | + * @param responseClass the concrete class to instantiate |
| 40 | + * @param <T> generic {@link Inference} |
| 41 | + * @return A {@link AsyncPredictResponse} instance. |
| 42 | + * @throws MindeeException if the payload cannot be deserialized into the requested type |
| 43 | + */ |
| 44 | + public <T extends Inference> AsyncPredictResponse<T> deserializeAsyncResponse( |
| 45 | + Class<T> responseClass |
| 46 | + ) { |
| 47 | + var objectMapper = new ObjectMapper(); |
| 48 | + objectMapper.findAndRegisterModules(); |
| 49 | + var type = objectMapper |
| 50 | + .getTypeFactory() |
| 51 | + .constructParametricType(AsyncPredictResponse.class, responseClass); |
| 52 | + try { |
| 53 | + AsyncPredictResponse<T> response = objectMapper.readValue(this.file, type); |
| 54 | + response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); |
| 55 | + return response; |
| 56 | + } catch (Exception ex) { |
| 57 | + throw new MindeeException("Invalid class specified for deserialization.", ex); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Deserialize this local JSON payload into a specific {@link PredictResponse}. |
| 63 | + * |
| 64 | + * @param responseClass the concrete class to instantiate |
| 65 | + * @param <T> generic {@link Inference} |
| 66 | + * @return A {@link PredictResponse} instance. |
| 67 | + * @throws MindeeException if the payload cannot be deserialized into the requested type |
| 68 | + */ |
| 69 | + public <T extends Inference> PredictResponse<T> deserializeSyncResponse(Class<T> responseClass) { |
| 70 | + var objectMapper = new ObjectMapper(); |
| 71 | + objectMapper.findAndRegisterModules(); |
| 72 | + var type = objectMapper |
| 73 | + .getTypeFactory() |
| 74 | + .constructParametricType(PredictResponse.class, responseClass); |
| 75 | + try { |
| 76 | + PredictResponse<T> response = objectMapper.readValue(this.file, type); |
| 77 | + response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); |
| 78 | + return response; |
| 79 | + } catch (Exception ex) { |
| 80 | + throw new MindeeException("Invalid class specified for deserialization.", ex); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments