All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.convergence_platform.common.helpers.JsonHelper Maven / Gradle / Ivy

Go to download

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);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy