当前位置: 首页>>代码示例>>Java>>正文


Java JsonObject类代码示例

本文整理汇总了Java中io.apptik.json.JsonObject的典型用法代码示例。如果您正苦于以下问题:Java JsonObject类的具体用法?Java JsonObject怎么用?Java JsonObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JsonObject类属于io.apptik.json包,在下文中一共展示了JsonObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testKeys

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testKeys() throws JsonException {
    JsonObject object = new JsonObject();
    object.put("foo", 5);
    object.put("bar", 6);
    object.put("foo", 7);

    @SuppressWarnings("unchecked")
    Iterator<String> keys = object.keys();
    Set<String> result = new HashSet<String>();
    assertTrue(keys.hasNext());
    result.add(keys.next());
    assertTrue(keys.hasNext());
    result.add(keys.next());
    assertFalse(keys.hasNext());
    assertEquals(new HashSet<String>(Arrays.asList("foo", "bar")), result);

    try {
        keys.next();
        fail();
    } catch (NoSuchElementException e) {
    }
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:24,代码来源:JsonObjectTest.java

示例2: getMockJsonArray

import io.apptik.json.JsonObject; //导入依赖的package包/类
public static JsonArray getMockJsonArray(int noElements, int picSize) {
    JsonArray res = new JsonArray();
    Random rand = new Random();

    for(int i=0;i<noElements;i++) {
        int cc = (int)(Math.random() * 0x1000000);
        int cc2 = 0xFFFFFF00 ^ cc;
                String color = Integer.toHexString(cc);
        String color2 = Integer.toHexString((0xFFFFFF - cc) );
       // String color2 = Integer.toHexString(cc2);
        res.add(new JsonObject().put("pic", "http://dummyimage.com/" +picSize+ "/"+ color
                +"/" + color2 + "&text=" + i).put("txt1", ""+i));
    }

    return res;
}
 
开发者ID:apptik,项目名称:jus,代码行数:17,代码来源:MockData.java

示例3: getMockJsonArray

import io.apptik.json.JsonObject; //导入依赖的package包/类
public static JsonArray getMockJsonArray(int noElements, int picSize) {
    JsonArray res = new JsonArray();
    Random rand = new Random();

    for(int i=0;i<noElements;i++) {
        int cc = (int)(Math.random() * 0x1000000);
        int cc2 = 0xFFFFFF00 ^ cc;
                String color = Integer.toHexString(cc);
        String color2 = Integer.toHexString((0xFFFFFF - cc) );
       // String color2 = Integer.toHexString(cc2);
        res.add(new JsonObject().put("imageUrl", "http://dummyimage.com/" +picSize+ "/"+ color
                +"/" + color2 + "&text=" + i).put("title", "title").put("author", "author")
                .put("views", i).put("favorites", i));
    }

    return res;
}
 
开发者ID:apptik,项目名称:jus,代码行数:18,代码来源:MockData.java

示例4: testContains

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testContains() throws Exception {
    arrayWrapper.wrap(jar, TestWrapper.class);
    Map<String, String> testMap = new HashMap<String, String>();
    testMap.put("theName", "value2");
    Map<String, String> testMap2 = new HashMap<String, String>();
    testMap2.put("theName", "valueNA");
    assertThat(arrayWrapper.contains(testMap)).isTrue();
    assertThat(arrayWrapper.contains(new JsonObject(testMap))).isTrue();
    assertThat(arrayWrapper.contains(
            new JsonObjectWrapper().wrap(new JsonObject(testMap)))).isTrue();

    assertThat(arrayWrapper.contains(testMap2)).isFalse();
    assertThat(arrayWrapper.contains(new JsonObject(testMap2))).isFalse();
    assertThat(arrayWrapper.contains(
            new JsonObjectWrapper().wrap(new JsonObject(testMap2)))).isFalse();

}
 
开发者ID:apptik,项目名称:JustJson,代码行数:19,代码来源:JsonObjectArrayWrapperTest.java

示例5: testToJsonObject

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testToJsonObject() throws JsonException {
    JsonArray keys = new JsonArray();
    keys.put("a");
    keys.put("b");

    JsonArray values = new JsonArray();
    values.put(5.5d);
    values.put(Boolean.FALSE);

    JsonObject object = values.toJsonObject(keys);
    assertEquals(object.get("a"), 5.5d);
    assertEquals(object.get("b"), Boolean.FALSE);

    keys.put(0, "a");
    values.put(0, 11.0d);
    assertEquals(object.get("a"), 5.5d);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:19,代码来源:JsonArrayTest.java

示例6: testToJsonObjectWithNulls

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testToJsonObjectWithNulls() throws JsonException {
    JsonArray keys = new JsonArray();
    keys.put("a");
    keys.put("b");

    JsonArray values = new JsonArray();
    values.put(5.5d);
    values.put(null);

    // null values are stripped!
    JsonObject object = values.toJsonObject(keys);
    assertEquals(1, object.length());
    assertFalse(object.has("b"));
    assertEquals("{\"a\":5.5}", object.toString());
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:17,代码来源:JsonArrayTest.java

示例7: testPut

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testPut() throws JsonException {
    JsonObject object = new JsonObject();
    assertSame(object, object.put("foo", Boolean.TRUE));
    object.put("foo", Boolean.FALSE);
    assertEquals(Boolean.FALSE, object.getBoolean("foo"));

    object.put("foo", 5.0d);
    assertEquals(5.0d, object.getDouble("foo"));
    object.put("foo", 0);
    assertEquals((Integer)0, object.getInt("foo"));
    object.put("bar", Long.MAX_VALUE - 1);
    assertEquals((Long)(Long.MAX_VALUE - 1), object.getLong("bar"));
    object.put("baz", "x");
    assertEquals("x", object.get("baz").toString());
    object.put("bar", null);
    assertEquals(object.get("bar"), null);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:19,代码来源:JsonObjectTest.java

示例8: testToJsonArray

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testToJsonArray() throws JsonException {
    JsonObject object = new JsonObject();
    Object value = new Object();
    object.put("foo", Boolean.TRUE);
    object.put("bar", 5.0d);
    object.put("baz", -0.0d);
    object.put("quux", value);

    JsonArray names = new JsonArray();
    names.put("baz");
    names.put("quux");
    names.put("foo");

    JsonArray array = object.toJsonArray(names);
    assertEquals(array.get(0), -0.0d);
    assertEquals(array.get(1), value.toString());
    assertEquals(array.get(2), Boolean.TRUE);

    object.put("foo", Boolean.FALSE);
    assertEquals(array.get(2), Boolean.TRUE);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:23,代码来源:JsonObjectTest.java

示例9: testToJsonArrayMissingNames

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testToJsonArrayMissingNames() throws JsonException {
    JsonObject object = new JsonObject();
    object.put("foo", Boolean.TRUE);
    object.put("bar", 5.0d);
    object.put("baz", JSON_NULL);

    JsonArray names = new JsonArray();
    names.put("bar");
    names.put("foo");
    names.put("quux");
    names.put("baz");

    JsonArray array = object.toJsonArray(names);
    assertEquals(3, array.length());

    assertEquals(array.get(0), 5.0d);
    assertEquals(array.get(1), Boolean.TRUE);
    assertEquals(array.get(2), null);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:21,代码来源:JsonObjectTest.java

示例10: testToJsonArrayNonString

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testToJsonArrayNonString() throws JsonException {
    JsonObject object = new JsonObject();
    object.put("foo", 5);
    object.put("null", 10);
    object.put("false", 15);

    JsonArray names = new JsonArray();
    names.put(JSON_NULL);
    names.put(Boolean.FALSE);
    names.put("foo");

    // array elements are converted to strings to do name lookups on the map!
    JsonArray array = object.toJsonArray(names);
    assertEquals(3, array.length());
    assertEquals(array.get(0), 10);
    assertEquals(array.get(1), 15);
    assertEquals(array.get(2), 5);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:20,代码来源:JsonObjectTest.java

示例11: testOptNull

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testOptNull() throws JsonException {
    JsonObject object = new JsonObject();
    object.put("foo", "bar");
    assertEquals(null, object.opt(null));
    assertNull(object.optBoolean(null));
    assertNull(object.optDouble(null));
    assertEquals(null, object.optInt(null));
    assertEquals(null, object.optLong(null));
    assertEquals(null, object.optJsonArray(null));
    assertEquals(null, object.optJsonObject(null));
    assertEquals(null, object.optString(null));
    assertEquals(Boolean.TRUE, object.optBoolean(null, Boolean.TRUE));
    assertEquals(0.0d, object.optDouble(null, 0.0d));
    assertEquals((Integer)1, object.optInt(null, 1));
    assertEquals((Long)1L, object.optLong(null, 1L));
    assertEquals("baz", object.optString(null, "baz"));
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:19,代码来源:JsonObjectTest.java

示例12: test_wrap

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void test_wrap() throws Exception {
    assertEquals(JSON_NULL, JsonObject.wrap(null));

    JsonArray a = new JsonArray();
    assertEquals(a, JsonObject.wrap(a));

    JsonObject o = new JsonObject();
    assertEquals(o, JsonObject.wrap(o));

    assertEquals(JSON_NULL, JsonObject.wrap(JSON_NULL));

    assertTrue(JsonObject.wrap(new byte[0]) instanceof JsonArray);
    assertTrue(JsonObject.wrap(new ArrayList<String>()) instanceof JsonArray);
    assertTrue(JsonObject.wrap(new HashMap<String, String>()) instanceof JsonObject);
    assertTrue(JsonObject.wrap(Double.valueOf(0)) instanceof JsonNumber);
    assertTrue(JsonObject.wrap("hello") instanceof JsonString);
    assertTrue(JsonObject.wrap(new ConnectException()) instanceof JsonString);
}
 
开发者ID:apptik,项目名称:JustJson,代码行数:20,代码来源:JsonObjectTest.java

示例13: testGenerate

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void testGenerate() throws Exception {
    JsonGeneratorConfig gConf = new JsonGeneratorConfig();
    ArrayList<String> images = new ArrayList<String>();
    images.add("/photos/image.jpg");
    images.add("/photos/image.jpg");

    gConf.uriPaths.put("seven", images);
    // gConf.globalUriPaths = images;
    JsonObject job = new JsonGenerator(schema, gConf).generate().asJsonObject();

    System.out.println(job.toString());

    assertEquals(8, job.length());

}
 
开发者ID:apptik,项目名称:JustJson,代码行数:17,代码来源:JsonGenerationTest.java

示例14: aJsonObject

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Test
public void aJsonObject() throws IOException, InterruptedException, ExecutionException {
    server.enqueue(new MockResponse().setBody("{\"theName\":\"value\"}"));

    JsonObjectWrapper body = service.aJsonObjectWrapper(
            new JsonObjectWrapper(new JsonObject().put("name","value")))
            .getFuture().get();
    assertThat(body.getJson().get("theName")).isEqualTo("value");

    RecordedRequest request = server.takeRequest();
    assertThat(request.getBody().readUtf8()).isEqualTo("{\"name\":\"value\"}");
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8");
}
 
开发者ID:apptik,项目名称:jus,代码行数:14,代码来源:JsonWrapperConverterFactoryTest.java

示例15: convert

import io.apptik.json.JsonObject; //导入依赖的package包/类
@Override
public JsonObject convert(NetworkResponse value) throws IOException {
    if (value.statusCode == 204 || value.statusCode == 205) {
        return null;
    } else {
        Reader reader = value.getCharStream();
        try {
            return JsonElement.readFrom(reader).asJsonObject();
        } finally {
            Utils.closeQuietly(reader);
        }
    }
}
 
开发者ID:apptik,项目名称:jus,代码行数:14,代码来源:JJsonObjectResponseConverter.java


注:本文中的io.apptik.json.JsonObject类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。