Coverage Report - org.apache.shindig.social.core.util.BeanJsonLibConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanJsonLibConfig
100%
29/29
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util;
 19  
 
 20  
 import com.google.common.collect.Maps;
 21  
 
 22  
 import org.apache.shindig.social.opensocial.model.Address;
 23  
 import org.apache.shindig.social.opensocial.model.Enum;
 24  
 import org.apache.shindig.social.opensocial.model.ListField;
 25  
 import org.apache.shindig.social.opensocial.model.MediaItem;
 26  
 import org.apache.shindig.social.opensocial.model.Organization;
 27  
 import org.apache.shindig.social.opensocial.model.Url;
 28  
 
 29  
 import com.google.inject.Inject;
 30  
 import com.google.inject.Injector;
 31  
 import net.sf.ezmorph.MorpherRegistry;
 32  
 import net.sf.json.JsonConfig;
 33  
 import net.sf.json.util.EnumMorpher;
 34  
 import net.sf.json.util.JSONUtils;
 35  
 
 36  
 import java.util.HashMap;
 37  
 import java.util.Map;
 38  
 
 39  
 /**
 40  
  * A Json Config class suitable for serializing Shindig json and pojos.
 41  
  */
 42  
 
 43  
 public class BeanJsonLibConfig extends JsonConfig {
 44  
 
 45  
   /*
 46  
    * Register the Enum Morphers so that JSON -> Bean works correctly for enums.
 47  
    */
 48  
   static {
 49  1
     MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
 50  1
     morpherRegistry.registerMorpher(new EnumMorpher(Address.Field.class));
 51  1
     morpherRegistry.registerMorpher(new EnumMorpher(ListField.Field.class));
 52  1
     morpherRegistry.registerMorpher(new EnumMorpher(ListField.Field.class));
 53  1
     morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Field.class));
 54  1
     morpherRegistry.registerMorpher(new EnumMorpher(MediaItem.Type.class));
 55  1
     morpherRegistry.registerMorpher(new EnumMorpher(Enum.Drinker.class));
 56  1
     morpherRegistry.registerMorpher(new EnumMorpher(Enum.Field.class));
 57  1
     morpherRegistry.registerMorpher(new EnumMorpher(Enum.NetworkPresence.class));
 58  1
     morpherRegistry.registerMorpher(new EnumMorpher(Enum.Smoker.class));
 59  1
     morpherRegistry.registerMorpher(new JsonObjectToMapMorpher());
 60  1
   }
 61  
 
 62  
   /**
 63  
    * Construct the config with a Guice injector.
 64  
    * @param injector the Guice injector
 65  
    */
 66  
   @Inject
 67  10
   public BeanJsonLibConfig(Injector injector) {
 68  
     /*
 69  
      * This hook deals with the creation of new beans in the JSON -> Java Bean
 70  
      * conversion
 71  
      */
 72  10
     setNewBeanInstanceStrategy(new InjectorBeanInstanceStrategy(injector));
 73  
 
 74  
     /*
 75  
      * We are expecting null for nulls
 76  
      */
 77  10
     registerDefaultValueProcessor(String.class, new NullDefaultValueProcessor());
 78  
 
 79  10
     setJsonPropertyFilter(new NullPropertyFilter());
 80  10
     setJavaPropertyFilter(new NullPropertyFilter());
 81  
     // the classMap deals with the basic json string to bean conversion
 82  
 
 83  10
     Map<String, Class<?>> classMap = Maps.newHashMap();
 84  
 
 85  
     /*
 86  
      * mappings are required where there is a List of objects in the interface
 87  
      * with no indication of what type the list should contain. At the moment,
 88  
      * we are using 1 map for all json trees, as there is no conflict, but if
 89  
      * there is a map could be selected on the basis of the root object. It
 90  
      * would be better to do this with generics, but this is good enough and
 91  
      * compact enough for the moment.
 92  
      *
 93  
      */
 94  
     //
 95  
     // activity
 96  10
     classMap.put("mediaItems", MediaItem.class);
 97  
     // this may not be necessary
 98  10
     classMap.put("templateParams", Map.class);
 99  
     // BodyType needs no mappings
 100  
     // Message needs no mappings
 101  
     // Name needs no mappings
 102  
     // Organization needs no mappings
 103  
     // Url needs no mappings
 104  
     // Email needs no mappings
 105  
     // Phone Needs no mappings
 106  
     // Address Needs no mappings
 107  
     // MediaItem needs no mappings
 108  
 
 109  
     // Person map
 110  10
     classMap.put("addresses", Address.class);
 111  10
     classMap.put("phoneNumbers", ListField.class);
 112  10
     classMap.put("emails", ListField.class);
 113  10
     classMap.put("mediaItems", MediaItem.class);
 114  10
     classMap.put("jobs", Organization.class);
 115  10
     classMap.put("schools", Organization.class);
 116  10
     classMap.put("urls", Url.class);
 117  10
     setClassMap(classMap);
 118  
 
 119  10
   }
 120  
 
 121  
 }