Package com.firefly.utils.json.support

Examples of com.firefly.utils.json.support.ParserMetaInfo


   
    if(reader.isEmptyObject())
      return obj;
   
    for (int i = 0;;i++) {
      ParserMetaInfo parser = parserMetaInfos[i];
      char[] field = reader.readField(parser.getPropertyName());
      if(!reader.isColon())
        throw new JsonException("missing ':'");
     
      if(field == null) { // 顺序相同,快速跳过
        parser.invoke(obj, reader);
      } else {
        ParserMetaInfo np = find(field);
        if(np != null)
          np.invoke(obj, reader);
        else
          reader.skipValue();
      }
     
      if(i == max)
        break;
     
      char ch = reader.readAndSkipBlank();
      if(ch == '}') // json string 的域数量比元信息少,提前结束
        return obj;

      if(ch != ',')
        throw new JsonException("missing ','");
    }
   
    char ch = reader.readAndSkipBlank();
    if(ch == '}')
      return obj;
   
    if(ch != ',')
      throw new JsonException("json string is not object format");
   
    for(;;) { // json string 的域数量比元信息多,继续读取
      char[] field = reader.readChars();
      if(!reader.isColon())
        throw new JsonException("missing ':'");
     
      ParserMetaInfo np = find(field);
      if(np != null)
        np.invoke(obj, reader);
      else
        reader.skipValue();
     
      char c = reader.readAndSkipBlank();
      if(c == '}') // 读到末尾
View Full Code Here


 
  protected ComplexTypeParser(Type elementType) {
    if(elementType instanceof ParameterizedType) { // 集合元素是参数类型
        ParameterizedType pt = (ParameterizedType)elementType;
        Class<?> rawClass = (Class<?>) (pt.getRawType());
        elementMetaInfo = new ParserMetaInfo();
         
          if(Collection.class.isAssignableFrom(rawClass)) {
            Type[] types2 = pt.getActualTypeArguments();
            if(types2.length != 1)
                throw new JsonException("collection actual type args length not equals 1");
           
            Type eleType = types2[0];
            elementMetaInfo.setType(getImplClass(rawClass));
            elementMetaInfo.setParser(new CollectionParser(eleType));
          } else if (Map.class.isAssignableFrom(rawClass)) {
            Type[] types2 = pt.getActualTypeArguments();
            if(types2.length != 2)
                throw new JsonException("map actual type args length not equals 2");
           
            Type key = types2[0];
              if (!((key instanceof Class) && key == String.class))
                throw new JsonException("map key type not string");
             
              Type eleType = types2[1];
              elementMetaInfo.setType(getImplClass(rawClass));
              elementMetaInfo.setParser(new MapParser(eleType));
          } else {
            elementMetaInfo.setType(rawClass);
            elementMetaInfo.setParser(ParserStateMachine.getParser(rawClass));
          }
      } else if (elementType instanceof Class) {
          Class<?> eleClass = (Class<?>) elementType; // 获取集合元素Parser
          elementMetaInfo = new ParserMetaInfo();
          elementMetaInfo.setType(eleClass);
          elementMetaInfo.setParser(ParserStateMachine.getParser(eleClass));
      } else if(elementType instanceof GenericArrayType) {
        GenericArrayType t = (GenericArrayType)elementType;
        Class<?> eleType = (Class<?>)t.getGenericComponentType();
        Object obj = Array.newInstance(eleType, 0);
        Class<?> rawClass = obj.getClass();
       
        elementMetaInfo = new ParserMetaInfo();
          elementMetaInfo.setType(rawClass);
          elementMetaInfo.setParser(ParserStateMachine.getParser(rawClass));
      } else {
        throw new JsonException("mot support type " + elementType);
      }
View Full Code Here

public class ArrayParser implements Parser {
 
  private ParserMetaInfo elementMetaInfo;
 
  public ArrayParser(Class<?> clazz) {
    elementMetaInfo = new ParserMetaInfo();
    elementMetaInfo.setType(clazz);
    elementMetaInfo.setParser(ParserStateMachine.getParser(clazz));
  }
View Full Code Here

      if (field != null
          && (Modifier.isTransient(field.getModifiers())
          || field.isAnnotationPresent(Transient.class)))
        continue;
           
            ParserMetaInfo parserMetaInfo = new ParserMetaInfo();
            parserMetaInfo.setPropertyNameString(propertyName);
            parserMetaInfo.setPropertyInvoke(new MethodInvoke(method));
            Class<?> type =  method.getParameterTypes()[0];
           
            if (Collection.class.isAssignableFrom(type)) {
              Type[] types = method.getGenericParameterTypes();
              if(types.length != 1 || !(types[0] instanceof ParameterizedType))
                throw new JsonException("not support the " + method);
             
              ParameterizedType paramType = (ParameterizedType) types[0];
              Type[] types2 = paramType.getActualTypeArguments();
              if(types2.length != 1)
                throw new JsonException("not support the " + method);
             
              Type elementType = types2[0];
              parserMetaInfo.setType(ComplexTypeParser.getImplClass(type));
              parserMetaInfo.setParser(new CollectionParser(elementType));
            } else if (Map.class.isAssignableFrom(type)) { // Map元信息构造
              Type[] types = method.getGenericParameterTypes();
              if(types.length != 1 || !(types[0] instanceof ParameterizedType))
                throw new JsonException("not support the " + method);
             
              ParameterizedType paramType = (ParameterizedType) types[0];
              Type[] types2 = paramType.getActualTypeArguments();
              if(types2.length != 2)
                throw new JsonException("not support the " + method);
             
              Type key = types2[0];
              if (!((key instanceof Class) && key == String.class))
                throw new JsonException("not support the " + method);
             
              Type elementType = types2[1];
              parserMetaInfo.setType(ComplexTypeParser.getImplClass(type));
              parserMetaInfo.setParser(new MapParser(elementType));
            } else { // 获取对象、枚举或者数组Parser
              parserMetaInfo.setType(type);
              parserMetaInfo.setParser(ParserStateMachine.getParser(type));
            }
            fieldSet.add(parserMetaInfo);
    }
   
    for(Field field : clazz.getFields()) { // public字段反序列化构造
      if(Modifier.isTransient(field.getModifiers()) || field.isAnnotationPresent(Transient.class) || Modifier.isStatic(field.getModifiers()))
        continue;
     
      field.setAccessible(true);
     
      ParserMetaInfo parserMetaInfo = new ParserMetaInfo();
            parserMetaInfo.setPropertyNameString(field.getName());
            parserMetaInfo.setPropertyInvoke(new FieldInvoke(field));
           
            Class<?> type = field.getType();
            if (Collection.class.isAssignableFrom(type)) {
              Type fieldType = field.getGenericType();
              if(!(fieldType instanceof ParameterizedType))
                throw new JsonException("not support the " + field);
             
              ParameterizedType paramType = (ParameterizedType)fieldType;
              Type[] types2 = paramType.getActualTypeArguments();
              if(types2.length != 1)
                throw new JsonException("not support the " + field);
             
              Type elementType = types2[0];
              parserMetaInfo.setType(ComplexTypeParser.getImplClass(type));
              parserMetaInfo.setParser(new CollectionParser(elementType));
            } else if (Map.class.isAssignableFrom(type)) { // Map元信息构造
              Type fieldType = field.getGenericType();
              if(!(fieldType instanceof ParameterizedType))
                throw new JsonException("not support the " + field);
             
              ParameterizedType paramType = (ParameterizedType) fieldType;
              Type[] types2 = paramType.getActualTypeArguments();
              if(types2.length != 2)
                throw new JsonException("not support the " + field);
             
              Type key = types2[0];
              if (!((key instanceof Class) && key == String.class))
                throw new JsonException("not support the " + field);
             
              Type elementType = types2[1];
              parserMetaInfo.setType(ComplexTypeParser.getImplClass(type));
              parserMetaInfo.setParser(new MapParser(elementType));
            } else { // 获取对象、枚举或者数组Parser
              parserMetaInfo.setType(type);
              parserMetaInfo.setParser(ParserStateMachine.getParser(type));
            }
            fieldSet.add(parserMetaInfo);
    }
   
    parserMetaInfos = fieldSet.toArray(EMPTY_ARRAY);
View Full Code Here

  }
 
  @Test
  public void test2() {
    ParserMetaInfo[] pp = DecodeCompiler.compile(CollectionObj.class);
    ParserMetaInfo p = pp[0];
    Assert.assertThat(p.getType().getName(), is(ArrayList.class.getName()));
   
    Parser parser = p.getParser();
    if(parser instanceof CollectionParser) {
      CollectionParser colp = (CollectionParser) parser;
      p = colp.getElementMetaInfo();
      Assert.assertThat(p.getType().getName(), is(LinkedList.class.getName()));
    }
   
    parser = p.getParser();
    if(parser instanceof CollectionParser) {
      CollectionParser colp = (CollectionParser) parser;
      p = colp.getElementMetaInfo();
      Assert.assertThat(p.getType().getName(), is(SimpleObj.class.getName()));
    }
  }
View Full Code Here

    }
  }
 
  public static void main(String[] args) {
    ParserMetaInfo[] pp = DecodeCompiler.compile(CollectionObj.class);
    ParserMetaInfo p = pp[0];
    test(p);
  }
View Full Code Here

TOP

Related Classes of com.firefly.utils.json.support.ParserMetaInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.