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


Java Json.newDocument方法代码示例

本文整理汇总了Java中org.ojai.json.Json.newDocument方法的典型用法代码示例。如果您正苦于以下问题:Java Json.newDocument方法的具体用法?Java Json.newDocument怎么用?Java Json.newDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.ojai.json.Json的用法示例。


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

示例1: testArrayWithinArray

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testArrayWithinArray() {
  JsonDocumentBuilder w = (JsonDocumentBuilder)Json.newDocumentBuilder();
  w.addNewMap();
  w.putNewArray("array");
  w.add("abcd");
  Document r = Json.newDocument();
  List<Object> l = new ArrayList<Object>();
  l.add(123);
  l.add(456);
  r.set("list", l);
  w.add(r.getValue("list"));
  w.endArray();
  w.endMap();
  r = w.getDocument();
  assertEquals(456, r.getInt("array[1][1]"));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:18,代码来源:TestJsonDocumentBuilder.java

示例2: testAsReaderLeaf

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testAsReaderLeaf() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_BYTE, (byte)127);
  document.set("map.num", 12345);
  Map<String, Object> m = new HashMap<String, Object>();
  m.put("first", "John");
  m.put("last", "Doe");
  m.put("age", (short)45);
  document.set("map.info", m);
  DocumentReader myReader = document.asReader("map.info.age");
  EventType event;
  int numtokens = 0;
  while ((event = myReader.next()) != null) {
    if (event == EventType.SHORT) {
      numtokens++;
      assertEquals((short)45, myReader.getShort());
    }
  }
  assertEquals(1, numtokens);
}
 
开发者ID:ojai,项目名称:ojai,代码行数:22,代码来源:TestJsonDocument.java

示例3: testIsNumeric

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testIsNumeric() throws IOException {
  URL url = Resources.getResource("org/ojai/test/data/test.json");
  String content = Resources.toString(url, Charsets.UTF_8);
  Document document = Json.newDocument(content);

  assertFalse(document.getValue("map.boolean").getType().isNumeric());
  assertFalse(document.getValue("map.string").getType().isNumeric());
  assertTrue(document.getValue("map.byte").getType().isNumeric());
  assertTrue(document.getValue("map.short").getType().isNumeric());
  assertTrue(document.getValue("map.int").getType().isNumeric());
  assertTrue(document.getValue("map.long").getType().isNumeric());
  assertTrue(document.getValue("map.float").getType().isNumeric());
  assertTrue(document.getValue("map.double").getType().isNumeric());
  assertTrue(document.getValue("map.decimal").getType().isNumeric());
  assertFalse(document.getValue("map.date").getType().isNumeric());
  assertFalse(document.getValue("map.time").getType().isNumeric());
  assertFalse(document.getValue("map.timestamp").getType().isNumeric());
  assertFalse(document.getValue("map.interval").getType().isNumeric());
  assertFalse(document.getValue("map.binary").getType().isNumeric());
}
 
开发者ID:ojai,项目名称:ojai,代码行数:22,代码来源:TestValues.java

示例4: testDates

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDates() throws ParseException {
  ODate midnightFeb29th2012 = new ODate(MIDNIGHT_FEB29TH_2012);
  ODate milliSecondBeforeMidnightFeb29th2012 = new ODate(MILLIS_BEFORE_MIDNIGHT_FEB29TH_2012);
  Document doc = Json.newDocument();
  doc.set("midnightFeb29th2012", midnightFeb29th2012);
  doc.set("milliSecondBeforeMidnightFeb29th2012", milliSecondBeforeMidnightFeb29th2012);

  midnightFeb29th2012 = doc.getDate("midnightFeb29th2012");
  milliSecondBeforeMidnightFeb29th2012 = doc.getDate("milliSecondBeforeMidnightFeb29th2012");
  assertEquals("2012-02-29", midnightFeb29th2012.toString());
  assertEquals("2012-02-28", milliSecondBeforeMidnightFeb29th2012.toString());

  assertEquals(doc.getDate("midnightFeb29th2012"), ODate.parse("2012-02-29"));
  assertEquals(doc.getDate("milliSecondBeforeMidnightFeb29th2012"), ODate.parse("2012-02-28"));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:17,代码来源:TestDateTime.java

示例5: testTimes

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testTimes() throws ParseException {
  OTime midnight = new OTime(MIDNIGHT_FEB29TH_2012);
  OTime milliSecondBeforeMidnight = new OTime(MILLIS_BEFORE_MIDNIGHT_FEB29TH_2012);
  Document doc = Json.newDocument();
  doc.set("midnight", midnight);
  doc.set("milliSecondBeforeMidnight", milliSecondBeforeMidnight);

  midnight = doc.getTime("midnight");
  milliSecondBeforeMidnight = doc.getTime("milliSecondBeforeMidnight");
  assertEquals("00:00:00.000", midnight.toString());
  assertEquals("23:59:59.999", milliSecondBeforeMidnight.toString());

  OTime parsedTime = OTime.parse("00:00:00");
  OTime storedTime = doc.getTime("midnight");
  assertEquals(storedTime, parsedTime);

  parsedTime = OTime.parse("23:59:59.999");
  storedTime = doc.getTime("milliSecondBeforeMidnight");
  assertEquals(storedTime, parsedTime);
}
 
开发者ID:ojai,项目名称:ojai,代码行数:22,代码来源:TestDateTime.java

示例6: testDocumentListener

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDocumentListener() throws IOException {
  final Document status = Json.newDocument();

  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
      DocumentStream stream = Json.newDocumentStream(in)) {
    stream.streamTo(new DocumentListener() {
      int documentProcessed = 0;
      @Override
      public boolean documentArrived(Document document) {
        documentProcessed++;
        status.set("documentArrived", true);
        logger.info("Document arrived: %s", document.asJsonString());
        if ("id3".equals(document.getString("business_id"))) {
          return false;
        } else {
          return true;
        }
      }

      @Override
      public void failed(Exception e) {
        status.set("failed", true);
      }

      @Override
      public void eos() {
        status.set("eos", true);
        assertEquals(3, documentProcessed);
      }
    });

    assertEquals(true, status.getBoolean("documentArrived"));
    assertNull(status.getBooleanObj("failed"));
    assertEquals(true, status.getBoolean("eos"));
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:38,代码来源:TestJsonDocumentStream.java

示例7: testDocumentListenerError

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDocumentListenerError() throws IOException {
  final Document status = Json.newDocument();
  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    stream.iterator(); // open an iterator and ignore it

    stream.streamTo(new DocumentListener() {
      @Override
      public boolean documentArrived(Document document) {
        status.set("documentArrived", true);
        return false;
      }

      @Override
      public void failed(Exception e) {
        status.set("failed", true);
      }

      @Override
      public void eos() {
        status.set("eos", true);
      }
    });

    assertNull(status.getBooleanObj("documentArrived"));
    assertEquals(true, status.getBoolean("failed"));
    assertNull(status.getBooleanObj("eos"));
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:32,代码来源:TestJsonDocumentStream.java

示例8: testValuesAsJsonString

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testValuesAsJsonString() {
  Document r = Json.newDocument();
  r.set("a", (long)1234);
  r.set("b", ODate.parse("2011-09-15"));

  assertEquals("{\"$numberLong\":1234}", Values.asJsonString(r.getValue("a")));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:9,代码来源:TestJsonUtil.java

示例9: testDocumentAdd

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDocumentAdd() {
  Map<String, Object> map = new LinkedHashMap<String, Object>();

  map.put("null", null);
  map.put("boolean", true);
  map.put("string", "eureka");
  List<Object> l = new ArrayList<Object>();
  l.add("test1");
  Map<String, Object> m2 = new LinkedHashMap<String, Object>();
  m2.put("int", 32900);
  l.add(m2);
  l.add(Long.valueOf("9223372036854775807").longValue());
  l.add(new BigDecimal(7126353.167263));
  map.put("array", l);

  Document innerDocument = Json.newDocument();
  innerDocument.set("map", map);

  //assert on integer field inside innerDocument
  assertEquals(32900, innerDocument.getInt("map.array[1].int"));

  JsonDocumentBuilder builder = (JsonDocumentBuilder)Json.newDocumentBuilder();
  builder.addNewMap();
  builder.putNewMap("map");
  builder.putNewArray("array");
  builder.add(innerDocument);
  builder.add(true);
  builder.endArray();
  builder.endMap();
  builder.endMap();

  Document rec = builder.getDocument();
  //rerun assert on built document
  assertEquals(32900, rec.getInt("map.array[0].map.array[1].int"));
  logger.info(builder.asUTF8String());
}
 
开发者ID:ojai,项目名称:ojai,代码行数:38,代码来源:TestJsonDocumentBuilder.java

示例10: testArrayAndMapWithinMap

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testArrayAndMapWithinMap() {
  JsonDocumentBuilder w = (JsonDocumentBuilder)Json.newDocumentBuilder();
  w.addNewMap();
  w.putNewArray("array");
  w.add("abcd");
  Document r = Json.newDocument();
  List<Object> l = new ArrayList<Object>();
  l.add(123);
  l.add(456);
  r.set("list", l);
  w.add(r.getValue("list"));
  w.endArray();
  Map<String, Object> m = new HashMap<String, Object>();
  Map<String, Object> m2 = new HashMap<String, Object>();
  m2.put("a1", true);
  m2.put("a2", 11.456);
  List<Object> ll = new ArrayList<Object>();
  ll.add(new BigDecimal(111.11111));
  m2.put("arr2",ll);
  m.put("a3", 5555);
  m.put("map", m2);
  w.put("f", m);
  w.endMap();
  r = w.getDocument();

  assertEquals(5555, r.getInt("f.a3"));
  assertEquals(true, r.getBoolean("f.map.a1"));

}
 
开发者ID:ojai,项目名称:ojai,代码行数:31,代码来源:TestJsonDocumentBuilder.java

示例11: testGetMap

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testGetMap() {
  Document document = Json.newDocument();
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("a", 1);
  map.put("b", "A");
  document.set("map", map);
  assertEquals(map, document.getMap("map"));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:10,代码来源:TestJsonDocument.java

示例12: testDateWithIntegerMaxMin

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDateWithIntegerMaxMin() {
  Document doc = Json.newDocument();
  ODate d1 = new ODate(Integer.MAX_VALUE);
  ODate d2 = new ODate(Integer.MIN_VALUE);
  doc.set("maxdate", d1);
  doc.set("boolean", false);
  doc.set("mindate", d2);

  logger.info("{}", d1);
  logger.info("{}", doc.getDate("maxdate"));

  assertEquals(true, doc.getValue("maxdate").equals(d1));
  assertEquals(true, doc.getValue("mindate").equals(d2));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:16,代码来源:TestJsonDocument.java

示例13: testDate

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testDate() {
  Document doc = Json.newDocument();
  doc.set("d1", ODate.parse("2005-06-22"));
  ODate d = new ODate(new java.util.Date());
  doc.set("d2", d);
  logger.info("{}", doc.getDate("d1"));
  logger.info("{}", doc.getDate("d2"));

  assertEquals(true, doc.getDate("d1").toString().equals("2005-06-22"));
  assertEquals(true, doc.getDate("d2").toString().equals(d.toString()));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:13,代码来源:TestJsonDocument.java

示例14: testMultiLevelJavaBeans

import org.ojai.json.Json; //导入方法依赖的package包/类
@Test
public void testMultiLevelJavaBeans() {
  String jsonString = "{" +
          " \"_id\" : \"1001\"," +
          " \"first_name\" : \"John\"," +
          " \"last_name\" : \"Doe\"," +
          " \"active\" : false," +
          " \"age\" : 35," +
          " \"interests\" : [\"sports\",\"computers\"]," +
          " \"account\" : 1234.4321," +
          " \"address\" : {" +
          "     \"street\" : \"1015 15th av\"," +
          "     \"city\" : \"SFO\"," +
          "     \"zip\" : 94065" +
          "    }" +
          "}";

  Document doc = Json.newDocument(jsonString);
  User user = doc.toJavaBean(User.class);
  Document userDoc = Json.newDocument(user);

  assertNotEquals(doc, userDoc);

  assertEquals(doc.getString("first_name"), userDoc.getString("first_name"));
  assertEquals(user.getFirstName(), userDoc.getString("first_name"));

  assertEquals(doc.getString("last_name"), userDoc.getString("last_name"));
  assertEquals(user.getLastName(), userDoc.getString("last_name"));

  assertEquals(doc.getDouble("age"), userDoc.getInt("age"), 0.0);
  assertEquals(user.getAge(), userDoc.getInt("age"), 0.0);

  assertEquals(doc.getString("address.city"), userDoc.getString("address.city"));
  assertEquals(user.getAddress().getTown(), userDoc.getString("address.city"));

  Document addressDoc = Json.newDocument(user.getAddress());
  assertEquals(addressDoc, userDoc.getMap("address"));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:39,代码来源:TestJavaBeans.java

示例15: newDocument

import org.ojai.json.Json; //导入方法依赖的package包/类
@Override
public Document newDocument(String documentJson) throws DecodingException {
  return Json.newDocument(documentJson);
}
 
开发者ID:ojai,项目名称:ojai,代码行数:5,代码来源:DummyJsonDriver.java


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