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

com.xliic.cicd.audit.JsonParser Maven / Gradle / Ivy

package com.xliic.cicd.audit;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.xliic.common.Workspace;
import com.xliic.openapi.bundler.Bundler;
import com.xliic.openapi.bundler.Document;
import com.xliic.openapi.bundler.Mapping;
import com.xliic.openapi.bundler.Parser;
import com.xliic.openapi.bundler.ReferenceResolutionException;
import com.xliic.openapi.bundler.Serializer;

public class JsonParser {

    private static ObjectMapper getMapper(boolean yaml) {
        ObjectMapper mapper;
        if (yaml) {
            mapper = new ObjectMapper(new YAMLFactory());
        } else {
            mapper = new ObjectMapper();
        }
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }

    public static  T parse(byte[] json, Class contentClass) throws IOException {
        return getMapper(false).readValue(json, contentClass);
    }

    public static  T parse(String json, Class contentClass) throws IOException {
        return getMapper(false).readValue(json, contentClass);
    }

    public static  T parse(String data, Class contentClass, boolean isYaml) throws IOException {
        return getMapper(isYaml).readValue(data, contentClass);
    }

    public static  T parse(InputStream json, Class contentClass) throws IOException {
        return getMapper(false).readValue(json, contentClass);
    }

    public static Bundled bundle(URI file, Workspace workspace) throws AuditException, ReferenceResolutionException {
        try {
            Parser parser = new Parser(workspace);
            Serializer serializer = new Serializer();
            Bundler bundler = new Bundler(parser, serializer);
            Document document = parser.parse(file);
            Mapping mapping = bundler.bundle(document);
            return new Bundled(serializer.serialize(document), mapping);
        } catch (ReferenceResolutionException e) {
            throw e;
        } catch (Exception e) {
            throw new AuditException(String.format("Failed to parse file: %s %s", file, e), e);
        }
    }

    public static class Bundled {
        public final String json;
        public final Mapping mapping;

        public Bundled(String json, Mapping mapping) {
            this.json = json;
            this.mapping = mapping;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy