/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014-2020 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.pnc.common.json;

import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author <a href="mailto:matejonnet@gmail.com">Matej Lazar</a>
 */
public class JsonOutputConverterMapper {

    public final static Logger log = LoggerFactory.getLogger(JsonOutputConverterMapper.class);

    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());

        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS);

        try {
            mapper.addMixIn(
                    Class.forName("com.openshift.restclient.model.IStatus"),
                    PNCMixinToIgnoreKubernetesProjectNPE.class);
        } catch (ClassNotFoundException e) {
            log.error(
                    "Could not ignore com.openshift.restclient.model.IStatus on com.openshift.restclient.authorization.ResourceForbiddenException",
                    e);
        }
    }

    /**
     *
     * @throws RuntimeException
     */
    public static String apply(Object objectToBeConverted) {
        if (objectToBeConverted != null) {
            try {
                return mapper.writeValueAsString(objectToBeConverted);
            } catch (JsonProcessingException e) {
                log.warn("Could not convert object to JSON", e);
                throw new IllegalArgumentException("Could not convert object to JSON", e);
            }
        }
        return "{}";
    }

    public static <T> T readValue(String serialized, Class<T> clazz) throws IOException {
        return mapper.readValue(serialized, clazz);
    }

    public static <T> T readValue(InputStream serialized, Class<T> clazz) throws IOException {
        return mapper.readValue(serialized, clazz);
    }

    static final class OptionalMixin {
        private OptionalMixin() {
        }

        @JsonProperty
        private Object value;
    }

    @JsonIgnoreType
    static final class PNCMixinToIgnoreKubernetesProjectNPE {
    }

    public static ObjectMapper getMapper() {
        return mapper;
    }
}

@@ -743,64 +743,8 @@
 n;%0A%0A
-import com.fasterxml.jackson.annotation.JsonIgnoreType;%0A
 impo
@@ -792,16 +792,16 @@
 nclude;%0A
+
 import c
@@ -2002,444 +2002,8 @@
 RS);
-%0A%0A        try %7B%0A            mapper.addMixIn(%0A                    Class.forName(%22com.openshift.restclient.model.IStatus%22),%0A                    PNCMixinToIgnoreKubernetesProjectNPE.class);%0A        %7D catch (ClassNotFoundException e) %7B%0A            log.error(%0A                    %22Could not ignore com.openshift.restclient.model.IStatus on com.openshift.restclient.authorization.ResourceForbiddenException%22,%0A                    e);%0A        %7D
 %0A   
@@ -2921,16 +2921,16 @@
 roperty%0A
+
         
@@ -2962,97 +2962,8 @@
  %7D%0A%0A
-    @JsonIgnoreType%0A    static final class PNCMixinToIgnoreKubernetesProjectNPE %7B%0A    %7D%0A%0A
     

Revert "Try to ignore nested reference chain bits which causes the NPE"