Skip to content

Commit

Permalink
Merge pull request #673 from quarkiverse/json-string
Browse files Browse the repository at this point in the history
Remove preceding  and trailing json markers when converting from json
  • Loading branch information
geoand authored Jun 13, 2024
2 parents 89e4331 + f870364 commit 32fcbc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public Json.JsonCodec create() {

private static class Codec implements Json.JsonCodec {

private static final String JSON_START_MARKER = "```json\n";
private static final String JSON_END_MARKER = "\n```";

@Override
public String toJson(Object o) {
try {
Expand All @@ -43,7 +46,8 @@ public String toJson(Object o) {
@Override
public <T> T fromJson(String json, Class<T> type) {
try {
return ObjectMapperHolder.MAPPER.readValue(json, type);
String sanitizedJson = sanitize(json, type);
return ObjectMapperHolder.MAPPER.readValue(sanitizedJson, type);
} catch (JsonProcessingException e) {
if ((e instanceof JsonParseException) && (type.isEnum())) {
// this is the case where LangChain4j simply passes the string value of the enum to Json.fromJson()
Expand All @@ -55,6 +59,16 @@ public <T> T fromJson(String json, Class<T> type) {
}
}

private <T> String sanitize(String original, Class<T> type) {
if (String.class.equals(type)) {
return original;
}
if (original.startsWith(JSON_START_MARKER) && original.endsWith(JSON_END_MARKER)) {
return original.substring(JSON_START_MARKER.length(), original.length() - JSON_END_MARKER.length());
}
return original;
}

@Override
public InputStream toInputStream(Object o, Class<?> type) throws IOException {
return new ByteArrayInputStream(ObjectMapperHolder.WRITER.writeValueAsBytes(o));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ interface Chef {
void test_create_recipe_from_list_of_ingredients() throws IOException {
setChatCompletionMessageContent(
// this is supposed to be a string inside a json string hence all the escaping...
"{\\n\\\"title\\\": \\\"Greek Salad\\\",\\n\\\"description\\\": \\\"A refreshing and tangy salad with Mediterranean flavors.\\\",\\n\\\"steps\\\": [\\n\\\"Chop, dice, and slice.\\\",\\n\\\"Mix veggies with feta.\\\",\\n\\\"Drizzle with olive oil.\\\",\\n\\\"Toss gently, then serve.\\\"\\n],\\n\\\"preparationTimeMinutes\\\": 15\\n}");
"```json\\n{\\n\\\"title\\\": \\\"Greek Salad\\\",\\n\\\"description\\\": \\\"A refreshing and tangy salad with Mediterranean flavors.\\\",\\n\\\"steps\\\": [\\n\\\"Chop, dice, and slice.\\\",\\n\\\"Mix veggies with feta.\\\",\\n\\\"Drizzle with olive oil.\\\",\\n\\\"Toss gently, then serve.\\\"\\n],\\n\\\"preparationTimeMinutes\\\": 15\\n}\\n```");
Chef chef = AiServices.create(Chef.class, createChatModel());

Recipe result = chef.createRecipeFrom("cucumber", "tomato", "feta", "onion", "olives");
Expand Down

0 comments on commit 32fcbc6

Please sign in to comment.