io.convergence_platform.common.helpers.JsonHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.helpers;
import io.convergence_platform.common.helpers.ExceptionHelper;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonHelper {
public static String asJsonString(Object obj) {
if (obj == null) return "";
return ExceptionHelper.executeWithValue(() -> {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
});
}
public static String asJsonStringPretty(Object obj) {
if (obj == null) return "";
return ExceptionHelper.executeWithValue(() -> {
ObjectMapper mapper = new ObjectMapper();
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
});
}
public static HashMap fromJsonMap(String json) {
return ExceptionHelper.executeWithValue(() -> {
ObjectMapper mapper = new ObjectMapper();
return mapper.readerFor(Map.class).readValue(json);
});
}
public static List fromJsonList(String json) {
return ExceptionHelper.executeWithValue(() -> {
ObjectMapper mapper = new ObjectMapper();
return mapper.readerFor(List.class).readValue(json);
});
}
public static T fromJson(String json, Class cls) {
return ExceptionHelper.executeWithValue(() -> {
ObjectMapper mapper = new ObjectMapper();
return mapper.readerFor(cls).readValue(json);
});
}
}