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


Java Json类代码示例

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


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

示例1: getDocument

import org.ojai.json.Json; //导入依赖的package包/类
@Override
public Document getDocument() {
  Preconditions.checkState(jsonGenerator.isClosed(), "The document has not been built.");

  if (b != null) {
    byte[] barray = b.getByteArray();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(barray);
    DocumentStream documentStream = Json.newDocumentStream(inputStream);
    Iterator<Document> iter = documentStream.iterator();
    if (iter.hasNext()) {
      Document r = iter.next();
      return r;
    }
  }
  return null;
}
 
开发者ID:ojai,项目名称:ojai,代码行数:17,代码来源:JsonDocumentBuilder.java

示例2: testResources

import org.ojai.json.Json; //导入依赖的package包/类
private void testResources(String resource) throws IOException {
  try (InputStream testJson = getJsonStream(resource);
       DocumentStream stream = Json.newDocumentStream(testJson);
       InputStream testJson1 = getJsonStream(resource);
       DocumentStream stream1 = Json.newDocumentStream(testJson1);
       InputStream testJson2 = getJsonStream(resource);
       DocumentStream stream2 = Json.newDocumentStream(testJson2);) {
    for (Document document : stream) {
      assertEquals(document, document); // self comparison
    }
    // same documents extracted from different streams
    Iterator<Document> itr1 = stream1.iterator();
    Iterator<Document> itr2 = stream2.iterator();
    while (itr1.hasNext()) {
      assertEquals(itr1.next(), itr2.next());
    }
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:19,代码来源:TestJsonDocumentEquals.java

示例3: testDocumentIterator

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testDocumentIterator() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
       DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    Iterator<Document> it = stream.iterator();
    Document document;
    while (it.hasNext()) {
      document = it.next();
      testDocumentElements(document);
      documentCount++;
    }
    assertEquals(4, documentCount);
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:17,代码来源:TestJsonDocumentStream.java

示例4: testDocumentIteratorNextMethod

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testDocumentIteratorNextMethod() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
       DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    Iterator<Document> it = stream.iterator();

    Document rec ;
    try {
      while ((rec = it.next()) != null) {
        documentCount++;
        if (documentCount == 1) {
          assertEquals("John", rec.getString("name.first"));
        }
      }
    } catch (Exception e) {
      assertEquals(4, documentCount);
    }
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:22,代码来源:TestJsonDocumentStream.java

示例5: testDocumentPut

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testDocumentPut() {
  Document document = Json.newDocument();
  document.set("recValue1", "string");
  document.set("recValue2", 1);
  Document document2 = Json.newDocument();
  document2.set("val1", true);
  document2.set("val2", 100);
  List<Object> l = new ArrayList<Object>();
  l.add("abcd");
  l.add(false);
  document2.set("list", l);
  document.set("rec", document2);
  DocumentBuilder documentBuilder = Json.newDocumentBuilder();
  documentBuilder.addNewMap();
  documentBuilder.put("document", document);
  documentBuilder.put("rootValue1", 1)
  .put("rootValue2", "2").endMap();
  logger.info(documentBuilder.toString());
}
 
开发者ID:ojai,项目名称:ojai,代码行数:21,代码来源:TestJsonDocumentBuilder.java

示例6: testPutMapUsingBuilder

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testPutMapUsingBuilder() {
  JsonDocumentBuilder w = (JsonDocumentBuilder)Json.newDocumentBuilder();
  w.addNewMap();
  w.putNull("a1");
  Map<String, Object> m = new HashMap<String, Object>();
  m.put("k1", "abcd");
  m.put("k2", Arrays.asList(1, 2, 3));
  Map<String, Object> m2 = new HashMap<String, Object>();
  m2.put("k3", ODate.parse("2005-10-22"));
  m2.put("k4", Arrays.asList(1.111, 2.222, 3.333));
  m.put("k5", m2);
  w.put("map", m);
  w.endMap();

  Document r = w.getDocument();
  logger.info(w.asUTF8String());
  assertEquals(2, r.getInt("map.k2[1]"));
}
 
开发者ID:ojai,项目名称:ojai,代码行数:20,代码来源:TestJsonDocumentBuilder.java

示例7: 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

示例8: testAsReaderPartial

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testAsReaderPartial() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_BYTE, (byte)127);
  document.set("map.num", 12345);
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("first","John");
  map.put("last", "Doe");
  map.put("id", (long)123456789);
  document.set("map.name", map);
  List<Object> mylist = new ArrayList<Object>();
  mylist.add(true);
  mylist.add("string");
  mylist.add(123.456);
  document.set(FIELD_MAP_ARRAY, mylist);
  DocumentReader r = document.asReader("map.name");
  EventType event;
  while ((event = r.next()) != null) {
    if (event == EventType.LONG) {
      assertEquals("id", r.getFieldName());
      assertEquals(123456789, r.getLong());
    }
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:25,代码来源:TestJsonDocument.java

示例9: 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

示例10: testJsonList

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testJsonList() throws Exception {
  Object[] objArr = new Object[11];
  objArr[0] = null;
  objArr[1] = true;
  objArr[2] = (byte) 127;
  objArr[3] = (short) 32767;
  objArr[4] = 2147483647;
  objArr[5] = (long) 123456789;
  objArr[6] = (float) 3.4028235;
  objArr[7] = 1.7976931348623157e308;
  objArr[8] = ODate.parse("2015-12-31");
  objArr[9] = OTime.parse("11:59:59");
  objArr[10] = ByteBuffer.wrap("ola".getBytes());

  List<Object> listOfObjs = Arrays.asList(objArr);
  Document doc = Json.newDocument();
  doc.set("objarray", listOfObjs);
  List<Object> newList = doc.getList("objarray");
  assertEquals(newList, listOfObjs);
  assertEquals(listOfObjs, newList);
}
 
开发者ID:ojai,项目名称:ojai,代码行数:23,代码来源:TestJsonDocument.java

示例11: testAsJsonString

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

  assertEquals("true", document.getValue("boolean").asJsonString());
  assertEquals("\"eureka\"", document.getValue("string").asJsonString());
  assertEquals("{\"$numberLong\":127}", document.getValue("byte").asJsonString());
  assertEquals("{\"$numberLong\":32767}", document.getValue("short").asJsonString());
  assertEquals("{\"$numberLong\":2147483647}", document.getValue("int").asJsonString());
  assertEquals("{\"$numberLong\":9223372036854775807}", document.getValue("long").asJsonString());
  assertEquals("3.4028235", document.getValue("float").asJsonString());
  assertEquals("1.7976931348623157E308", document.getValue("double").asJsonString());
  assertEquals("\"123456789012345678901234567890123456789012345678901.23456789\"", document.getValue("decimal").asJsonString());
  assertEquals("{\"$dateDay\":\"2012-10-20\"}", document.getValue("date").asJsonString());
  assertEquals("{\"$time\":\"07:42:46.123\"}", document.getValue("time").asJsonString());
  assertEquals("{\"$date\":\"2012-10-20T14:42:46.123Z\"}", document.getValue("timestamp").asJsonString());
  assertEquals("172800000", document.getValue("interval").asJsonString());
  assertEquals("{\"$binary\":\"YWJjZA==\"}", document.getValue("binary").asJsonString());
}
 
开发者ID:ojai,项目名称:ojai,代码行数:22,代码来源:TestValues.java

示例12: 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

示例13: 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

示例14: 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

示例15: testFetchAndParseJsonDocumentStream

import org.ojai.json.Json; //导入依赖的package包/类
@Test
public void testFetchAndParseJsonDocumentStream() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/manydocs.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      if (documentCount == 1) {
        validateDocumentReaderOne(reader);
      } else if (documentCount == 2) {
        validateDocumentReaderTwo(reader);
      } else {
        validateDocumentReaderThree(reader);
      }
    }
    assertEquals(3, documentCount);
  }
}
 
开发者ID:ojai,项目名称:ojai,代码行数:20,代码来源:TestJsonDocumentStreamFormat.java


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