Package com.firefly.utils.json.parser

Source Code of com.firefly.utils.json.parser.CollectionParser

package com.firefly.utils.json.parser;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;

import com.firefly.utils.json.JsonReader;
import com.firefly.utils.json.exception.JsonException;

public class CollectionParser extends ComplexTypeParser {
 
  public CollectionParser(Type elementType) {
    super(elementType);
  }

  @Override
  @SuppressWarnings({ "rawtypes", "unchecked" })
  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 ','");
    }
  }

}
TOP

Related Classes of com.firefly.utils.json.parser.CollectionParser

TOP
Copyright © 2018 www.massapi.com. 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.