org.butor.json.JsonHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of butor-json Show documentation
Show all versions of butor-json Show documentation
This module enables fast and easy creation of sync., async., req./resp., req./resp. stream HTTP/json services.
/*******************************************************************************
* Copyright 2013 butor.com
*
* 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.butor.json;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
public class JsonHelper {
private Logger _logger = LoggerFactory.getLogger(getClass());
private static class GmtDateTypeAdapter implements JsonSerializer, JsonDeserializer {
private final DateFormat dateFormat;
private GmtDateTypeAdapter() {
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
public synchronized JsonElement serialize(Date date, Type type,
JsonSerializationContext jsonSerializationContext) {
synchronized (dateFormat) {
String dateFormatAsString = dateFormat.format(date);
return new JsonPrimitive(dateFormatAsString);
}
}
@Override
public synchronized Date deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) {
try {
synchronized (dateFormat) {
return dateFormat.parse(jsonElement.getAsString());
}
} catch (ParseException e) {
throw new JsonSyntaxException(jsonElement.getAsString(), e);
}
}
}
private Gson _mapper;
/**
* Allow to use specifi date format,such: yyyy-MM-dd HH:mm:ss.SSS
*/
public JsonHelper(String dateFormat) {
GsonBuilder gb = new GsonBuilder();
gb.setDateFormat(dateFormat);
_mapper = gb.create();
}
/**
* By default will use GMT date format with milliseconds
*/
public JsonHelper() {
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());
_mapper = gb.create();
}
public String serialize(Object obj_) {
try {
if (obj_ == null) {
return null;
}
return _mapper.toJson(obj_);
} catch (Exception e) {
_logger.warn(String.format("Failed to serialize obj : %s", obj_), e);
}
return null;
}
public T deserialize(String obj_, Class class_) {
if (obj_ == null)
return null;
try {
return _mapper.fromJson(obj_, class_);
} catch (Exception e) {
_logger.warn(String.format("Failed to deserialize obj: %s", obj_), e);
}
return null;
}
}