Java Code Examples for com.google.gson.JsonArray.iterator()

The following are Jave code examples for showing how to use iterator() of the com.google.gson.JsonArray class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Pand   File: DAOJson.java   Source Code and License Vote up 8 votes
@SuppressWarnings("unchecked")
public <T> ArrayList<T> parseAsArrayList(JsonElement json, T type) {
    ArrayList<T> newArray = new ArrayList<T>();
    Gson gson = new Gson();

    JsonArray array= json.getAsJsonArray();
    Iterator<JsonElement> iterator = array.iterator();

    while(iterator.hasNext()){
        JsonElement json2 = iterator.next();
        T object = (T) gson.fromJson(json2, (Class<?>)type);
        newArray.add(object);
    }

    return newArray;
}
 
Example 2
Project: RepositoryPattern   File: Response.java   Source Code and License Vote up 6 votes
public <T extends BaseModel> List<T> getList() {

        List<T> modelList = new ArrayList<T>();

        JsonElement outerJson = new JsonParser().parse(this.body);
        JsonArray array = outerJson.getAsJsonArray();
        Iterator iterator = array.iterator();

        while (iterator.hasNext()) {
            JsonElement innerJson = (JsonElement) iterator.next();
            Gson gson = new Gson();
            T code = gson.fromJson(innerJson, this.type);
            modelList.add(code);
        }
        return modelList;
    }
 
Example 3
Project: datarouter   File: JsonDatabeanTool.java   Source Code and License Vote up 5 votes
public static <PK extends PrimaryKey<PK>> List<PK> primaryKeysFromJson(Class<PK> pkClass, Fielder<PK> fielder,
		JsonArray json){
	List<PK> pks = new ArrayList<>();
	if(json == null){
		return pks;
	}
	Iterator<JsonElement> iter = json.iterator();
	while(iter.hasNext()){
		JsonObject jsonPk = iter.next().getAsJsonObject();
		PK pk = ReflectionTool.create(pkClass);
		primaryKeyFromJson(pk, fielder, jsonPk);
		pks.add(pk);
	}
	return pks;
}
 
Example 4
Project: datarouter   File: JsonDatabeanTool.java   Source Code and License Vote up 5 votes
public static <PK extends PrimaryKey<PK>,D extends Databean<PK,D>>
List<D> databeansFromJson(Supplier<D> databeanSupplier, DatabeanFielder<PK,D> fielder, JsonArray json){
	List<D> databeans = new ArrayList<>();
	if(json == null){
		return databeans;
	}
	Iterator<JsonElement> iter = json.iterator();
	while(iter.hasNext()){
		JsonObject jsonDatabean = iter.next().getAsJsonObject();
		D databean = databeanFromJson(databeanSupplier, fielder, jsonDatabean);
		databeans.add(databean);
	}
	return databeans;
}
 
Example 5
Project: TruenoNPC   File: TruenoNPC_v1_8_r3.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 6
Project: TruenoNPC   File: TruenoNPC_v1_8_r3.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Project: TruenoNPC   File: TruenoNPC_v1_9_r2.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 8
Project: TruenoNPC   File: TruenoNPC_v1_9_r2.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 9
Project: TruenoNPC   File: TruenoNPC_v1_10_r1.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 10
Project: TruenoNPC   File: TruenoNPC_v1_10_r1.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Project: TruenoNPC   File: TruenoNPC_v1_9_r1.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 12
Project: TruenoNPC   File: TruenoNPC_v1_9_r1.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Project: TruenoNPC   File: TruenoNPC_v1_11_r1.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 14
Project: TruenoNPC   File: TruenoNPC_v1_11_r1.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 15
Project: TruenoNPC   File: TruenoNPC_v1_12_r1.java   Source Code and License Vote up 5 votes
private SkinData getCachedSkin(){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
    Iterator it = oldskindata.iterator();
    SkinData skin = null;
    while(it.hasNext()){
        JsonElement element = (JsonElement) it.next();
        if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
            String value = element.getAsJsonObject().get("value").getAsString();
            String signature = element.getAsJsonObject().get("signature").getAsString();
            skin = new SkinData(value, signature);
        }
    }
    return skin;
}
 
Example 16
Project: TruenoNPC   File: TruenoNPC_v1_12_r1.java   Source Code and License Vote up 5 votes
private void cacheSkin(SkinData skindata){
    JsonObject jsonFile = getChacheFile(plugin);
    JsonArray newskindata = new JsonArray();
    if(jsonFile!=null){
        JsonArray oldskindata = jsonFile.getAsJsonArray("skindata");
        Iterator it = oldskindata.iterator();
        while(it.hasNext()){
            JsonElement element = (JsonElement) it.next();
            if(element.getAsJsonObject().get("id").getAsInt()==this.npcid){
                // element.getAsJsonObject().remove("value");
                //element.getAsJsonObject().remove("signature");
                //element.getAsJsonObject().addProperty("value", skindata.getValue());
                //element.getAsJsonObject().addProperty("signature", skindata.getSignature());
            }else {
                newskindata.add(element);
            }
        }
    }
    JsonObject skin = new JsonObject();
    skin.addProperty("id", this.npcid);
    skin.addProperty("value", skindata.getValue());
    skin.addProperty("signature", skindata.getSignature());
    newskindata.add(skin);

    JsonObject obj = new JsonObject();
    obj.add("skindata", newskindata);
    try {
        plugin.getDataFolder().mkdir();
        File file = new File(plugin.getDataFolder().getPath()+"/truenonpcdata.json");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.write(obj.toString());
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 17
Project: iosched-reader   File: DataExtractor.java   Source Code and License Vote up 5 votes
public JsonObject extractFromDataSources(JsonDataSources sources) {
  usedTags = new HashSet<String>();
  usedSpeakers = new HashSet<String>();

  JsonObject result = new JsonObject();
  result.add(OutputJsonKeys.MainTypes.rooms.name(), extractRooms(sources));
  JsonArray speakers = extractSpeakers(sources);

  JsonArray tags = extractTags(sources);
  result.add(OutputJsonKeys.MainTypes.video_library.name(), extractVideoSessions(sources));

  result.add(OutputJsonKeys.MainTypes.sessions.name(), extractSessions(sources));

  // Remove tags that are not used on any session (b/14419126)
  Iterator<JsonElement> tagsIt = tags.iterator();
  while (tagsIt.hasNext()) {
    JsonElement tag = tagsIt.next();
    String tagName = get(tag.getAsJsonObject(), OutputJsonKeys.Tags.tag).getAsString();
    if (!usedTags.contains(tagName)) {
      tagsIt.remove();
    }
  }

  // Remove speakers that are not used on any session:
  Iterator<JsonElement> it = speakers.iterator();
  while (it.hasNext()) {
    JsonElement el = it.next();
    String id = get(el.getAsJsonObject(), OutputJsonKeys.Speakers.id).getAsString();
    if (!usedSpeakers.contains(id)) {
      it.remove();
    }
  }

  result.add(OutputJsonKeys.MainTypes.speakers.name(), speakers);
  result.add(OutputJsonKeys.MainTypes.tags.name(), tags);
  return result;
}
 
Example 18
Project: dsp4e   File: DebugMessageTypeAdapterFactory.java   Source Code and License Vote up 5 votes
protected Object parseParams(Object params, String method) {
	if (isNull(params)) {
		return null;
	}
	if (!(params instanceof JsonElement)) {
		return params;
	}
	JsonElement rawParams = (JsonElement) params;
	Type[] parameterTypes = getParameterTypes(method);
	if (parameterTypes.length == 1) {
		return fromJson(rawParams, parameterTypes[0]);
	}
	if (parameterTypes.length > 1 && rawParams instanceof JsonArray) {
		JsonArray array = (JsonArray) rawParams;
		List<Object> parameters = new ArrayList<Object>(Math.max(array.size(), parameterTypes.length));
		int index = 0;
		Iterator<JsonElement> iterator = array.iterator();
		while (iterator.hasNext()) {
			Type parameterType = index < parameterTypes.length ? parameterTypes[index] : null;
			Object parameter = fromJson(iterator.next(), parameterType);
			parameters.add(parameter);
			index++;
		}
		while (index < parameterTypes.length) {
			parameters.add(null);
			index++;
		}
		return parameters;
	}
	return rawParams;
}