Package com.google.gson

Examples of com.google.gson.JsonArray


    EasyMock.expect(gatewayFactory.create("http://www.asdf.com", "/asdf")).andReturn(
        gateway);
    EasyMock.expect(mockFactory.create(EasyMock.isA(MockResponse.class))).andReturn(
        mock);
    control.replay();
    JsonArray jsonConfig = new JsonParser().parse(CONFIG).getAsJsonArray();
    configuration.updateConfiguration(jsonConfig);
    assertEquals(2, configuration.getMatchers().size());
    assertEquals(gateway,
        configuration.getRequestHandler(new RequestMatcher(ANY, "/asdf")));
    assertEquals(mock,
View Full Code Here


      + " - {matcher: /five, responseText: \"hello world\"}\n";
    ByteArrayInputStream bias = new ByteArrayInputStream(configFile.getBytes());
    ConfigurationParser parser = new YamlParser();

    Configuration config = parser.parse(new InputStreamReader(bias), null);
    JsonArray gatewayConfig = config.getGatewayConfiguration();
    assertEquals(5, gatewayConfig.size());

    JsonObject first = gatewayConfig.get(0).getAsJsonObject();
    assertTrue(first.has(GatewayConfiguration.MATCHER));
    assertEquals("/asdf*", first.get(GatewayConfiguration.MATCHER).getAsString());
    assertTrue(first.has(GatewayConfiguration.SERVER));
    assertEquals("http://www.google.com", first.get(GatewayConfiguration.SERVER).getAsString());

    JsonObject second = gatewayConfig.get(1).getAsJsonObject();
    assertTrue(second.has(GatewayConfiguration.MATCHER));
    assertEquals("/two", second.get(GatewayConfiguration.MATCHER).getAsString());
    assertTrue(second.has(MockResponse.STATUS));
    assertEquals(200, second.get(MockResponse.STATUS).getAsInt());

    JsonObject third = gatewayConfig.get(2).getAsJsonObject();
    assertTrue(third.has(GatewayConfiguration.MATCHER));
    assertEquals("/three", third.get(GatewayConfiguration.MATCHER).getAsString());
    assertTrue(third.has(MockResponse.RESPONSE_HEADERS));

    JsonObject responseHeaders = third.get(MockResponse.RESPONSE_HEADERS).getAsJsonObject();
    assertEquals(2, responseHeaders.entrySet().size());
    assertTrue(responseHeaders.has("X-0"));
    assertEquals("0", responseHeaders.get("X-0").getAsString());
    assertTrue(responseHeaders.has("X-1"));
    assertEquals("1", responseHeaders.get("X-1").getAsString());

    JsonObject fourth = gatewayConfig.get(3).getAsJsonObject();
    assertTrue(fourth.has(GatewayConfiguration.MATCHER));
    assertEquals("/four", fourth.get(GatewayConfiguration.MATCHER).getAsString());
    assertTrue(fourth.has(MockResponse.CONTENT_TYPE));
    assertEquals("text/plain", fourth.get(MockResponse.CONTENT_TYPE).getAsString());

    JsonObject fifth = gatewayConfig.get(4).getAsJsonObject();
    assertTrue(fifth.has(GatewayConfiguration.MATCHER));
    assertEquals("/five", fifth.get(GatewayConfiguration.MATCHER).getAsString());
    assertTrue(fifth.has(MockResponse.RESPONSE_TEXT));
    assertEquals("hello world", fifth.get(MockResponse.RESPONSE_TEXT).getAsString());
  }
View Full Code Here

         return firewall;
      }
   }

   private static JsonArray buildArrayOfStrings(Set<String> strings) {
      JsonArray array = new JsonArray();
      for (String string : strings) {
         array.add(new JsonPrimitive(string));
      }
      return array;
   }
View Full Code Here

              JsonParseException {
         Operation.Builder operationBuilder = ((Operation) context.deserialize(json,
                 OperationInternal.class)).toBuilder();
         JsonObject error = json.getAsJsonObject().getAsJsonObject("error");
         if (error != null) {
            JsonArray array = error.getAsJsonArray("errors");
            if (array != null) {
               for (JsonElement element : array) {
                  operationBuilder.addError((Operation.Error) context.deserialize(element, Operation.Error.class));
               }
            }
View Full Code Here

      public JsonElement serialize(InstanceTemplate src, Type typeOfSrc, JsonSerializationContext context) {
         InstanceTemplateInternal template = new InstanceTemplateInternal(src);
         JsonObject instance = (JsonObject) context.serialize(template, InstanceTemplateInternal.class);

         // deal with network
         JsonArray networkInterfaces = new JsonArray();
         for (InstanceTemplate.NetworkInterface networkInterface : template.getNetworkInterfaces()){
            networkInterfaces.add(context.serialize(networkInterface, InstanceTemplate.NetworkInterface.class));
         }
         instance.add("networkInterfaces", networkInterfaces);

         // deal with persistent disks
         if (src.getDisks() != null && !src.getDisks().isEmpty()) {
            JsonArray disks = new JsonArray();
            for (InstanceTemplate.PersistentDisk persistentDisk : src.getDisks()) {
               JsonObject disk = (JsonObject) context.serialize(persistentDisk, InstanceTemplate.PersistentDisk.class);
               disk.addProperty("type", "PERSISTENT");
               disks.add(disk);
            }
            instance.add("disks", disks);
         }

         // deal with metadata
View Full Code Here

              JsonParseException {
         Instance.Builder instanceBuilder = ((Instance) context.deserialize(json,
                 InstanceInternal.class)).toBuilder();
         JsonObject object = (JsonObject) json;
         if (object.get("disks") != null) {
            JsonArray disks = (JsonArray) object.get("disks");
            for (JsonElement element : disks) {
               JsonObject disk = (JsonObject) element;
               if (disk.get("type").getAsString().equals("PERSISTENT")) {
                  instanceBuilder.addDisk((Instance.PersistentAttachedDisk) context.deserialize(disk,
                          Instance.PersistentAttachedDisk.class));
View Full Code Here

      @Override
      public Metadata deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws
              JsonParseException {
         ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
         JsonObject metadata = json.getAsJsonObject();
         JsonArray items = metadata.getAsJsonArray("items");
         if (items != null) {
            for (JsonElement element : items) {
               JsonObject object = element.getAsJsonObject();
               builder.put(object.get("key").getAsString(), object.get("value").getAsString());
            }
View Full Code Here

      @Override
      public JsonElement serialize(Metadata src, Type typeOfSrc, JsonSerializationContext context) {
         JsonObject metadataObject = new JsonObject();
         metadataObject.add("kind", new JsonPrimitive("compute#metadata"));
         JsonArray items = new JsonArray();
         for (Map.Entry<String, String> entry : src.entrySet()) {
            JsonObject object = new JsonObject();
            object.addProperty("key", entry.getKey());
            object.addProperty("value", entry.getValue());
            items.add(object);
         }
         metadataObject.add("items", items);
         return metadataObject;
      }
View Full Code Here

         }
         if (!src.getTargetTags().isEmpty()) {
            firewall.add("targetTags", buildArrayOfStrings(src.getTargetTags()));
         }
         if (!src.getAllowed().isEmpty()) {
            JsonArray rules = new JsonArray();
            for (Rule rule : src.getAllowed()) {
               rules.add(context.serialize(rule, Firewall.Rule.class));
            }
            firewall.add("allowed", rules);
         }
         return firewall;
      }
View Full Code Here

              JsonParseException {
         JsonObject rule = json.getAsJsonObject();
         Rule.Builder builder = Rule.builder();
         builder.IPProtocol(Rule.IPProtocol.fromValue(rule.get("IPProtocol").getAsString()));
         if (rule.get("ports") != null) {
            JsonArray ports = (JsonArray) rule.get("ports");
            for (JsonElement port : ports) {
               String portAsString = port.getAsString();
               if (portAsString.contains("-")) {
                  String[] split = portAsString.split("-");
                  builder.addPortRange(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
View Full Code Here

TOP

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