List of usage examples for com.google.gson.stream JsonReader beginArray
public void beginArray() throws IOException
From source file:kihira.minicreatures.MiniCreatures.java
License:Open Source License
@SuppressWarnings("ResultOfMethodCallIgnored") private void loadPersonalityTypes(File configDir) { File personalityTypesFile = new File(configDir, File.separator + "MiniCreatures" + File.separator + "PersonalityTypes.json"); try {//from ww w. ja v a 2s.com Gson gson = GsonHelper.createGson(Mood.class); if (!personalityTypesFile.exists()) { //Create files/directories new File(configDir, File.separator + "MiniCreatures").mkdirs(); personalityTypesFile.createNewFile(); JsonWriter jsonWriter = new JsonWriter(new FileWriter(personalityTypesFile)); //TODO just copy a pre-genned file like marker beacons //Create defaults //Create default personalities jsonWriter.beginArray(); List<String> list = new ArrayList<String>(); list.add(EntityMiniPlayer.class.getName()); gson.toJson(gson.toJsonTree(new Mood("psychotic", list, new HashMap<String, MoodVariable>() { { put("happiness", new MoodVariable(35, 50)); put("hostility", new MoodVariable(40, 50)); } })), jsonWriter); gson.toJson(gson.toJsonTree(new Mood("coldblooded", list, new HashMap<String, MoodVariable>() { { put("happiness", new MoodVariable(-50, 0)); put("hostility", new MoodVariable(40, 50)); } })), jsonWriter); gson.toJson(gson.toJsonTree(new Mood("happy", list, new HashMap<String, MoodVariable>() { { put("happiness", new MoodVariable(10, 50)); put("hostility", new MoodVariable(-50, 10)); } })), jsonWriter); gson.toJson(gson.toJsonTree(new Mood("depressed", list, new HashMap<String, MoodVariable>() { { put("happiness", new MoodVariable(-50, 0)); put("hostility", new MoodVariable(-10, 10)); } })), jsonWriter); gson.toJson(gson.toJsonTree(new MoodTest("test")), jsonWriter); jsonWriter.endArray(); jsonWriter.close(); } //Load personality types JsonReader reader = new JsonReader(new FileReader(personalityTypesFile)); reader.beginArray(); while (reader.hasNext()) { Mood mood = gson.fromJson(reader, Mood.class); Personality.moodList.add(mood); logger.debug("Loaded mood %s", mood.toString()); } reader.endArray(); } catch (IOException e1) { e1.printStackTrace(); } }
From source file:me.ixfan.wechatkit.message.out.json.MassMessageGsonTypeAdapter.java
License:Open Source License
@Override public MessageForMassSend read(JsonReader in) throws IOException { MessageForMassSend.Filter filter = null; List<String> toUser = null; String msgType = null;/*from w ww .jav a 2s . c o m*/ String msgContent = null; in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "msgtype": msgType = in.nextString(); break; case "filter": in.beginObject(); String tagId = null; boolean isToAll = false; while (in.hasNext()) { switch (in.nextName()) { case "is_to_all": isToAll = in.nextBoolean(); break; case "tag_id": tagId = in.nextString(); break; default: break; } } in.endObject(); filter = new MessageForMassSend.Filter(tagId, isToAll); break; case "touser": in.beginArray(); toUser = new ArrayList<>(); while (in.hasNext()) { toUser.add(in.nextString()); } in.endArray(); break; case "text": case "image": case "voice": case "mpnews": case "mpvideo": case "wxcard": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "content": case "media_id": case "card_id": msgContent = in.nextString(); break; default: break; } } in.endObject(); break; default: break; } } in.endObject(); if (null != filter) { return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, filter.getTagId(), filter.isToAll()); } else if (null != toUser) { return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, toUser); } return null; }
From source file:me.j360.trace.core.internal.JsonCodec.java
License:Apache License
public List<List<Span>> readTraces(byte[] bytes) { JsonReader reader = jsonReader(bytes); List<List<Span>> result = new LinkedList<List<Span>>(); // cause we don't know how long it will be try {/*from w w w . j ava 2 s . c om*/ reader.beginArray(); while (reader.hasNext()) { reader.beginArray(); List<Span> trace = new LinkedList<Span>(); // cause we don't know how long it will be while (reader.hasNext()) { trace.add(SPAN_ADAPTER.fromJson(reader)); } reader.endArray(); result.add(trace); } reader.endArray(); return result; } catch (Exception e) { throw exceptionReading("List<List<Span>>", bytes, e); } }
From source file:me.j360.trace.core.internal.JsonCodec.java
License:Apache License
static <T> List<T> readList(JsonAdapter<T> adapter, byte[] bytes) { JsonReader reader = jsonReader(bytes); List<T> result;/*from ww w. j a v a 2 s.c om*/ try { reader.beginArray(); if (reader.hasNext()) { result = new LinkedList<T>(); // cause we don't know how long it will be } else { result = Collections.emptyList(); } while (reader.hasNext()) { result.add(adapter.fromJson(reader)); } reader.endArray(); return result; } catch (Exception e) { throw exceptionReading("List<" + adapter + ">", bytes, e); } }
From source file:nectec.thai.widget.address.repository.JsonParser.java
License:Apache License
public static <T> List<T> parse(Context context, String jsonFileName, Class<T> tClass) { List<T> allProvince = new ArrayList<>(); Gson gson = new Gson(); try {/* w ww .ja va 2 s . com*/ InputStream inputStream = context.getAssets().open(jsonFileName); JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { T message = gson.fromJson(reader, tClass); allProvince.add(message); } reader.endArray(); reader.close(); } catch (IOException e) { if (BuildConfig.DEBUG) Log.e(TAG, "parse() error.", e); } return allProvince; }
From source file:net.cnri.doregistrytools.registrar.replication.ReplicationPoller.java
License:Open Source License
public void getRemoteSchemas(CloseableHttpClient httpClient, RemoteRepositoryInfo remoteRepository) throws IOException, RepositoryException { String baseUri = remoteRepository.baseUri; if (baseUri.endsWith("/")) { baseUri = baseUri.substring(0, baseUri.length() - 1); }/*from w w w . ja va 2 s. c o m*/ String url = baseUri + "/replicate/schemas"; HttpGet request = new HttpGet(url); try { UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteRepository.username, remoteRepository.password); request.addHeader(new BasicScheme(Charset.forName("UTF-8")).authenticate(creds, request, null)); } catch (AuthenticationException e) { throw new AssertionError(e); } CloseableHttpResponse response = null; HttpEntity entity = null; try { response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { logger.error("Exception in getRemoteSchemas response code: " + statusCode); return; } entity = response.getEntity(); InputStream in = entity.getContent(); Reader reader = new InputStreamReader(in, "UTF-8"); JsonReader jsonReader = new JsonReader(reader); jsonReader.beginArray(); while (jsonReader.hasNext()) { registrar.addReplicatedObject(jsonReader, remoteRepository.baseUri); } } finally { if (entity != null) EntityUtils.consumeQuietly(entity); if (response != null) try { response.close(); } catch (IOException e) { } } }
From source file:net.cnri.doregistrytools.registrar.replication.ReplicationPoller.java
License:Open Source License
private void getRecentlyModifiedObjects(CloseableHttpClient httpClient, RemoteRepositoryInfo remoteRepository) throws IOException, ClientProtocolException, UnsupportedEncodingException, RepositoryException { String url = remoteRepository.baseUri + "/replicate?since=" + remoteRepository.lastTimestamp; if (remoteRepository.includeTypes != null) { url += "&" + listToParamString(remoteRepository.includeTypes, "include"); }//ww w. j a v a2 s . c o m if (remoteRepository.excludeTypes != null) { url += "&" + listToParamString(remoteRepository.excludeTypes, "exclude"); } HttpGet request = new HttpGet(url); // Credentials basicCreds = new Credentials(remoteRepository.username, remoteRepository.password); // String basicAuthHeader = basicCreds.getAuthHeader(); // request.addHeader("Authorization", basicAuthHeader); try { UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteRepository.username, remoteRepository.password); request.addHeader(new BasicScheme(Charset.forName("UTF-8")).authenticate(creds, request, null)); } catch (AuthenticationException e) { throw new AssertionError(e); } CloseableHttpResponse response = null; HttpEntity entity = null; long mostRecentModified = 0; try { response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { logger.error("Exception in getRecentlyModifiedObjects response code: " + statusCode); return; } entity = response.getEntity(); InputStream in = entity.getContent(); Reader reader = new InputStreamReader(in, "UTF-8"); JsonReader jsonReader = new JsonReader(reader); jsonReader.beginArray(); while (jsonReader.hasNext()) { long currentModified = registrar.addReplicatedObject(jsonReader, remoteRepository.baseUri); if (currentModified > mostRecentModified) { mostRecentModified = currentModified; } } if (mostRecentModified > 0) { if (System.currentTimeMillis() - mostRecentModified > 120000) mostRecentModified++; //TODO consider improving this remoteRepository.lastTimestamp = mostRecentModified; } } finally { if (entity != null) EntityUtils.consumeQuietly(entity); if (response != null) try { response.close(); } catch (IOException e) { } } }
From source file:net.evecom.android.EventAddActivity.java
License:Open Source License
/** * getTreeOne/* w w w. j a va 2s. c o m*/ */ private void getTreeOne(final String sbd_id) { progressDialog_getInfo = ProgressDialog.show(EventAddActivity.this, "", "..."); list.removeAll(list); new Thread(new Runnable() { @Override public void run() { Message msg_listData = new Message(); String strUrl = HttpUtil.BASE_URL + "teventAndroid/queryChildTree?pid=" + "0"; String strResult = null; try { strResult = connServerForResult(strUrl); } catch (Exception e) { msg_listData.what = MESSAGETYPE_02; handler1.sendMessage(msg_listData); if (null != e) { e.printStackTrace(); } return; } // DialogToast(strResult); // System.out.println(strResult); if (null == strResult || "".equals(strResult)) { msg_listData.what = MESSAGETYPE_02; handler1.sendMessage(msg_listData); return; } StringReader reader = new StringReader(strResult); JsonReader jsonReader = new JsonReader(reader); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); SysBaseDict dict = new SysBaseDict(); while (jsonReader.hasNext()) { String nextName = jsonReader.nextName(); String nextValue = ""; if (nextName.equals("id")) { nextValue = jsonReader.nextString(); dict.setSbdCode(nextValue); } else if (nextName.equals("name")) { nextValue = jsonReader.nextString(); dict.setSbdName(nextValue); } else if (nextName.equals("isparent")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } else if (nextName.equals("pid")) { nextValue = jsonReader.nextString(); dict.setSbdPid(nextValue); } else if (nextName.equals("type")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } // System.out.println(nextName+"="+nextValue); } list.add(dict); dict = null; jsonReader.endObject(); } jsonReader.endArray(); } catch (IOException e) { if (null != e) { e.printStackTrace(); } } if (null == list || list.size() < 1) { msg_listData.what = MESSAGETYPE_02; } else { msg_listData.what = MESSAGETYPE_01; } handler1.sendMessage(msg_listData); } }).start(); }
From source file:net.evecom.android.EventAddActivity.java
License:Open Source License
/** * getTreeTwo// w ww .j av a2s.co m */ private void getTreeTwo(final String oneStr) { progressDialog_getInfo = ProgressDialog.show(EventAddActivity.this, "", "..."); list.removeAll(list); final Message msg_listData = new Message(); new Thread(new Runnable() { @Override public void run() { String strUrl = HttpUtil.BASE_URL + "teventAndroid/queryChildTree?pid=" + oneStr; String strResult = null; try { strResult = connServerForResult(strUrl); } catch (Exception e) { msg_listData.what = MESSAGETYPE_02; handler2.sendMessage(msg_listData); if (null != e) { e.printStackTrace(); } return; } // DialogToast(strResult); // System.out.println(strResult); if (null == strResult || "".equals(strResult)) { msg_listData.what = MESSAGETYPE_02; handler2.sendMessage(msg_listData); return; } StringReader reader = new StringReader(strResult); JsonReader jsonReader = new JsonReader(reader); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); SysBaseDict dict = new SysBaseDict(); while (jsonReader.hasNext()) { String nextName = jsonReader.nextName(); String nextValue = ""; if (nextName.equals("id")) { nextValue = jsonReader.nextString(); dict.setSbdCode(nextValue); } else if (nextName.equals("name")) { nextValue = jsonReader.nextString(); dict.setSbdName(nextValue); } else if (nextName.equals("isparent")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } else if (nextName.equals("pid")) { nextValue = jsonReader.nextString(); dict.setSbdPid(nextValue); } else if (nextName.equals("type")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } // System.out.println(nextName+"="+nextValue); } list.add(dict); dict = null; jsonReader.endObject(); } jsonReader.endArray(); } catch (IOException e) { if (null != e) { e.printStackTrace(); } } if (null == list || list.size() < 1) { msg_listData.what = MESSAGETYPE_02; } else { msg_listData.what = MESSAGETYPE_01; } handler2.sendMessage(msg_listData); } }).start(); }
From source file:net.evecom.android.EventAddActivity.java
License:Open Source License
/** * getTreeThree//from ww w.j av a 2 s . c o m */ private void getTreeThree(final String sbd_id) { progressDialog_getInfo = ProgressDialog.show(EventAddActivity.this, "", "..."); list.removeAll(list); final Message msg_listData = new Message(); new Thread(new Runnable() { @Override public void run() { String strUrl = HttpUtil.BASE_URL + "teventAndroid/queryChildTree?pid=" + sbd_id; String strResult = null; try { strResult = connServerForResult(strUrl); } catch (Exception e) { msg_listData.what = MESSAGETYPE_02; handler3.sendMessage(msg_listData); if (null != e) { e.printStackTrace(); } return; } if (null == strResult || "".equals(strResult)) { msg_listData.what = MESSAGETYPE_02; handler3.sendMessage(msg_listData); return; } // System.out.println(strResult); StringReader reader = new StringReader(strResult); JsonReader jsonReader = new JsonReader(reader); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); SysBaseDict dict = new SysBaseDict(); while (jsonReader.hasNext()) { String nextName = jsonReader.nextName(); String nextValue = ""; if (nextName.equals("id")) { nextValue = jsonReader.nextString(); dict.setSbdCode(nextValue); } else if (nextName.equals("name")) { nextValue = jsonReader.nextString(); dict.setSbdName(nextValue); } else if (nextName.equals("isparent")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } else if (nextName.equals("pid")) { nextValue = jsonReader.nextString(); dict.setSbdPid(nextValue); } else if (nextName.equals("type")) { nextValue = jsonReader.nextString(); dict.setSbdType(nextValue); } } list.add(dict); dict = null; jsonReader.endObject(); } jsonReader.endArray(); } catch (IOException e) { if (null != e) { e.printStackTrace(); } } if (null == list || list.size() < 1) { msg_listData.what = MESSAGETYPE_02; } else { msg_listData.what = MESSAGETYPE_01; } handler3.sendMessage(msg_listData); } }).start(); }