org.brunocvcunha.digesteroids.cast.DigesteroidsDefaultCaster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of digesteroids Show documentation
Show all versions of digesteroids Show documentation
Digester on Steroids for Java
/**
* Copyright (C) 2015 Bruno Candido Volpato da Cunha ([email protected])
*
* 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.brunocvcunha.digesteroids.cast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
* Reflection Difference Handling
*
* @author Bruno Candido Volpato da Cunha
*/
public class DigesteroidsDefaultCaster implements DigesteroidsCaster {
private static Logger log = Logger.getLogger(DigesteroidsDefaultCaster.class);
/**
* Constant for Map of String to Object
*/
public static final Type _obj_map_token = new TypeToken>() {
//no impl
}.getType();
/**
* Constant for List of Map from String to Object
*/
public static final Type _obj_map_list_token = new TypeToken>>() {
//no impl
}.getType();
/**
* The default format for date and time
*/
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
/**
* The default format for date
*/
public static final String DEFAULT_DATE_ONLY_FORMAT = "yyyy-MM-dd";
/**
* The default date/time formatter
*/
public static final DateTimeFormatter DATE_FORMAT_PARSER = DateTimeFormat.forPattern(DEFAULT_DATE_FORMAT).withZoneUTC();
private static Gson gson = new GsonBuilder().create();
@Override
public String json(Object value) {
if (value == null) {
return null;
}
return gson.toJson(value);
}
@Override
public String string(Object value) {
if (value == null) {
return null;
}
if (value instanceof String) {
return (String) value;
}
return String.valueOf(value);
}
@Override
public Map map(Object value) {
if (value instanceof Map) {
return (Map) value;
}
String json;
if (value instanceof String) {
json = (String) value;
} else {
json = gson.toJson(value);
}
return gson.fromJson(json, _obj_map_token);
}
@Override
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy