Package external.JSON

Examples of external.JSON.JSONArray


    @Override
  protected Set<String> getUncachedGameKeys() {
      Set<String> theGameKeys = new HashSet<String>();
        try {
            JSONArray theArray = RemoteResourceLoader.loadJSONArray(theRepoURL + "/games/");
            for(int i = 0; i < theArray.length(); i++) {
                theGameKeys.add(theArray.getString(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theGameKeys;
View Full Code Here


    public void runTestingLoop() {
      try {
        Random r = new Random();
        JSONObject theBatchRequest = new JSONObject();
        JSONArray theRequests = new JSONArray();
        JSONObject theRequest = new JSONObject();
        theRequest.put("targetPort", 12345);
        theRequest.put("targetHost", "127.0.0.1");
        theRequest.put("timeoutClock", 3000);
        theRequest.put("forPlayerName", "");
        theRequests.put(theRequest);
        theRequests.put(theRequest);
        theBatchRequest.put("requests", theRequests);
        theBatchRequest.put("callbackURL", "http://127.0.0.1:12346");

        int nRequests = 0;
        while (true) {
View Full Code Here

                  }
                  this.activeRequests = activeRequests;
                }

                JSONObject theBatchJSON = new JSONObject(line);
                JSONArray theRequests = theBatchJSON.getJSONArray("requests");
                theRequestThreads = new HashSet<RunSingleRequestThread>();
                for (int i = 0; i < theRequests.length(); i++) {
                  JSONObject aRequest = theRequests.getJSONObject(i);
                  RunSingleRequestThread aRequestThread = new RunSingleRequestThread(aRequest);
                  theRequestThreads.add(aRequestThread);
                }
                callbackURL = theBatchJSON.getString("callbackURL");
View Full Code Here

              aRequestThread.start();
            }

            // Wait for all of the requests to finish; aggregate them into a batch response.
            JSONObject responseJSON = new JSONObject();
            JSONArray responses = new JSONArray();
            for (RunSingleRequestThread aRequestThread : theRequestThreads) {
              try {
          aRequestThread.join();
          responses.put(aRequestThread.getResponse());
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
            }
            try {
View Full Code Here

    public static JSONArray loadJSONArray(String theURL) throws JSONException, IOException {
      return loadJSONArray(theURL, 1);
    }
    public static JSONArray loadJSONArray(String theURL, int nMaxAttempts) throws JSONException, IOException {
        return new JSONArray(loadRaw(theURL, nMaxAttempts));
    }
View Full Code Here

        }
        // Add a data point to the frequency of second player wins, if this is a match that
        // has a second player and recorded goal values.
        if (matchJSON.has("goalValues") && matchJSON.getJSONArray("goalValues").length() > 1) {
          boolean secondPlayerWon = true;
          JSONArray goalValues = matchJSON.getJSONArray("goalValues");
          for (int i = 0; i < goalValues.length(); i++) {
            if (i == 1) continue;
            if (goalValues.getInt(i) >= goalValues.getInt(1)) {
              secondPlayerWon = false;
            }
          }
          data.secondPlayerWinFrequency.add(gameURL, secondPlayerWon ? 1 : 0);
        }
View Full Code Here

                    sb.append(renderSimpleCanonicalJSON(theObject.get(o.toString())));
                }
                sb.append('}');
                return sb.toString();
            } else if (x instanceof JSONArray) {
                JSONArray theArray = (JSONArray)x;
                StringBuffer sb = new StringBuffer();
                sb.append("[");
                int len = theArray.length();
                for (int i = 0; i < len; i += 1) {
                    if (i > 0) {
                        sb.append(",");
                    }
                    sb.append(renderSimpleCanonicalJSON(theArray.get(i)));
                }
                sb.append("]");
                return sb.toString();
            } else {
                if (x == null || x.equals(null)) {
                    return "null";
                }
                if (x instanceof JSONString) {
                    Object object;
                    try {
                        object = ((JSONString)x).toJSONString();
                    } catch (Exception e) {
                        throw new JSONException(e);
                    }
                    if (object instanceof String) {
                        return (String)object;
                    }
                    throw new JSONException("Bad value from toJSONString: " + object);
                }
                if (x instanceof Number) {
                    return JSONObject.numberToString((Number)x);
                }
                if (x instanceof Boolean || x instanceof JSONObject ||
                        x instanceof JSONArray) {
                    return x.toString();
                }
                if (x instanceof Map) {
                    return renderSimpleCanonicalJSON(new JSONObject((Map<?,?>)x)).toString();
                }
                if (x instanceof Collection) {
                    return renderSimpleCanonicalJSON(new JSONArray((Collection<?>)x)).toString();
                }
                if (x.getClass().isArray()) {
                    return renderSimpleCanonicalJSON(new JSONArray(x)).toString();
                }
                return JSONObject.quote(x.toString());
            }
        } catch (Exception e) {
            return null;
View Full Code Here

        this.moveHistory = new ArrayList<List<GdlTerm>>();
        this.stateHistory = new ArrayList<Set<GdlSentence>>();
        this.stateTimeHistory = new ArrayList<Date>();
        this.errorHistory = new ArrayList<List<String>>();

        JSONArray theMoves = theMatchObject.getJSONArray("moves");
        for (int i = 0; i < theMoves.length(); i++) {
            List<GdlTerm> theMove = new ArrayList<GdlTerm>();
            JSONArray moveElements = theMoves.getJSONArray(i);
            for (int j = 0; j < moveElements.length(); j++) {
                theMove.add(GdlFactory.createTerm(moveElements.getString(j)));
            }
            moveHistory.add(theMove);
        }
        JSONArray theStates = theMatchObject.getJSONArray("states");
        for (int i = 0; i < theStates.length(); i++) {
            Set<GdlSentence> theState = new HashSet<GdlSentence>();
            SymbolList stateElements = (SymbolList) SymbolFactory.create(theStates.getString(i));
            for (int j = 0; j < stateElements.size(); j++)
            {
                theState.add((GdlSentence)GdlFactory.create("( true " + stateElements.get(j).toString() + " )"));
            }
            stateHistory.add(theState);
        }
        JSONArray theStateTimes = theMatchObject.getJSONArray("stateTimes");
        for (int i = 0; i < theStateTimes.length(); i++) {
            this.stateTimeHistory.add(new Date(theStateTimes.getLong(i)));
        }
        if (theMatchObject.has("errors")) {
            JSONArray theErrors = theMatchObject.getJSONArray("errors");
            for (int i = 0; i < theErrors.length(); i++) {
                List<String> theMoveErrors = new ArrayList<String>();
                JSONArray errorElements = theErrors.getJSONArray(i);
                for (int j = 0; j < errorElements.length(); j++)
                {
                    theMoveErrors.add(errorElements.getString(j));
                }
                errorHistory.add(theMoveErrors);
            }
        }

        this.goalValues = new ArrayList<Integer>();
        try {
            JSONArray theGoalValues = theMatchObject.getJSONArray("goalValues");
            for (int i = 0; i < theGoalValues.length(); i++) {
                this.goalValues.add(theGoalValues.getInt(i));
            }
        } catch (JSONException e) {}

        // TODO: Add a way to recover cryptographic public keys and signatures.
        // Or, perhaps loading a match into memory for editing should strip those?

        if (theMatchObject.has("playerNamesFromHost")) {
            thePlayerNamesFromHost = new ArrayList<String>();
            JSONArray thePlayerNames = theMatchObject.getJSONArray("playerNamesFromHost");
            for (int i = 0; i < thePlayerNames.length(); i++) {
                thePlayerNamesFromHost.add(thePlayerNames.getString(i));
            }
        }
        if (theMatchObject.has("isPlayerHuman")) {
          isPlayerHuman = new ArrayList<Boolean>();
            JSONArray isPlayerHumanArray = theMatchObject.getJSONArray("isPlayerHuman");
            for (int i = 0; i < isPlayerHumanArray.length(); i++) {
              isPlayerHuman.add(isPlayerHumanArray.getBoolean(i));
            }
        }
  }
View Full Code Here

            theJSON.put("randomToken", randomToken);
            theJSON.put("startTime", startTime.getTime());
            theJSON.put("gameMetaURL", getGameRepositoryURL());
            theJSON.put("isCompleted", isCompleted);
            theJSON.put("isAborted", isAborted);
            theJSON.put("states", new JSONArray(renderArrayAsJSON(renderStateHistory(stateHistory), true)));
            theJSON.put("moves", new JSONArray(renderArrayAsJSON(renderMoveHistory(moveHistory), false)));
            theJSON.put("stateTimes", new JSONArray(renderArrayAsJSON(stateTimeHistory, false)));
            if (errorHistory.size() > 0) {
                theJSON.put("errors", new JSONArray(renderArrayAsJSON(renderErrorHistory(errorHistory), false)));
            }
            if (goalValues.size() > 0) {
                theJSON.put("goalValues", goalValues);
            }
            theJSON.put("previewClock", previewClock);
View Full Code Here

      } finally {
        br.close();
      }
      JSONObject playerListJSON = new JSONObject(pdata.toString());
      if (playerListJSON.has("hostports")) {
        JSONArray theHostports = playerListJSON.getJSONArray("hostports");
        for (int i = 0; i < theHostports.length(); i++) {
          try {
            addPlayerSilently(theHostports.get(i).toString());
          } catch (InvalidHostportException e) {
            e.printStackTrace();
          }
        }
      }
View Full Code Here

TOP

Related Classes of external.JSON.JSONArray

Copyright © 2018 www.massapicom. 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.