package org.apache.logging.log4j.core.layout;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Charsets;

@Plugin(
   name = "JsonLayout",
   category = "Core",
   elementType = "layout",
   printObject = true
)
public final class JsonLayout extends AbstractJacksonLayout {
   protected JsonLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset) {
      super((new JacksonFactory.JSON()).newWriter(locationInfo, properties, compact), charset, compact, complete);
   }

   public byte[] getHeader() {
      if (!this.complete) {
         return null;
      } else {
         StringBuilder buf = new StringBuilder();
         buf.append('[');
         buf.append(this.eol);
         return buf.toString().getBytes(this.getCharset());
      }
   }

   public byte[] getFooter() {
      return !this.complete ? null : (this.eol + ']' + this.eol).getBytes(this.getCharset());
   }

   public Map<String, String> getContentFormat() {
      Map<String, String> result = new HashMap();
      result.put("version", "2.0");
      return result;
   }

   public String getContentType() {
      return "application/json; charset=" + this.getCharset();
   }

   @PluginFactory
   public static AbstractJacksonLayout createLayout(@PluginAttribute(value = "locationInfo",defaultBoolean = false) boolean locationInfo, @PluginAttribute(value = "properties",defaultBoolean = false) boolean properties, @PluginAttribute(value = "complete",defaultBoolean = false) boolean complete, @PluginAttribute(value = "compact",defaultBoolean = false) boolean compact, @PluginAttribute(value = "charset",defaultString = "UTF-8") Charset charset) {
      return new JsonLayout(locationInfo, properties, complete, compact, charset);
   }

   public static AbstractJacksonLayout createDefaultLayout() {
      return new JsonLayout(false, false, false, false, Charsets.UTF_8);
   }
}
