Package org.jtester.json.decoder.array

Source Code of org.jtester.json.decoder.array.CollectionDecoder

package org.jtester.json.decoder.array;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.jtester.bytecode.reflector.helper.ClazzHelper;
import org.jtester.exception.JSONException;
import org.jtester.json.JSON;
import org.jtester.json.decoder.ArrayDecoder;
import org.jtester.json.helper.JSONObject;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class CollectionDecoder extends ArrayDecoder<Collection> {

  public CollectionDecoder(Class clazz) {
    super(clazz);
  }

  public CollectionDecoder() {
    super(ArrayList.class);
  }

  @Override
  protected Collection newArraysObject(int size) {
    if (ClazzHelper.isInterfaceOrAbstract(this.realTargetType)) {
      throw new JSONException("the type[" + this.realTargetType.getName()
          + "] is an interface or abstract class,that can't be instnaced.");
    }
    Collection target = (Collection) ClazzHelper.newInstance(this.realTargetType == null ? this.clazz
        : this.realTargetType);
    return target;
  }

  @Override
  protected void setObjectByIndex(Collection target, int index, JSONObject json, Map<String, Object> references) {
    Object value = JSON.toObject(json, references);
    target.add(value);
  }
}
TOP

Related Classes of org.jtester.json.decoder.array.CollectionDecoder

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.