|
4 | 4 | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
5 | 5 | import com.fasterxml.jackson.annotation.PropertyAccessor; |
6 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.google.gson.Gson; |
7 | 8 | import com.google.gson.JsonElement; |
8 | 9 | import com.google.gson.JsonObject; |
9 | 10 | import com.google.gson.JsonParser; |
@@ -54,6 +55,25 @@ public static String getEncodedString(File image) { |
54 | 55 | } |
55 | 56 | } |
56 | 57 |
|
| 58 | + /** |
| 59 | + * Saves a Base64-encoded string as an image file in the specified directory. |
| 60 | + * |
| 61 | + * @param base64String The Base64-encoded string representing the image. |
| 62 | + * @param outputFile The file where the image should be saved. |
| 63 | + * @throws RuntimeException if an IOException occurs during file writing. |
| 64 | + */ |
| 65 | + public static void saveDecodedImage(String base64String, File outputFile) { |
| 66 | + try { |
| 67 | + // Decode the Base64 string to a byte array |
| 68 | + byte[] imageBytes = Base64.getDecoder().decode(base64String); |
| 69 | + // Write the byte array to the specified file |
| 70 | + Files.write(outputFile.toPath(), imageBytes); |
| 71 | + } catch (IOException e) { |
| 72 | + // Wrap IOException in a RuntimeException and throw it |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
57 | 77 | /** |
58 | 78 | * Retrieves the Base64-encoded string representation of an image file from the given file path. |
59 | 79 | * |
@@ -438,6 +458,7 @@ public static class Json { |
438 | 458 | public JSONObject urlsJson = new JSONObject(); |
439 | 459 | public JSONObject notificationJson = new JSONObject(); |
440 | 460 | private final Printer log = new Printer(Json.class); |
| 461 | + private static final Gson gson = new Gson(); |
441 | 462 |
|
442 | 463 | /** |
443 | 464 | * Saves a JSON object to a file. |
@@ -603,5 +624,27 @@ public static String formatJsonString(String json) { |
603 | 624 | catch (IOException e) {e.printStackTrace();} |
604 | 625 | return null; |
605 | 626 | } |
| 627 | + |
| 628 | + /** |
| 629 | + * Converts a given input object to another type using Gson serialization/deserialization. |
| 630 | + * |
| 631 | + * @param <T> The target class type. |
| 632 | + * @param input The input object to be converted. |
| 633 | + * @param tClass The target class type to convert into. |
| 634 | + * @return An instance of the target class {@code T} populated with data from the input object, or null if conversion fails. |
| 635 | + */ |
| 636 | + public static <T> T typeConversion(Object input, Class<T> tClass) { |
| 637 | + return gson.fromJson(getJsonString(input), tClass); |
| 638 | + } |
| 639 | + |
| 640 | + /** |
| 641 | + * Converts a given input object to its JSON string representation using Gson. |
| 642 | + * |
| 643 | + * @param input The input object to be serialized into JSON. |
| 644 | + * @return A JSON string representing the input object, or null if the input is null. |
| 645 | + */ |
| 646 | + public static String getJsonString(Object input) { |
| 647 | + return gson.toJson(input); |
| 648 | + } |
606 | 649 | } |
607 | 650 | } |
0 commit comments