Package com.firefly.utils.json.exception

Examples of com.firefly.utils.json.exception.JsonException


  public Object convertTo(JsonReader reader, Class<?> clazz) {
    if(reader.isNull())
      return null;
   
    if(!reader.isObject())
      throw new JsonException("json string is not object format");
   
    Object obj = null;
    try {
      obj = clazz.newInstance();
    } catch (Throwable e) {
      e.printStackTrace();
    }
   
    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 == '}') // 读到末尾
        return obj;

     
      if(c != ',')
        throw new JsonException("missing ','");
    }
  }
View Full Code Here


        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

        Class<?> ret = HashMap.class;
        if(SortedMap.class.isAssignableFrom(clazz))
          ret = TreeMap.class;
        return ret;
      }
      throw new JsonException("not support the type " + clazz);
    } else
      return clazz;
  }
View Full Code Here

  public Object convertTo(JsonReader reader, Class<?> clazz) throws IOException {
    if(reader.isNull())
      return null;
   
    if(!reader.isObject())
      throw new JsonException("json string is not object format");
   
    Map obj = null;
    try {
      obj = (Map)clazz.newInstance();
    } catch (Throwable e) {
      e.printStackTrace();
    }
   
    if(reader.isEmptyObject())
      return obj;
   
    for(;;) {
      String key = reader.readString();
      if(!reader.isColon())
        throw new JsonException("missing ':'");
     
      obj.put(key, elementMetaInfo.getValue(reader));
     
      char ch = reader.readAndSkipBlank();
      if(ch == '}')
        return obj;

      if(ch != ',')
        throw new JsonException("missing ','");
    }
  }
View Full Code Here

  public Object convertTo(JsonReader reader, Class<?> clazz) throws IOException {
    if(reader.isNull())
      return null;
   
    if(!reader.isArray())
      throw new JsonException("json string is not array format");
   
    Collection obj = null;
    try {
      obj = (Collection)clazz.newInstance();
    } catch (Throwable e) {
      e.printStackTrace();
    }
   
    if(reader.isEmptyArray())
      return obj;
   
    for(;;) {
      obj.add(elementMetaInfo.getValue(reader));
     
      char ch = reader.readAndSkipBlank();
      if(ch == ']')
        return obj;

      if(ch != ',')
        throw new JsonException("missing ','");
    }
  }
View Full Code Here

  public Object convertTo(JsonReader reader, Class<?> clazz) throws IOException {
    if(reader.isNull())
      return null;
   
    if(!reader.isArray())
      throw new JsonException("json string is not array format");
   
    if(reader.isEmptyArray())
      return Array.newInstance(elementMetaInfo.getType(), 0);
   
    List<Object> obj = new ArrayList<Object>();
   
    for(;;) {
      obj.add(elementMetaInfo.getValue(reader));
     
      char ch = reader.readAndSkipBlank();
      if(ch == ']')
        return copyOf(obj);

      if(ch != ',')
        throw new JsonException("missing ','");
    }
  }
View Full Code Here

          if (clazz.isEnum()) {
            ret = new EnumParser(clazz);
            PARSER_MAP.put(clazz, ret);
          } else if (Collection.class.isAssignableFrom(clazz)
              || Map.class.isAssignableFrom(clazz)) {
            throw new JsonException("not support type " + clazz);
          } else if (clazz.isArray()) {
            Class<?> elementClass = clazz.getComponentType();
            ret = new ArrayParser(elementClass);
            PARSER_MAP.put(clazz, ret);
          } else {
View Full Code Here

      ret = false;
   
    if(isString) {
      ch = readAndSkipBlank();
      if(ch != '"')
        throw new JsonException("read boolean error");
    }
   
    return ret;
  }
View Full Code Here

   
    if(!negative) {
      if(VerifyUtils.isDigit(ch))
        value = (value << 3) + (value << 1) + (ch - '0');
      else
        throw new JsonException("read int error, charactor \"" + ch + "\" is not integer");
    }
   
    for(;;) {
      ch = (char)read();
      if(VerifyUtils.isDigit(ch))
        value = (value << 3) + (value << 1) + (ch - '0');
      else {
        if(isString) {
          if(ch == '"')
            break;
        } else {
          if (isEndFlag(ch)) {
            pos--;
            break;
          } else
            throw new JsonException("read int error, charactor \"" + ch + "\" is not integer");
        }
      }
     
      if(pos >= limit)
        break;
View Full Code Here

   
    if(!negative) {
      if(VerifyUtils.isDigit(ch))
        value = (value << 3) + (value << 1) + (ch - '0');
      else
        throw new JsonException("read int error, charactor \"" + ch + "\" is not integer");
    }
   
    for(;;) {
      ch = (char)read();
      if(VerifyUtils.isDigit(ch))
        value = (value << 3) + (value << 1) + (ch - '0');
      else {
        if(isString) {
          if(ch == '"')
            break;
        } else {
          if (isEndFlag(ch)) {
            pos--;
            break;
          } else
            throw new JsonException("read int error, charactor \"" + ch + "\" is not integer");
        }
      }
     
      if(pos >= limit)
        break;
View Full Code Here

TOP

Related Classes of com.firefly.utils.json.exception.JsonException

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.