|
| 1 | +package ai.wanaku.core.persistence; |
| 2 | + |
| 3 | +import ai.wanaku.api.exceptions.WanakuException; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import jakarta.enterprise.context.ApplicationScoped; |
| 8 | +import jakarta.enterprise.inject.Default; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * Service class responsible for marshalling and unmarshalling objects to and from JSON format. |
| 15 | + * This class provides functionality to convert Java objects to JSON strings and vice versa. |
| 16 | + */ |
| 17 | +@ApplicationScoped |
| 18 | +@Default |
| 19 | +public class WanakuMarshallerService { |
| 20 | + |
| 21 | + private ObjectMapper mapper; |
| 22 | + |
| 23 | + public WanakuMarshallerService() { |
| 24 | + mapper = new ObjectMapper(); |
| 25 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Converts a Java object to its JSON string representation. |
| 30 | + * |
| 31 | + * @param object The object to be marshalled to JSON |
| 32 | + * @return A JSON string representation of the object |
| 33 | + * @throws WanakuException If marshalling fails |
| 34 | + */ |
| 35 | + public String marshal(Object object) { |
| 36 | + try { |
| 37 | + return mapper.writeValueAsString(object); |
| 38 | + } catch (JsonProcessingException e) { |
| 39 | + throw new WanakuException("Marshal failed", e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Converts a JSON string to a list of Java objects of the specified type. |
| 45 | + * |
| 46 | + * @param json The JSON string to be unmarshalled |
| 47 | + * @param clazz The class type to convert the JSON elements to |
| 48 | + * @param <T> The generic type of the objects in the resulting list |
| 49 | + * @return A list of objects of type T parsed from the JSON string |
| 50 | + * @throws WanakuException If unmarshalling fails |
| 51 | + */ |
| 52 | + public <T> List<T> unmarshal(String json, Class<T> clazz) { |
| 53 | + if (json == null || json.isEmpty()) { |
| 54 | + return new ArrayList<>(); |
| 55 | + } |
| 56 | + try { |
| 57 | + return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, clazz)); |
| 58 | + } catch (JsonProcessingException e) { |
| 59 | + throw new WanakuException("Unmarshal failed", e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Converts a JSON string to a single Java object of the specified type. |
| 65 | + * |
| 66 | + * @param json The JSON string to be unmarshalled |
| 67 | + * @param clazz The class type to convert the JSON to |
| 68 | + * @param <T> The generic type of the resulting object |
| 69 | + * @return An object of type T parsed from the JSON string |
| 70 | + * @throws WanakuException If unmarshalling fails |
| 71 | + */ |
| 72 | + public <T> T unmarshalOne(String json, Class<T> clazz) { |
| 73 | + try { |
| 74 | + return mapper.readValue(json, clazz); |
| 75 | + } catch (JsonProcessingException e) { |
| 76 | + throw new WanakuException("Unmarshal failed", e); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments