-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
465 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/de/flavormate/ad_configurations/jackson/ImageDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.flavormate.ad_configurations.jackson; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import de.flavormate.ba_entities.scrape.model.ImageObject; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ImageDeserializer extends JsonDeserializer<List<String>> { | ||
@Override | ||
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
List<String> result = new ArrayList<>(); | ||
|
||
JsonToken currentToken = p.currentToken(); | ||
if (currentToken == JsonToken.START_ARRAY) { | ||
// If the token is the start of an array, iterate through it | ||
while (p.nextToken() != JsonToken.END_ARRAY) { | ||
if (p.currentToken() == JsonToken.VALUE_STRING) { | ||
// Add string images to the list | ||
result.add(p.getValueAsString()); | ||
} else if (p.currentToken() == JsonToken.START_OBJECT) { | ||
|
||
// Deserialize ImageObject and extract the URL | ||
ImageObject imageObject = p.readValueAs(ImageObject.class); | ||
result.add(imageObject.url()); | ||
} | ||
} | ||
} else if (currentToken == JsonToken.VALUE_STRING) { | ||
// If it's a single string, just add it to the result | ||
result.add(p.getValueAsString()); | ||
} else if (currentToken == JsonToken.START_OBJECT) { | ||
// Deserialize ImageObject if it's an object | ||
ImageObject imageObject = p.readValueAs(ImageObject.class); | ||
result.add(imageObject.url()); | ||
} else { | ||
// Handle unexpected types by throwing an error or logging | ||
ctxt.reportInputMismatch( | ||
List.class, | ||
"Expected either a string, array, or an ImageObject, but got: " + currentToken | ||
); | ||
} | ||
|
||
return result; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/de/flavormate/ad_configurations/jackson/ListOrStringDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package de.flavormate.ad_configurations.jackson; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ListOrStringDeserializer extends JsonDeserializer<List<Object>> { | ||
|
||
@Override | ||
public List<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
List<Object> result = new ArrayList<>(); | ||
|
||
JsonToken currentToken = p.currentToken(); | ||
if (currentToken == JsonToken.START_ARRAY) { | ||
// If the token is the start of an array, read it as a list | ||
result = p.readValueAs(List.class); | ||
} else if (currentToken == JsonToken.VALUE_STRING) { | ||
// If the token is a single string, add it to the list | ||
result.add(p.getValueAsString()); | ||
} else { | ||
// Handle unexpected cases by throwing an exception | ||
ctxt.reportInputMismatch( | ||
List.class, | ||
"Expected either an array or a string for recipeIngredient or recipeInstructions, but got: " + currentToken | ||
); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.