package com.growthbeat.utils;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

	private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

	public static <T> String sanitize(String json, Class<T> valueType) {
		return serialize(deserialize(json, valueType));
	}

	@SuppressWarnings("deprecation")
	public static String serialize(Object value) {

		// TODO beat, push に合わせたが、非推奨メソッドとなっていたので、後で対応
		ObjectMapper mapper = new ObjectMapper();
		mapper.getJsonFactory().setCharacterEscapes(new HTMLCharacterEscapes());

		String json = null;
		try {
			json = mapper.writeValueAsString(value);
		} catch (IOException e) {
			logger.debug("Serialization error. " + e.getMessage());
			return null;
		}
		return json;

	}

	public static <T> T deserialize(String json, Class<T> valueType) {

		if (json == null)
			return null;

		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		T object = null;
		try {
			object = mapper.readValue(json, valueType);
		} catch (IOException e) {
			return null;
		}
		return object;

	}

	public static <T> T deserialize(String json, TypeReference<T> valueType) {

		if (json == null)
			return null;

		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		T object = null;
		try {
			object = mapper.readValue(json, valueType);
		} catch (IOException e) {
			return null;
		}
		return object;

	}

	public static class HTMLCharacterEscapes extends CharacterEscapes {

		private static final long serialVersionUID = 1L;

		private final int[] asciiEscapes;

		public HTMLCharacterEscapes() {
			asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON();
			asciiEscapes['<'] = CharacterEscapes.ESCAPE_STANDARD;
			asciiEscapes['>'] = CharacterEscapes.ESCAPE_STANDARD;
			asciiEscapes['&'] = CharacterEscapes.ESCAPE_STANDARD;
			asciiEscapes['\''] = CharacterEscapes.ESCAPE_STANDARD;
			asciiEscapes['/'] = CharacterEscapes.ESCAPE_STANDARD;
			asciiEscapes['+'] = CharacterEscapes.ESCAPE_STANDARD;
		}

		@Override
		public int[] getEscapeCodesForAscii() {
			return asciiEscapes;
		}

		@Override
		public SerializableString getEscapeSequence(int ch) {
			return null;
		}
	}

}

@@ -57,66 +57,8 @@
 n;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -361,89 +361,8 @@
  %7B%0A%0A
-%09private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);%0A%0A
 %09pub
@@ -485,42 +485,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22deprecation%22)%0A
 %09pub
@@ -534,57 +534,8 @@
  %7B%0A%0A
-%09%09// TODO beat, push %E3%81%AB%E5%90%88%E3%82%8F%E3%81%9B%E3%81%9F%E3%81%8C%E3%80%81%E9%9D%9E%E6%8E%A8%E5%A5%A8%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%A8%E3%81%AA%E3%81%A3%E3%81%A6%E3%81%84%E3%81%9F%E3%81%AE%E3%81%A7%E3%80%81%E5%BE%8C%E3%81%A7%E5%AF%BE%E5%BF%9C%0A
 %09%09Ob
@@ -586,20 +586,16 @@
 pper.get
-Json
 Factory(
@@ -752,67 +752,8 @@
 ) %7B%0A
-%09%09%09logger.debug(%22Serialization error. %22 + e.getMessage());%0A
 %09%09%09r

remove deprecated method