示例#1
0
 void writeObject(Object bean, TypeAdapters typeAdapters, int depth) throws IOException {
   if (depth > MAX_DEPTH) {
     throw new JsonSerializeException("Maximum depth of nested object.");
   }
   if (bean == null) {
     writeNull();
     return;
   }
   Class<? extends Object> beanClass = bean.getClass();
   if (typeAdapters != null) {
     @SuppressWarnings("rawtypes")
     TypeAdapter adapter = typeAdapters.getTypeAdapter(beanClass);
     if (adapter != null) {
       @SuppressWarnings("unchecked")
       String result = adapter.serialize(bean);
       writeString(result);
       return;
     }
   }
   String className = beanClass.getName();
   PropertyGetters pgs = cachedGetters.get(className);
   if (pgs == null) {
     pgs = new PropertyGetters(bean.getClass());
     cachedGetters.put(className, pgs);
   }
   Map<String, PropertyGetter> getters = pgs.map;
   if (getters.isEmpty()) {
     writer.write("{}");
     return;
   }
   try {
     writer.write('{');
     boolean isFirst = true;
     for (String propertyName : getters.keySet()) {
       if (isFirst) {
         isFirst = false;
       } else {
         writer.write(',');
       }
       PropertyGetter pg = getters.get(propertyName);
       Object obj = pg.getProperty(bean);
       writer.write('\"');
       writer.write(propertyName);
       writer.write("\":");
       write(obj, depth);
     }
     writer.write('}');
   } catch (RuntimeException | IOException e) {
     throw e;
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
示例#2
0
 public FieldSpec(short id, boolean required, String name, TypeAdapter typeAdapter) {
   this.id = id;
   this.required = required;
   this.name = name;
   this.typeAdapter = typeAdapter;
   this.tField = new TField(name, typeAdapter.getTType(), id);
 }