List of usage examples for org.json JSONArray put
public JSONArray put(Object value)
From source file:org.schedulesdirect.grabber.Grabber.java
private void updateZip(NetworkEpgClient clnt) throws IOException, JSONException, JsonParseException { Set<String> completedListings = new HashSet<String>(); LOG.debug(String.format("Using %d worker threads", globalOpts.getMaxThreads())); pool = createThreadPoolExecutor();// ww w .j a va 2 s . c o m start = System.currentTimeMillis(); File dest = grabOpts.getTarget(); cachedSeriesIds = new HashSet<String>(); boolean rmDest = false; if (dest.exists()) { ZipEpgClient zipClnt = null; try { zipClnt = new ZipEpgClient(dest); if (!zipClnt.getUserStatus().getLastServerRefresh() .before(clnt.getUserStatus().getLastServerRefresh())) { LOG.info( "Current cache file contains latest data from Schedules Direct server; use --force-download to force a new download from server."); boolean force = grabOpts.isForce(); if (!force) return; else LOG.warn("Forcing an update of data with the server due to user request!"); } } catch (Exception e) { if (grabOpts.isKeep()) { LOG.error("Existing cache is invalid, keeping by user request!", e); return; } else { LOG.warn("Existing cache is invalid, deleting it; use --keep-bad-cache to keep existing cache!", e); rmDest = true; } } finally { if (zipClnt != null) try { zipClnt.close(); } catch (IOException e) { } if (rmDest && !dest.delete()) throw new IOException("Unable to delete " + dest); } } freshZip = !dest.exists(); try (FileSystem vfs = FileSystems.newFileSystem(new URI(String.format("jar:%s", dest.toURI())), Collections.singletonMap("create", "true"))) { if (freshZip) { Path target = vfs.getPath(ZipEpgClient.ZIP_VER_FILE); Files.write(target, Integer.toString(ZipEpgClient.ZIP_VER).getBytes(ZipEpgClient.ZIP_CHARSET)); } ProgramCache progCache = ProgramCache.get(vfs); Path lineups = vfs.getPath("lineups.txt"); Files.deleteIfExists(lineups); Path scheds = vfs.getPath("/schedules/"); if (!Files.isDirectory(scheds)) Files.createDirectory(scheds); Path maps = vfs.getPath("/maps/"); PathUtils.removeDirectory(maps); Files.createDirectory(maps); Path progs = vfs.getPath("/programs/"); if (!Files.isDirectory(progs)) Files.createDirectory(progs); Path logos = vfs.getPath("/logos/"); if (!Files.isDirectory(logos)) Files.createDirectory(logos); Path md5s = vfs.getPath("/md5s/"); if (!Files.isDirectory(md5s)) Files.createDirectory(md5s); Path cache = vfs.getPath(LOGO_CACHE); if (Files.exists(cache)) { String cacheData = new String(Files.readAllBytes(cache), ZipEpgClient.ZIP_CHARSET); logoCache = Config.get().getObjectMapper().readValue(cacheData, JSONObject.class); } else logoCache = new JSONObject(); Path seriesInfo = vfs.getPath("/seriesInfo/"); if (!Files.isDirectory(seriesInfo)) Files.createDirectories(seriesInfo); loadSeriesInfoIds(seriesInfo); missingSeriesIds = Collections.synchronizedSet(new HashSet<String>()); loadRetryIds(vfs.getPath(SERIES_INFO_DATA)); JSONObject resp = Config.get().getObjectMapper().readValue( factory.get(DefaultJsonRequest.Action.GET, RestNouns.LINEUPS, clnt.getHash(), clnt.getUserAgent(), globalOpts.getUrl().toString()).submitForJson(null), JSONObject.class); if (!JsonResponseUtils.isErrorResponse(resp)) Files.write(lineups, resp.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET)); else LOG.error("Received error response when requesting lineup data!"); for (Lineup l : clnt.getLineups()) { buildStationList(); JSONObject o = Config.get().getObjectMapper() .readValue( factory.get(DefaultJsonRequest.Action.GET, l.getUri(), clnt.getHash(), clnt.getUserAgent(), globalOpts.getUrl().toString()).submitForJson(null), JSONObject.class); Files.write(vfs.getPath("/maps", ZipEpgClient.scrubFileName(String.format("%s.txt", l.getId()))), o.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET)); JSONArray stations = o.getJSONArray("stations"); JSONArray ids = new JSONArray(); for (int i = 0; i < stations.length(); ++i) { JSONObject obj = stations.getJSONObject(i); String sid = obj.getString("stationID"); if (stationList != null && !stationList.contains(sid)) LOG.debug(String.format("Skipped %s; not listed in station file", sid)); else if (completedListings.add(sid)) { ids.put(sid); if (!grabOpts.isNoLogos()) { if (logoCacheInvalid(obj)) pool.execute(new LogoTask(obj, vfs, logoCache)); else if (LOG.isDebugEnabled()) LOG.debug(String.format("Skipped logo for %s; already cached!", obj.optString("callsign", null))); } else if (!logosWarned) { logosWarned = true; LOG.warn("Logo downloads disabled by user request!"); } } else LOG.debug(String.format("Skipped %s; already downloaded.", sid)); //pool.setMaximumPoolSize(5); // Processing these new schedules takes all kinds of memory! if (ids.length() == grabOpts.getMaxSchedChunk()) { pool.execute(new ScheduleTask(ids, vfs, clnt, progCache, factory)); ids = new JSONArray(); } } if (ids.length() > 0) pool.execute(new ScheduleTask(ids, vfs, clnt, progCache, factory)); } pool.shutdown(); try { LOG.debug("Waiting for SchedLogoExecutor to terminate..."); if (pool.awaitTermination(15, TimeUnit.MINUTES)) LOG.debug("SchedLogoExecutor: Terminated successfully."); else { failedTask = true; LOG.warn( "SchedLogoExecutor: Termination timed out; some tasks probably didn't finish properly!"); } } catch (InterruptedException e) { failedTask = true; LOG.warn( "SchedLogoExecutor: Termination interrupted); some tasks probably didn't finish properly!"); } Files.write(cache, logoCache.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE, StandardOpenOption.CREATE); ScheduleTask.commit(vfs); pool = createThreadPoolExecutor(); //pool.setMaximumPoolSize(5); // Again, we've got memory problems String[] dirtyPrograms = progCache.getDirtyIds(); progCache.markAllClean(); progCache = null; LOG.info(String.format("Identified %d program ids requiring an update!", dirtyPrograms.length)); Collection<String> progIds = new ArrayList<String>(); for (String progId : dirtyPrograms) { progIds.add(progId); if (progIds.size() == grabOpts.getMaxProgChunk()) { pool.execute(new ProgramTask(progIds, vfs, clnt, factory, missingSeriesIds, "programs", null, false)); progIds.clear(); } } if (progIds.size() > 0) pool.execute( new ProgramTask(progIds, vfs, clnt, factory, missingSeriesIds, "programs", null, false)); pool.shutdown(); try { LOG.debug("Waiting for ProgramExecutor to terminate..."); if (pool.awaitTermination(15, TimeUnit.MINUTES)) { LOG.debug("ProgramExecutor: Terminated successfully."); Iterator<String> itr = missingSeriesIds.iterator(); while (itr.hasNext()) { String id = itr.next(); if (cachedSeriesIds.contains(id)) itr.remove(); } if (missingSeriesIds.size() > 0) { LOG.info(String.format("Grabbing %d series info programs!", missingSeriesIds.size())); Set<String> retrySet = new HashSet<>(); try { new ProgramTask(missingSeriesIds, vfs, clnt, factory, missingSeriesIds, "seriesInfo", retrySet, true).run(); } catch (RuntimeException e) { LOG.error("SeriesInfo task failed!", e); Grabber.failedTask = true; } Path seriesInfoData = vfs.getPath(SERIES_INFO_DATA); if (retrySet.size() > 0) { StringBuilder sb = new StringBuilder(); for (String id : retrySet) sb.append(id + "\n"); Files.write(seriesInfoData, sb.toString().getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); } else if (Files.exists(seriesInfoData)) Files.delete(seriesInfoData); } } else { failedTask = true; LOG.warn("ProgramExecutor: Termination timed out; some tasks probably didn't finish properly!"); } } catch (InterruptedException e) { failedTask = true; LOG.warn("ProgramExecutor: Termination interrupted); some tasks probably didn't finish properly!"); } String userData = clnt.getUserStatus().toJson(); if (failedTask) { LOG.error("One or more tasks failed! Resetting last data refresh timestamp to zero."); SimpleDateFormat fmt = Config.get().getDateTimeFormat(); String exp = fmt.format(new Date(0L)); JSONObject o = Config.get().getObjectMapper().readValue(userData, JSONObject.class); o.put("lastDataUpdate", exp); userData = o.toString(2); } Path p = vfs.getPath(USER_DATA); Files.write(p, userData.getBytes(ZipEpgClient.ZIP_CHARSET), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); removeIgnoredStations(vfs); } catch (URISyntaxException e1) { throw new RuntimeException(e1); } finally { Runtime rt = Runtime.getRuntime(); LOG.info(String.format("MemStats:%n\tFREE: %s%n\tUSED: %s%n\t MAX: %s", FileUtils.byteCountToDisplaySize(rt.freeMemory()), FileUtils.byteCountToDisplaySize(rt.totalMemory()), FileUtils.byteCountToDisplaySize(rt.maxMemory()))); } }
From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerDTO.java
private Object createJSONObject(Object v) { if (v instanceof JSONProducer) { return ((JSONProducer) v).toJSON(); } else if (v instanceof Collection) { JSONArray a = new JSONArray(); for (Object o : ((Collection) v)) { a.put(createJSONObject(o)); }//from ww w .ja va 2 s . co m return a; } else { return v; } }
From source file:org.apache.tika.parser.ner.NamedEntityParserTest.java
@Test public void testParse() throws Exception { //test config is added to resources directory TikaConfig config = new TikaConfig(getClass().getResourceAsStream(CONFIG_FILE)); Tika tika = new Tika(config); JSONParser parser = new JSONParser(); String text = ""; HashMap<Integer, String> hmap = new HashMap<Integer, String>(); HashMap<String, HashMap<Integer, String>> outerhmap = new HashMap<String, HashMap<Integer, String>>(); int index = 0; //Input Directory Path String inputDirPath = "/Users/AravindMac/Desktop/polardata_json_grobid/application_pdf"; int count = 0; try {//www .j a va 2 s . c om File root = new File(inputDirPath); File[] listDir = root.listFiles(); for (File filename : listDir) { if (!filename.getName().equals(".DS_Store") && count < 3573) { count += 1; System.out.println(count); String absoluteFilename = filename.getAbsolutePath().toString(); // System.out.println(absoluteFilename); //Read the json file, parse and retrieve the text present in the content field. Object obj = parser.parse(new FileReader(absoluteFilename)); BufferedWriter bw = new BufferedWriter(new FileWriter(new File(absoluteFilename))); JSONObject jsonObject = (JSONObject) obj; text = (String) jsonObject.get("content"); Metadata md = new Metadata(); tika.parse(new ByteArrayInputStream(text.getBytes()), md); //Parse the content and retrieve the values tagged as the NER entities HashSet<String> set = new HashSet<String>(); set.addAll(Arrays.asList(md.getValues("X-Parsed-By"))); // Store values tagged as NER_PERSON set.clear(); set.addAll(Arrays.asList(md.getValues("NER_PERSON"))); hmap = new HashMap<Integer, String>(); index = 0; for (Iterator<String> i = set.iterator(); i.hasNext();) { String f = i.next(); hmap.put(index, f); index++; } if (!hmap.isEmpty()) { outerhmap.put("PERSON", hmap); } // Store values tagged as NER_LOCATION set.clear(); set.addAll(Arrays.asList(md.getValues("NER_LOCATION"))); hmap = new HashMap<Integer, String>(); index = 0; for (Iterator<String> i = set.iterator(); i.hasNext();) { String f = i.next(); hmap.put(index, f); index++; } if (!hmap.isEmpty()) { outerhmap.put("LOCATION", hmap); } //Store values tagged as NER_ORGANIZATION set.clear(); set.addAll(Arrays.asList(md.getValues("NER_ORGANIZATION"))); hmap = new HashMap<Integer, String>(); index = 0; for (Iterator<String> i = set.iterator(); i.hasNext();) { String f = i.next(); hmap.put(index, f); index++; } if (!hmap.isEmpty()) { outerhmap.put("ORGANIZATION", hmap); } // Store values tagged as NER_DATE set.clear(); set.addAll(Arrays.asList(md.getValues("NER_DATE"))); hmap = new HashMap<Integer, String>(); index = 0; for (Iterator<String> i = set.iterator(); i.hasNext();) { String f = i.next(); hmap.put(index, f); index++; } if (!hmap.isEmpty()) { outerhmap.put("DATE", hmap); } JSONArray array = new JSONArray(); array.put(outerhmap); if (!outerhmap.isEmpty()) { jsonObject.put("OpenNLP", array); //Add the NER entities to the json under NER key as a JSON array. } System.out.println(jsonObject); bw.write(jsonObject.toJSONString()); //Stringify thr JSON and write it back to the file bw.close(); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.mclinic.json.CohortConverter.java
public JSONArray serialize(final List<Cohort> cohorts) throws JSONException { JSONArray array = new JSONArray(); for (Cohort cohort : cohorts) array.put(serialize(cohort)); return array; }
From source file:com.yahoo.sql4d.query.groupby.LimitSpec.java
public Map<String, Object> getJsonMap() { Map<String, Object> map = new LinkedHashMap<>(); map.put("type", type); map.put("limit", limit); if (columns != null) { JSONArray columnsArray = new JSONArray(); for (Map.Entry<String, Direction> item : columns.entrySet()) { JSONObject itemJson = new JSONObject(); itemJson.put("dimension", item.getKey()); itemJson.put("direction", item.getValue().name()); columnsArray.put(itemJson); }/*w ww .jav a 2 s . c om*/ map.put("columns", columnsArray); } return map; }
From source file:com.google.android.apps.muzei.api.internal.SourceState.java
public JSONObject toJson() throws JSONException { JSONObject jsonObject = new JSONObject(); if (mCurrentArtwork != null) { jsonObject.put("currentArtwork", mCurrentArtwork.toJson()); }/*from w w w . j ava 2 s .co m*/ jsonObject.put("description", mDescription); jsonObject.put("wantsNetworkAvailable", mWantsNetworkAvailable); JSONArray commandsSerialized = new JSONArray(); for (UserCommand command : mUserCommands) { commandsSerialized.put(command.serialize()); } jsonObject.put("userCommands", commandsSerialized); return jsonObject; }
From source file:it.mb.whatshare.Utils.java
/** * Encodes the argument <tt>matrix</tt> into a JSON array. * //from ww w. j a v a 2s .c o m * <p> * Values are cast to <tt>double</tt> because of JSON lack for a primitive * <tt>float</tt> value. * * @param matrix * the matrix to be encoded * @return the matrix encoded into a JSON array, or <code>null</code> if * <tt>matrix</tt> is <code>null</code> or inconsistent (i.e. it * doesn't contain <tt>float</tt>'s) */ public static JSONArray matrixToJson(Matrix matrix) { if (matrix == null) return null; JSONArray array = new JSONArray(); float[] values = new float[9]; matrix.getValues(values); for (float value : values) { try { array.put(value); } catch (JSONException e) { e.printStackTrace(); return null; } } return array; }
From source file:com.github.cambierr.lorawanpacket.semtech.PullResp.java
@Override public void toRaw(ByteBuffer _bb) throws MalformedPacketException { super.toRaw(_bb); JSONObject json = new JSONObject(); if (!txpks.isEmpty()) { JSONArray txpk = new JSONArray(); for (Txpk s : txpks) { txpk.put(s.toJson()); }//from w ww .j av a2 s . co m json.put("txpk", txpks); } _bb.put(json.toString().getBytes()); }
From source file:com.wso2.mobile.mdm.api.DeviceInfo.java
/** *Returns the network operator name//from ww w . ja va2 s.com */ public JSONArray getNetworkOperatorName() { JSONArray jsonArray = null; final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (CommonUtilities.DEBUG_MODE_ENABLED) { Log.e("Network OP", tm.getSimOperatorName()); } if (tm.getSimOperatorName() != null && tm.getSimOperatorName() != "") { networkOperatorName = tm.getSimOperatorName(); } else { networkOperatorName = "No Sim"; } SharedPreferences mainPref = context.getSharedPreferences("com.mdm", Context.MODE_PRIVATE); try { jsonArray = new JSONArray(mainPref.getString("operators", "[]")); boolean simstatus = false; if (jsonArray.length() > 0) { for (int i = 0; i < jsonArray.length(); i++) { if ((jsonArray.getString(i) != null) && jsonArray.getString(i).trim().equals(tm.getSimOperatorName())) { simstatus = true; } } if (!simstatus) { jsonArray.put(tm.getSimOperatorName()); } } else { jsonArray.put(tm.getSimOperatorName()); } } catch (Exception e) { e.printStackTrace(); } Editor editor = mainPref.edit(); editor.putString("operators", jsonArray.toString()); editor.commit(); return jsonArray; }
From source file:org.eclipse.orion.server.cf.objects.App2.java
@PropertyDescription(name = CFProtocolConstants.KEY_ROUTES) private JSONArray getRoutesJSON() { try {// w ww .java2s.c o m JSONArray ret = new JSONArray(); if (routes == null) { routes = new ArrayList<Route>(); JSONArray routesJSON = appJSON.getJSONObject("entity").getJSONArray("routes"); for (int i = 0; i < routesJSON.length(); i++) { Route route = new Route().setCFJSON(routesJSON.getJSONObject(i)); routes.add(route); ret.put(route.toJSON()); } } return ret; } catch (JSONException e) { return null; } }