Package external.JSON

Examples of external.JSON.JSONObject


            }

            // Provide a listing of all of the metadata files for all of
            // the games, on request.
            if (reqURI.equals("/games/metadata")) {
                JSONObject theGameMetaMap = new JSONObject();
                for (String gameName : new File("games", "games").list()) {
                  if (shouldIgnoreFile(gameName)) continue;
                    try {
                        theGameMetaMap.put(gameName, new JSONObject(new String(getResponseBytesForURI("/games/" + gameName + "/"))));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                return theGameMetaMap.toString().getBytes();
            }

            // Accessing the folder containing a game should show the game's
            // associated metadata (which includes the contents of the folder).
            if(reqURI.endsWith("/") && reqURI.length() > 9) {
View Full Code Here


        // When the user requests a particular version, the metadata should always be for that version.
        // When the user requests the latest version, the metadata should always indicate the most recent version.
        public static byte[] adjustMetadataJSON(String reqURI, byte[] theMetaBytes, Integer nExplicitVersion, int nMaxVersion) throws IOException {
            try {
                JSONObject theMetaJSON = new JSONObject(new String(theMetaBytes));
                if (nExplicitVersion == null) {
                    theMetaJSON.put("version", nMaxVersion);
                } else {
                    theMetaJSON.put("version", nExplicitVersion);
                }
                String theRulesheet = new String(getResponseBytesForURI(reqURI.replace("METADATA",theMetaJSON.getString("rulesheet"))));
                MetadataCompleter.completeMetadataFromRulesheet(theMetaJSON, theRulesheet);
                return theMetaJSON.toString().getBytes();
            } catch (JSONException je) {
                throw new IOException(je);
            }
        }
View Full Code Here

      }
    }

    public String serializeToJSON() {
        try {
            JSONObject theGameObject = new JSONObject();
            theGameObject.put("theKey", getKey());
            theGameObject.put("theName", getName());
            theGameObject.put("theDescription", getDescription());
            theGameObject.put("theRepositoryURL", getRepositoryURL());
            theGameObject.put("theStylesheet", getStylesheet());
            theGameObject.put("theRulesheet", getRulesheet());

            return theGameObject.toString();
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
View Full Code Here

        }
    }

    public static Game loadFromJSON(String theSerializedGame) {
        try {
            JSONObject theGameObject = new JSONObject(theSerializedGame);

            String theKey = null;
            try {
                theKey = theGameObject.getString("theKey");
            } catch (Exception e) {}

            String theName = null;
            try {
                theName = theGameObject.getString("theName");
            } catch (Exception e) {}

            String theDescription = null;
            try {
                theDescription = theGameObject.getString("theDescription");
            } catch (Exception e) {}

            String theRepositoryURL = null;
            try {
                theRepositoryURL = theGameObject.getString("theRepositoryURL");
            } catch (Exception e) {}

            String theStylesheet = null;
            try {
                theStylesheet = theGameObject.getString("theStylesheet");
            } catch (Exception e) {}

            String theRulesheet = null;
            try {
              theRulesheet = theGameObject.getString("theRulesheet");
            } catch (Exception e) {}

            return new Game(theKey, theName, theDescription, theRepositoryURL, theStylesheet, theRulesheet);
        } catch(Exception e) {
            e.printStackTrace();
View Full Code Here

    public static Game loadSingleGame(String theGameURL) {
        String[] theSplitURL = theGameURL.split("/");
        String theKey = theSplitURL[theSplitURL.length-1];

        try {
            JSONObject theMetadata = getGameMetadataFromRepository(theGameURL);
            return loadSingleGameFromMetadata(theKey, theGameURL, theMetadata);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
View Full Code Here

        @Override
        public void run() {
            try {
                String theGameURL = theRepository.getGameURL(theKey);
                JSONObject theMetadata = RemoteGameRepository.getGameMetadataFromRepository(theGameURL);

                int repoVersion = theMetadata.getInt("version");
                String versionedRepoURL = RemoteGameRepository.addVersionToGameURL(theGameURL, repoVersion);

                Game myGameVersion = loadGameFromCache(theKey);
                String myVersionedRepoURL = "";
                if (myGameVersion != null)
View Full Code Here

            Set<String> theGameKeys = remoteRepository.getGameKeys();
            if (theGameKeys == null) return;

            // If the server offers a single combined metadata file, download that
            // and use it to avoid checking games that haven't gotten new versions.
            JSONObject bundledMetadata = remoteRepository.getBundledMetadata();
            if (bundledMetadata != null) {
                Set<String> unchangedKeys = new HashSet<String>();
                for (String theKey : theGameKeys) {
                    try {
                        Game myGameVersion = loadGameFromCache(theKey);
                        if (myGameVersion == null)
                            continue;

                        String remoteGameURL = remoteRepository.getGameURL(theKey);
                        int remoteVersion = bundledMetadata.getJSONObject(theKey).getInt("version");
                        String remoteVersionedGameURL = RemoteGameRepository.addVersionToGameURL(remoteGameURL, remoteVersion);

                        // Skip updating the game cache entry if the version is the same
                        // and the cache entry was written less than a week ago.
                        if (myGameVersion.getRepositoryURL().equals(remoteVersionedGameURL) &&
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) {
          theRequest.put("requestContent", "" + r.nextLong());
          theRequest.put("timestamp", System.currentTimeMillis());
          assertEquals("okay", RemoteResourceLoader.postRawWithTimeout("http://127.0.0.1:" + TiltyardRequestFarm.SERVER_PORT, theBatchRequest.toString(), 5000));
          nRequests++;
          Thread.sleep(10);
          int curNumSuccesses = nSuccesses.get();
          System.out.println("Successes so far: " + curNumSuccesses + " vs Requests: " + nRequests);
View Full Code Here

        public void run() {
            try {
                String line = HttpReader.readAsServer(conn);
                HttpWriter.writeAsServer(conn, "cool");
                conn.close();
                JSONObject batchResponseJSON = new JSONObject(line);
                assertTrue(batchResponseJSON.has("responses"));
                assertEquals(2, batchResponseJSON.getJSONArray("responses").length());
                JSONObject responseJSON = batchResponseJSON.getJSONArray("responses").getJSONObject(0);
                assertEquals(response, responseJSON.getString("responseType"));
                if (responseJSON.getString("responseType").equals("OK")) {
                    long original = Long.parseLong(new JSONObject(responseJSON.getString("originalRequest")).getString("requestContent"));
                  long response = Long.parseLong(responseJSON.getString("response"));
                  assertEquals(response, doMath(original));
                }
                nSuccesses.incrementAndGet();
            } catch (Exception e) {
                e.printStackTrace();
View Full Code Here

public class SignableJSONTest extends Assert {
  @Test
    public void testSimpleSigning() throws JSONException {
        EncodedKeyPair p = BaseCryptography.generateKeys();

        JSONObject x = new JSONObject("{3:{7:9,c:4,2:5,a:6},1:2,2:3,moves:14,states:21,alpha:'beta'}");
        assertFalse(SignableJSON.isSignedJSON(x));
        SignableJSON.signJSON(x, p.thePublicKey, p.thePrivateKey);
        assertTrue(SignableJSON.isSignedJSON(x));
        assertTrue(SignableJSON.verifySignedJSON(x));

        JSONObject x2 = new JSONObject(x.toString().replace(",", ", ").replace("{", "{ ").replace("}", "} "));
        assertTrue(SignableJSON.isSignedJSON(x2));
        assertTrue(SignableJSON.verifySignedJSON(x2));

        JSONObject x3 = new JSONObject("{1:2,2:3,3:4}");
        assertFalse(SignableJSON.isSignedJSON(x3));
    }
View Full Code Here

TOP

Related Classes of external.JSON.JSONObject

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.