Package com.google.gson

Examples of com.google.gson.JsonArray


      @Override
      public JsonElement serialize(Firewall.Rule src, Type typeOfSrc, JsonSerializationContext context) {
         JsonObject ruleObject = new JsonObject();
         ruleObject.addProperty("IPProtocol", src.getIPProtocol().value());
         if (src.getPorts() != null && !src.getPorts().isEmpty()) {
            JsonArray ports = new JsonArray();
            for (Range<Integer> range : src.getPorts().asRanges()) {
               ports.add(new JsonPrimitive(range.lowerEndpoint() == range.upperEndpoint() ? range.lowerEndpoint() + "" :
                       range.lowerEndpoint() + "-" + range.upperEndpoint()));
            }
            ruleObject.add("ports", ports);
         }
         return ruleObject;
View Full Code Here


        return -1;
    }
   
    @Override
    public void writeTo(List objs, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
        JsonArray array = new JsonArray();
        for(Object obj : objs) {
            JsonParser parser = new JsonParser();
            try {
                JAXBContext context = JAXBContext.newInstance(obj.getClass());
                Marshaller marshaller = context.createMarshaller();
                StringWriter sw = new StringWriter();
                JSONJAXBContext.getJSONMarshaller(marshaller).marshallToJSON(obj, sw);
                array.add(parser.parse(sw.toString()));
            }
            catch(JAXBException e) {
                array.add(new JsonPrimitive(obj.toString()));
            }
        }
        out.write(new Gson().toJson(array).getBytes());
    }
View Full Code Here

        assertThat(using(GSON_CONFIGURATION).parse("{\"val\": 1}").read("val", Double.class)).isEqualTo(1D);
    }

    @Test
    public void list_of_numbers() {
        JsonArray objs =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price");

        assertThat(objs.iterator()).extracting("asDouble").containsExactly(8.95D, 12.99D, 8.99D, 22.99D);

    }
View Full Code Here

        return obj.toString();
    }

    @Override
    public Object createArray() {
        return new JsonArray();
    }
View Full Code Here

    @Override
    public void setProperty(Object obj, Object key, Object value) {
        if (isMap(obj))
            toJsonObject(obj).add(key.toString(), createJsonElement(value));
        else {
            JsonArray array = toJsonArray(obj);
            int index;
            if (key != null) {
                index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
            } else {
                index = array.size();
            }
            if (index == array.size()) {
                array.add(createJsonElement(value));
            } else {
                array.set(index, createJsonElement(value));
            }
        }
    }
View Full Code Here

    @SuppressWarnings("unchecked")
    public void removeProperty(Object obj, Object key) {
        if (isMap(obj))
            toJsonObject(obj).remove(key.toString());
        else {
            JsonArray array = toJsonArray(obj);
            int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
            array.remove(index);
        }
    }
View Full Code Here

    }

    @Override
    public Iterable<?> toIterable(Object obj) {
        if (isArray(obj)) {
            JsonArray arr = toJsonArray(obj);
            List<Object> values = new ArrayList<Object>(arr.size());
            for (Object o : arr) {
                values.add(unwrap(o));
            }
            return values;
        } else {
View Full Code Here

    assertEquals(EventType.DOCUMENT_CHANGED, calledEvents.get(1));
    assertEquals(EventType.WAVELET_TAGS_CHANGED, calledEvents.get(2));

    // Assert that the outgoing operation bundle contains robot.notify() op.
    JsonParser jsonParser = new JsonParser();
    JsonArray ops = jsonParser.parse(mockWriter.getString()).getAsJsonArray();
    assertEquals(1, ops.size());

    JsonObject op = ops.get(0).getAsJsonObject();
    assertEquals(OperationType.ROBOT_NOTIFY.method(),
        op.get(RequestProperty.METHOD.key()).getAsString());

    JsonObject params = op.get(RequestProperty.PARAMS.key()).getAsJsonObject();
    assertEquals("0.22", params.get(ParamsProperty.PROTOCOL_VERSION.key()).getAsString());
View Full Code Here

  /**
   * Faithfully serializes a 64-bit long value as a two-number array.
   */
  public static JsonArray toJson(long value) {
    JsonArray arr = new JsonArray();
    arr.add(new JsonPrimitive(JsonLongHelper.getLowWord(value)));
    arr.add(new JsonPrimitive(JsonLongHelper.getHighWord(value)));
    return arr;
  }
View Full Code Here

  /**
   * Deserializes a two-number array from {@link #toJson(long)} into a long.
   */
  public static long fromJson(JsonElement e) {
    JsonArray arr = e.getAsJsonArray();
    return JsonLongHelper.toLong(arr.get(1).getAsInt(), arr.get(0).getAsInt());
  }
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.