Beispiel #1
0
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

      @SuppressWarnings("unchecked")
      Class<T> rawType = (Class<T>) type.getRawType();
      if (!rawType.isEnum()) {
        return null;
      }

      final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
      for (T constant : rawType.getEnumConstants()) {
        lowercaseToConstant.put(toLowercase(constant), constant);
      }

      return new TypeAdapter<T>() {
        public void write(JsonWriter out, T value) throws IOException {
          if (value == null) {
            out.nullValue();
          } else {
            out.value(toLowercase(value));
          }
        }

        public T read(JsonReader reader) throws IOException {
          if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return null;
          } else {
            return lowercaseToConstant.get(reader.nextString());
          }
        }
      };
    }
Beispiel #2
0
  private PropertyDescriptor getPropertyDescriptor(Class objClass, String property)
      throws IntrospectionException {
    synchronized (m_descriptorMapLock) {
      Map<String, PropertyDescriptor> propMap = m_descriptorMap.get(objClass);

      if (propMap == null) {
        propMap = new HashMap<String, PropertyDescriptor>();
        m_descriptorMap.put(objClass, propMap);

        BeanInfo beanInfo = Introspector.getBeanInfo(objClass);
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : descriptors) {
          propMap.put(getUnderscorePropertyName(descriptor.getName()), descriptor);
        }
      }

      return (propMap.get(property));
    }
  }