本文整理汇总了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]"));
}
示例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);
}
示例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());
}
示例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"));
}
示例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);
}
示例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"));
}
}
示例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"));
}
}
示例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")));
}
示例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());
}
示例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"));
}
示例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"));
}
示例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));
}
示例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()));
}
示例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"));
}
示例15: newDocument
import org.ojai.json.Json; //导入方法依赖的package包/类
@Override
public Document newDocument(String documentJson) throws DecodingException {
return Json.newDocument(documentJson);
}