001    /*
002     * Copyright © 2008, 2012 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     * 
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn.gson;
027    
028    import java.lang.reflect.Type;
029    import java.util.Date;
030    
031    import com.google.gson.GsonBuilder;
032    import com.google.gson.JsonDeserializationContext;
033    import com.google.gson.JsonDeserializer;
034    import com.google.gson.JsonElement;
035    import com.google.gson.JsonParseException;
036    import com.google.gson.JsonPrimitive;
037    import com.google.gson.JsonSerializationContext;
038    import com.google.gson.JsonSerializer;
039    import com.softwarementors.extjs.djn.config.GlobalConfiguration;
040    
041    public class DefaultGsonBuilderConfigurator implements GsonBuilderConfigurator {
042    
043      private static class DateDeserializer implements JsonDeserializer<Date> {
044        public Date deserialize( JsonElement json, Type typeOfT,
045            JsonDeserializationContext context)
046        throws JsonParseException
047        {
048          assert json != null;
049          assert context != null;
050          assert typeOfT != null;
051    
052          if( !json.isJsonPrimitive() ) {
053            throw new JsonParseException( "The value for a Date must be a valid number");
054          }
055          
056          assert json instanceof JsonPrimitive; // To please Findugs!
057          JsonPrimitive primitivejson = (JsonPrimitive)json;
058          if( !primitivejson.isNumber()) {
059            throw new JsonParseException( "The value for a Date must be a valid number");
060          }
061          return new Date( primitivejson.getAsLong() );
062        }
063      }
064    
065      private static class DateSerializer implements JsonSerializer<Date> {
066        public JsonElement serialize( Date src, Type typeOfSrc,
067            JsonSerializationContext context) {
068          assert src != null;
069          assert context != null;
070          assert typeOfSrc != null;
071    
072          JsonElement result = new JsonPrimitive( Long.valueOf(src.getTime()) );
073          return result;
074        }
075      }
076    
077      public void configure(GsonBuilder builder, GlobalConfiguration configuration) {
078        assert builder != null;
079        assert configuration != null;
080    
081        if( configuration.getDebug() ) {
082          builder.setPrettyPrinting();
083        }
084        builder.serializeNulls();
085        builder.disableHtmlEscaping();
086        
087        builder.registerTypeAdapter( Date.class, new DateDeserializer());
088        builder.registerTypeAdapter( Date.class, new DateSerializer());
089      }
090    
091    }