/** Attempts to create a CookieList from a malformed string. Expects a JSONException. */ @Test public void malFormedCookieListException() { String cookieStr = "thisCookieHasNoEqualsChar"; try { CookieList.toJSONObject(cookieStr); assertTrue("should throw an exception", false); } catch (JSONException e) { /** Not sure of the missing char, but full string compare fails */ assertTrue( "Expecting an exception message", e.getMessage().startsWith("Expected '=' and instead saw '") && e.getMessage().endsWith("' at 27 [character 28 line 1]")); } }
private void testJSONArray(Object array, String expected) { try { JSONArray jsonArray = JSONArray.fromObject(array); assertEquals(expected, jsonArray.toString()); } catch (JSONException jsone) { fail(jsone.getMessage()); } }
public static Object parse(String json) throws ShellException { try { final String input = json.trim(); if (input.isEmpty()) { return null; } if (input.charAt(0) == '{') { return new JSONObject(input).toMap(); } if (input.charAt(0) == '[') { return new JSONArray(input).toList(); } final Object value = JSONObject.stringToValue(input); if (value.equals(null)) { return null; } return value; } catch (JSONException e) { throw new ShellException("Could not parse value " + json + " " + e.getMessage()); } }
private void init(JSONObject json) throws TwitterException { id = ParseUtil.getLong("id", json); name = ParseUtil.getRawString("name", json); fullName = ParseUtil.getRawString("full_name", json); slug = ParseUtil.getRawString("slug", json); description = ParseUtil.getRawString("description", json); subscriberCount = ParseUtil.getInt("subscriber_count", json); memberCount = ParseUtil.getInt("member_count", json); uri = ParseUtil.getRawString("uri", json); mode = "public".equals(ParseUtil.getRawString("mode", json)); following = ParseUtil.getBoolean("following", json); createdAt = ParseUtil.getDate("created_at", json); try { if (!json.isNull("user")) { user = new UserJSONImpl(json.getJSONObject("user")); } } catch (JSONException jsone) { throw new TwitterException(jsone.getMessage() + ":" + json.toString(), jsone); } }
/** * Process HTTP request. * * @param act Action. * @param req Http request. * @param res Http response. */ private void processRequest(String act, HttpServletRequest req, HttpServletResponse res) { res.setContentType("application/json"); res.setCharacterEncoding("UTF-8"); GridRestCommand cmd = command(req); if (cmd == null) { res.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } if (!authChecker.apply(req.getHeader("X-Signature"))) { res.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return; } GridRestResponse cmdRes; Map<String, Object> params = parameters(req); try { GridRestRequest cmdReq = createRequest(cmd, params, req); if (log.isDebugEnabled()) log.debug("Initialized command request: " + cmdReq); cmdRes = hnd.handle(cmdReq); if (cmdRes == null) throw new IllegalStateException("Received null result from handler: " + hnd); byte[] sesTok = cmdRes.sessionTokenBytes(); if (sesTok != null) cmdRes.setSessionToken(U.byteArray2HexString(sesTok)); res.setStatus(HttpServletResponse.SC_OK); } catch (Exception e) { res.setStatus(HttpServletResponse.SC_OK); U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e); cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage()); } catch (Throwable e) { U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e); throw e; } JsonConfig cfg = new GridJettyJsonConfig(); // Workaround for not needed transformation of string into JSON object. if (cmdRes.getResponse() instanceof String) cfg.registerJsonValueProcessor(cmdRes.getClass(), "response", SKIP_STR_VAL_PROC); if (cmdRes.getResponse() instanceof GridClientTaskResultBean && ((GridClientTaskResultBean) cmdRes.getResponse()).getResult() instanceof String) cfg.registerJsonValueProcessor(cmdRes.getResponse().getClass(), "result", SKIP_STR_VAL_PROC); JSON json; try { json = JSONSerializer.toJSON(cmdRes, cfg); } catch (JSONException e) { U.error(log, "Failed to convert response to JSON: " + cmdRes, e); json = JSONSerializer.toJSON(new GridRestResponse(STATUS_FAILED, e.getMessage()), cfg); } try { if (log.isDebugEnabled()) log.debug("Parsed command response into JSON object: " + json.toString(2)); res.getWriter().write(json.toString()); if (log.isDebugEnabled()) log.debug( "Processed HTTP request [action=" + act + ", jsonRes=" + cmdRes + ", req=" + req + ']'); } catch (IOException e) { U.error(log, "Failed to send HTTP response: " + json.toString(2), e); } }
public List<Tweet> readFile(String fileLocation, int limit, String fromTweetId) { long startTime = System.currentTimeMillis(); int errors = 0; int added = 0; int ignored = 0; int skipped = 0; List<Tweet> tweets = new ArrayList<Tweet>(); try { // Read gzFile InputStream inputStream = new GZIPInputStream(new FileInputStream(fileLocation)); Reader decoder = new InputStreamReader(inputStream); BufferedReader br = new BufferedReader(decoder); String status; while ((status = br.readLine()) != null) { // Ignore garbage and log output lines, all statuses start with JSON bracket if (!status.equals("") && status.charAt(0) == '{') { try { JSONObject jsonObject = new JSONObject(status); // We use created_at as an indicator that this is a Tweet. if (jsonObject.has("created_at")) { Status statusObject = TwitterObjectFactory.createStatus(status); Tweet tweet = this.getTweetObjectFromStatus(statusObject); if (fromTweetId != null) { if (fromTweetId.equals(tweet.getId())) { this.log.write("StatusFileReader - Scanner pickup from " + fromTweetId); fromTweetId = null; } else { skipped++; } continue; } added++; tweets.add(tweet); if (limit > 0 && added >= limit) { break; } } else { ignored++; } } catch (JSONException e) { this.log.write( "Exception in StatusFileReader: Json Parse Failure on: " + status + ", " + e.getMessage()); } } else { ignored++; } } br.close(); decoder.close(); inputStream.close(); } catch (Exception e) { this.log.write( "Exception in StatusFileReader: Error Reading File: " + e.getClass().getName() + ": " + e.getMessage()); } long runTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime); double ops = ((double) added / (double) Math.max(runTimeSeconds, 1)); this.log.write( "StatusFileReader - " + fileLocation + " processed in " + runTimeSeconds + "s. " + added + " ok / " + errors + " errors / " + ignored + " ignored / " + skipped + " skipped. " + ops + " ops. Limit: " + limit + ", Fetch: " + tweets.size()); return tweets; }