Package com.google.gson

Examples of com.google.gson.JsonObject


          return;
        }

        // See if the page has a successful HTTP code
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObj    = jsonParser.parse(json).getAsJsonObject();

        int httpCode;

        if (jsonObj.has("http_result") == false) {
          reporter.incrCounter(this._counterGroup, "HTTP Code Missing", 1);
          return;
        }

        if (jsonObj.get("http_result").getAsInt() == 200) {
          reporter.incrCounter(this._counterGroup, "HTTP Success", 1);

          // only output counts for pages that were successfully retrieved
          output.collect(new Text(domain), new LongWritable(1));
        }
View Full Code Here


      try {
        // See if the page has a successful HTTP code
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObj    = jsonParser.parse(json).getAsJsonObject();

        boolean isSuccessful = false;

        String disposition = "[no status]";
        if (jsonObj.has("disposition"))
        {
          disposition = jsonObj.get("disposition").getAsString().trim().toUpperCase();

          if (disposition.equals("SUCCESS"))
            isSuccessful = true;
        }

        // Output a basic page count
        output.collect(new Text("Pages Requested\tTotal"), new LongWritable(1));

        output.collect(new Text("Pages Requested\t"+disposition), new LongWritable(1));

        // Output the HTTP result
        String httpResult = "[missing]";

        if (jsonObj.has("http_result"))
          httpResult = jsonObj.get("http_result").getAsString().trim().toUpperCase();

        output.collect(new Text("HTTP Code\t"+httpResult+" ("+disposition+")"), new LongWritable(1));

        // If the request was not successful, move to the next record
        if (isSuccessful == false)
          return;

        // Gather the host name
        try {

          URI uri = new URI(url);
          String host = uri.getHost();

          if (host == null || host.equals(""))
            throw new URISyntaxException(url, "Unable to gather host or no host found");

          // Gather the domain object
          InternetDomainName domainObj = InternetDomainName.from(host);

          // Output the TLD
          String publicSuffix = "[none]";

          if (domainObj.hasPublicSuffix())
            publicSuffix = domainObj.publicSuffix().name().trim().toLowerCase();

          output.collect(new Text("TLD\t"+publicSuffix), new LongWritable(1));

          // Output the private domain
          // WARNING - This dramatically increases the size of the output.
          String privateDomain = "[invalid]";

          if (domainObj.topPrivateDomain() != null)
            privateDomain = domainObj.topPrivateDomain().name().trim().toLowerCase();

          //output.collect(new Text("Domain\t"+privateDomain), new LongWritable(1));
        }
        catch (URISyntaxException ex) {
          output.collect(new Text("TLD\t[invalid URL]"), new LongWritable(1));
          reporter.incrCounter(this._counterGroup, "Invalid URLs", 1);
        }
        // Output MIME Type
        String mimeType = "[missing]";

        if (jsonObj.has("mime_type"))
          mimeType = jsonObj.get("mime_type").getAsString().trim().toLowerCase();

        output.collect(new Text("Type\t"+mimeType), new LongWritable(1));

        // Output Charset
        String charset = "[missing]";

        if (jsonObj.has("charset_detected"))
          charset = jsonObj.get("charset_detected").getAsString().trim().toUpperCase();

        output.collect(new Text("Charset\t"+charset), new LongWritable(1));

        // Download Size
        if (jsonObj.has("download_size") == true)
          output.collect(new Text("Content Size\t"), new LongWritable(jsonObj.get("download_size").getAsInt()));
      }
      catch (IOException ex) {
        throw ex;
      }
      catch (Exception ex) {
View Full Code Here

  public void testToJson() throws JsonParseException {
    JsonArray modules = graph.toJson();
    assertEquals(6, modules.size());
    for (int i = 0; i < modules.size(); i++) {
      JsonObject m = modules.get(i).getAsJsonObject();
      assertNotNull(m.get("name"));
      assertNotNull(m.get("dependencies"));
      assertNotNull(m.get("transitive-dependencies"));
      assertNotNull(m.get("inputs"));
    }
    JsonObject m = modules.get(3).getAsJsonObject();
    assertEquals("D", m.get("name").getAsString());
    assertEquals("[\"B\"]", m.get("dependencies").getAsJsonArray().toString());
    assertEquals(2,
        m.get("transitive-dependencies").getAsJsonArray().size());
    assertEquals("[]", m.get("inputs").getAsJsonArray().toString());
  }
View Full Code Here

   * @return List of module JSONObjects.
   */
  JsonArray toJson() {
    JsonArray modules = new JsonArray();
    for (JSModule module : getAllModules()) {
      JsonObject node = new JsonObject();
      try {
        node.add("name", new JsonPrimitive(module.getName()));
        JsonArray deps = new JsonArray();
        node.add("dependencies", deps);
        for (JSModule m : module.getDependencies()) {
          deps.add(new JsonPrimitive(m.getName()));
        }
        JsonArray transitiveDeps = new JsonArray();
        node.add("transitive-dependencies", transitiveDeps);
        for (JSModule m : getTransitiveDepsDeepestFirst(module)) {
          transitiveDeps.add(new JsonPrimitive(m.getName()));
        }
        JsonArray inputs = new JsonArray();
        node.add("inputs", inputs);
        for (CompilerInput input : module.getInputs()) {
          inputs.add(new JsonPrimitive(
              input.getSourceFile().getOriginalPath()));
        }
        modules.add(node);
View Full Code Here

      // extract embedded JSON
      try {
        String html = response.getResponseBody("UTF-8");

        JsonObject status = extractor.extractTweet(html);

        // save the requested id
        status.addProperty("requested_id", new Long(id));

        crawl.put(id, GSON.toJson(status));
        connections.decrementAndGet();

        return response;
View Full Code Here

      e.printStackTrace();
    }

    // Get metadata for the Module
    List<JsonElement> requestElements = new ArrayList<JsonElement>();
    JsonObject moduleJsonObject = gson.fromJson(output, JsonObject.class);

    JsonElement moduleNameElement = moduleJsonObject.get("moduleName");
    JsonElement moduleDescriptionElement = moduleJsonObject.get("moduleDescription");
    JsonElement moduleVersionElement = moduleJsonObject.get("moduleDescription");

    String moduleName = "default_module_name";
    String moduleDescription = "default_module_description";
    String moduleVersion = "1.0";

    if (moduleNameElement != null && !moduleNameElement.getAsString().isEmpty()) {
      moduleName = moduleNameElement.getAsString();
    }

    if (moduleDescriptionElement != null && !moduleDescriptionElement.getAsString().isEmpty()) {
      moduleDescription = moduleDescriptionElement.getAsString();
    }
   
    if (moduleVersionElement != null && !moduleDescriptionElement.getAsString().isEmpty()) {
      moduleDescription = moduleVersionElement.getAsString();
    }

    // Get all 'functions' as JsonObjects from the file and add them as a
    // list
    JsonElement functions = JsonUtils.findElement(moduleJsonObject, "functions");
    if (functions != null) {
      if (functions.isJsonArray()) {
        JsonArray requests_array = functions.getAsJsonArray();
        for (JsonElement requestElement : requests_array) {
          if (requestElement.isJsonObject()) {
            JsonObject request = requestElement.getAsJsonObject();
            requestElements.add(request);
          }
        }
      }
    }
    else{
      System.out.println("Error: Invalid file format. File does not contain \'functions\' element");
      System.exit(0);
    }

    // Parse all JsonObject requests into Requests
    List<Function> requests = new ArrayList<Function>();
    for (JsonElement jsonRequest : requestElements) {
      JsonObject request = JsonUtils.asJsonObject(jsonRequest);
      if (request != null) {
        if (request.get("functionName") != null
            && !request.get("functionName").getAsString().isEmpty()) {
          // Create Request from JSON Request
          requests.add( new Function(request));
        } else {
          System.out
              .println("WARNING: Function not generated because function is empty!");
View Full Code Here

  private static JsonElement search(Queue<JsonElement> queue, String elementName) {
    JsonElement ret = null;
    while (queue.size() > 0) {
      JsonElement element = queue.poll();
      if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        Set<Entry<String, JsonElement>> members = object.entrySet();
        for (Entry<String, JsonElement> member : members) {
          if (member.getKey().equals(elementName)) {
            return member.getValue();
          } else {
            queue.add(member.getValue());
View Full Code Here

    JsonElement responseFields = null;
    if(jsonResponse != null) responseFields = JsonUtils.findElement(jsonResponse, "fields");
   
    if (responseFields != null) {
      for (JsonElement responseField : JsonUtils.asJsonArray(responseFields)) {
        JsonObject response = JsonUtils.asJsonObject(responseField);
        String name = response.get("name").getAsString();
        String cssSelector = response.get("cssSelector").getAsString();
        this.addResponseField(name, cssSelector);
      }
    }
  }
View Full Code Here

  private void setHeaders(JsonElement headers) {
    JsonArray array_headers = JsonUtils.asJsonArray(headers);
    if (array_headers != null) {
      for (JsonElement jHeader : array_headers) {
        JsonObject header = JsonUtils.asJsonObject(jHeader);
        String name = header.get("name").getAsString();
        String value = header.get("value").getAsString();
        this.addHeader(name, value);
        if (Parameter.isParameter(value)) {
          this.addParameter(new Parameter(value));
        }
      }
View Full Code Here

    JsonArray array_postDataParams = null;
    if(jsonPostData != null) array_postDataParams = JsonUtils.asJsonArray(jsonPostData);
   
    if (array_postDataParams != null) {
      for (JsonElement postParam : array_postDataParams) {
        JsonObject param = JsonUtils.asJsonObject(postParam);
        String name = param.get("name").getAsString();
        String value = param.get("value").getAsString();
        this.addPostData(name, value);
        if (Parameter.isParameter(value)) {
          this.addParameter(new Parameter(value));
        }
      }
View Full Code Here

TOP

Related Classes of com.google.gson.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.