package com.github.blindpirate.gogradle.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.io.File;
import java.io.IOException;

import static com.github.blindpirate.gogradle.util.ExceptionHandler.uncheckException;

public class DataExchange {
    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
    private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());

    public static <T> T parseJson(File file, Class<T> clazz) {
        return parseWithMapper(JSON_MAPPER, file, clazz);
    }

    public static <T> T parseYaml(File file, Class<T> clazz) {
        return parseWithMapper(YAML_MAPPER, file, clazz);
    }

    private static <T> T parseWithMapper(ObjectMapper mapper, File file, Class<T> clazz) {
        try {
            return mapper.readValue(file, clazz);
        } catch (IOException e) {
            throw uncheckException(e);
        }
    }

    public static String toYaml(Object model) {
        try {
            return YAML_MAPPER.writeValueAsString(model);
        } catch (JsonProcessingException e) {
            throw uncheckException(e);
        }
    }
}

@@ -698,99 +698,468 @@
 arse
-Yaml(File file, Class%3CT%3E clazz) %7B%0A        return parseWithMapper(YAML_MAPPER, file, clazz);
+Json(String str, Class%3CT%3E clazz) %7B%0A        return parseWithMapper(JSON_MAPPER, str, clazz);%0A    %7D%0A%0A    public static %3CT%3E T parseYaml(File file, Class%3CT%3E clazz) %7B%0A        return parseWithMapper(YAML_MAPPER, file, clazz);%0A    %7D%0A%0A    private static %3CT%3E T parseWithMapper(ObjectMapper mapper, String s, Class%3CT%3E clazz) %7B%0A        try %7B%0A            return mapper.readValue(s, clazz);%0A        %7D catch (IOException e) %7B%0A            throw uncheckException(e);%0A        %7D
 %0A   

Support parsing JSON string directly