Skip to content

Commit 03453a4

Browse files
committed
refactor: enhance JSON parsing methods in OpenAiFunctionCall and remove redundant methods from OpenAiUtils
1 parent c92954a commit 03453a4

File tree

2 files changed

+19
-51
lines changed

2 files changed

+19
-51
lines changed

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiFunctionCall.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sap.ai.sdk.foundationmodels.openai;
22

3+
import static com.sap.ai.sdk.foundationmodels.openai.OpenAiUtils.getOpenAiObjectMapper;
4+
35
import com.fasterxml.jackson.core.type.TypeReference;
46
import com.google.common.annotations.Beta;
57
import java.util.Map;
@@ -26,28 +28,39 @@ public class OpenAiFunctionCall implements OpenAiToolCall {
2628
@Nonnull String arguments;
2729

2830
/**
29-
* Returns the arguments as a {@code Map<String, Object>}.
31+
* Parses the arguments, encoded as a JSON string, into a {@code Map<String, Object>}.
3032
*
31-
* @return the parsed arguments
33+
* @return a map of the arguments
3234
* @throws IllegalArgumentException if parsing fails
3335
* @since 1.7.0
3436
*/
3537
@Nonnull
3638
public Map<String, Object> getArgumentsAsMap() throws IllegalArgumentException {
37-
return OpenAiUtils.parseJson(getArguments(), new TypeReference<>() {});
39+
final var typeReference = new TypeReference<Map<String, Object>>() {};
40+
try {
41+
return getOpenAiObjectMapper().readValue(getArguments(), typeReference);
42+
} catch (Exception e) {
43+
throw new IllegalArgumentException(
44+
"Failed to parse JSON string to type " + typeReference.getType(), e);
45+
}
3846
}
3947

4048
/**
41-
* Returns the arguments as an object of the specified class.
49+
* Parses the arguments, encoded as a JSON string, into an object of the specified type.
4250
*
4351
* @param clazz the class to convert the arguments to
4452
* @param <T> the type of the class
45-
* @return the arguments as an object of the specified class
53+
* @return the parsed arguments as an object
4654
* @throws IllegalArgumentException if parsing fails
4755
* @since 1.7.0
4856
*/
4957
@Nonnull
5058
public <T> T getArgumentsAsObject(@Nonnull final Class<T> clazz) throws IllegalArgumentException {
51-
return OpenAiUtils.parseJson(getArguments(), clazz);
59+
try {
60+
return getOpenAiObjectMapper().readValue(getArguments(), clazz);
61+
} catch (Exception e) {
62+
throw new IllegalArgumentException(
63+
"Failed to parse JSON string to class " + clazz.getTypeName(), e);
64+
}
5265
}
5366
}

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiUtils.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static com.sap.ai.sdk.core.JacksonConfiguration.getDefaultObjectMapper;
44

5-
import com.fasterxml.jackson.core.type.TypeReference;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import com.google.common.annotations.Beta;
87
import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionRequestMessage;
@@ -54,48 +53,4 @@ static ObjectMapper getOpenAiObjectMapper() {
5453
ChatCompletionsCreate200Response.class,
5554
JacksonMixins.DefaultChatCompletionCreate200ResponseMixIn.class);
5655
}
57-
58-
/**
59-
* Parses a JSON string into an object of the specified {@code TypeReference} using the module
60-
* default object mapper.
61-
*
62-
* @param json the JSON string to parse
63-
* @param typeReference the type reference for the target type
64-
* @param <T> the target type
65-
* @return the parsed object
66-
* @throws IllegalArgumentException if parsing fails
67-
* @since 1.7.0
68-
*/
69-
@Nonnull
70-
static <T> T parseJson(@Nonnull final String json, @Nonnull final TypeReference<T> typeReference)
71-
throws IllegalArgumentException {
72-
try {
73-
return getOpenAiObjectMapper().readValue(json, typeReference);
74-
} catch (Exception e) {
75-
throw new IllegalArgumentException(
76-
"Failed to parse JSON string to type " + typeReference.getType(), e);
77-
}
78-
}
79-
80-
/**
81-
* Parses a JSON string into an object of the specified class using the module default object
82-
* mapper.
83-
*
84-
* @param json the JSON string to parse
85-
* @param clazz the class of the target type
86-
* @param <T> the target type
87-
* @return the parsed object
88-
* @throws IllegalArgumentException if parsing fails
89-
* @since 1.7.0
90-
*/
91-
@Nonnull
92-
static <T> T parseJson(@Nonnull final String json, @Nonnull final Class<T> clazz)
93-
throws IllegalArgumentException {
94-
try {
95-
return getOpenAiObjectMapper().readValue(json, clazz);
96-
} catch (Exception e) {
97-
throw new IllegalArgumentException(
98-
"Failed to parse JSON string to class " + clazz.getTypeName(), e);
99-
}
100-
}
10156
}

0 commit comments

Comments
 (0)