11package com .mindee .v1 .input ;
22
3+ import com .fasterxml .jackson .core .JacksonException ;
34import com .fasterxml .jackson .databind .ObjectMapper ;
45import com .mindee .MindeeException ;
6+ import com .mindee .input .BaseLocalResponse ;
57import com .mindee .v1 .parsing .common .AsyncPredictResponse ;
68import com .mindee .v1 .parsing .common .Inference ;
79import com .mindee .v1 .parsing .common .PredictResponse ;
1416/**
1517 * A Mindee response saved locally.
1618 */
17- public class LocalResponse extends com . mindee . input . LocalResponse {
19+ public class LocalResponse extends BaseLocalResponse {
1820
1921 public LocalResponse (InputStream input ) {
2022 super (input );
@@ -32,52 +34,52 @@ public LocalResponse(Path input) throws IOException {
3234 super (input );
3335 }
3436
37+ private <P extends Inference , R > R deserialize (
38+ Class <R > responseClass ,
39+ Class <P > productClass
40+ ) throws IOException {
41+ var mapper = new ObjectMapper ().findAndRegisterModules ();
42+ var type = mapper .getTypeFactory ().constructParametricType (responseClass , productClass );
43+ try {
44+ return mapper .readValue (this .file , type );
45+ } catch (Exception e ) {
46+ if (e instanceof JacksonException ) {
47+ throw new MindeeException ("Invalid JSON payload." , e );
48+ }
49+ throw e ;
50+ }
51+ }
52+
3553 /**
3654 * Deserialize this local JSON payload into a specific {@link AsyncPredictResponse}.
3755 * subtype: {@code InferenceResponse}, {@code JobResponse}.
3856 *
39- * @param responseClass the concrete class to instantiate
57+ * @param productClass the concrete class to instantiate
4058 * @param <T> generic {@link Inference}
4159 * @return A {@link AsyncPredictResponse} instance.
4260 * @throws MindeeException if the payload cannot be deserialized into the requested type
4361 */
4462 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- }
63+ Class <T > productClass
64+ ) throws IOException {
65+ AsyncPredictResponse <T > response = deserialize (AsyncPredictResponse .class , productClass );
66+ response .setRawResponse (new String (this .file , StandardCharsets .UTF_8 ));
67+ return response ;
5968 }
6069
6170 /**
6271 * Deserialize this local JSON payload into a specific {@link PredictResponse}.
6372 *
64- * @param responseClass the concrete class to instantiate
73+ * @param productClass the concrete class to instantiate
6574 * @param <T> generic {@link Inference}
6675 * @return A {@link PredictResponse} instance.
6776 * @throws MindeeException if the payload cannot be deserialized into the requested type
6877 */
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- }
78+ public <T extends Inference > PredictResponse <T > deserializeSyncResponse (
79+ Class <T > productClass
80+ ) throws IOException {
81+ PredictResponse <T > response = deserialize (PredictResponse .class , productClass );
82+ response .setRawResponse (new String (this .file , StandardCharsets .UTF_8 ));
83+ return response ;
8284 }
8385}
0 commit comments