本文整理汇总了Java中com.fasterxml.jackson.core.JsonFactory.createGenerator方法的典型用法代码示例。如果您正苦于以下问题:Java JsonFactory.createGenerator方法的具体用法?Java JsonFactory.createGenerator怎么用?Java JsonFactory.createGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.core.JsonFactory
的用法示例。
在下文中一共展示了JsonFactory.createGenerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _testFailOnWritingStringNotFieldName
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testFailOnWritingStringNotFieldName(JsonFactory f, boolean useReader) throws Exception
{
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
gen.writeStartObject();
try {
StringReader reader = new StringReader("a");
gen.writeString(reader, -1);
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let "+gen.getClass().getName()+".writeString() be used in place of 'writeFieldName()': output = "+json);
} catch (JsonProcessingException e) {
verifyException(e, "can not write a String");
}
gen.close();
}
示例2: _testFailOnWritingStringFromReaderWithTooFewCharacters
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testFailOnWritingStringFromReaderWithTooFewCharacters(JsonFactory f, boolean useReader) throws Exception{
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
gen.writeStartObject();
try {
String testStr = "aaaaaaaaa";
StringReader reader = new StringReader(testStr);
gen.writeFieldName("a");
gen.writeString(reader, testStr.length() + 1);
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let "+gen.getClass().getName()+".writeString() ': output = "+json);
} catch (JsonProcessingException e) {
verifyException(e, "Didn't read enough from reader");
}
gen.close();
}
示例3: _testFailOnWritingStringFromNullReader
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testFailOnWritingStringFromNullReader(JsonFactory f, boolean useReader) throws Exception{
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
gen.writeStartObject();
try {
gen.writeFieldName("a");
gen.writeString(null, -1);
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let "+gen.getClass().getName()+".writeString() ': output = "+json);
} catch (JsonProcessingException e) {
verifyException(e, "null reader");
}
gen.close();
}
示例4: _testDupFieldNameWrites
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testDupFieldNameWrites(JsonFactory f, boolean useReader) throws Exception
{
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
gen.writeStartObject();
gen.writeFieldName("a");
try {
gen.writeFieldName("b");
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let two consecutive field name writes succeed: output = "+json);
} catch (JsonProcessingException e) {
verifyException(e, "can not write a field name, expecting a value");
}
gen.close();
}
示例5: _testFailOnWritingFieldNameInRoot
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testFailOnWritingFieldNameInRoot(JsonFactory f, boolean useReader) throws Exception
{
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
try {
gen.writeFieldName("a");
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let "+gen.getClass().getName()+".writeFieldName() be used in root context: output = "+json);
} catch (JsonProcessingException e) {
verifyException(e, "can not write a field name");
}
gen.close();
}
示例6: testGenerationException
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public void testGenerationException() throws Exception
{
JsonFactory jf = new JsonFactory();
JsonGenerator g = jf.createGenerator(ObjectWriteContext.empty(), new ByteArrayOutputStream());
JsonGenerationException exc = null;
g.writeStartObject();
try {
g.writeNumber(4);
fail("Should not get here");
} catch (JsonGenerationException e) {
exc = e;
}
g.close();
byte[] stuff = jdkSerialize(exc);
JsonGenerationException result = jdkDeserialize(stuff);
assertNotNull(result);
}
示例7: toJson
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
/**
* @return A json representation of the topology. Intended for printing only.
*/
public static final String toJson(ITopology topo, boolean pretty) throws IOException {
ObjectMapper mapper = new ObjectMapper();
if (pretty)
mapper.enable(SerializationFeature.INDENT_OUTPUT);
StringWriter sw = new StringWriter();
JsonFactory f = mapper.getFactory();
JsonGenerator g = f.createGenerator(sw);
g.writeStartObject();
// use TreeSet to sort the list
g.writeObjectField("switches", new TreeSet<String>(topo.getSwitches().keySet()));
g.writeObjectField("links", new TreeSet<String>(topo.getLinks().keySet()));
g.writeEndObject();
g.close();
return sw.toString();
}
示例8: OutputStreamDemo
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public static void OutputStreamDemo() throws IOException {
String out = "";
JsonFactory f = new JsonFactory();
FileOutputStream fos = new FileOutputStream(new File("user.out.json"));
JsonGenerator g = f.createGenerator(fos);
g.writeStartObject();
g.writeObjectFieldStart("name");
g.writeStringField("first", "BorLion");
g.writeStringField("last", "Zhu");
g.writeEndObject(); // for field 'name'
g.writeStringField("gender", "MALE");
g.writeBooleanField("verified", true);
g.writeBinaryField("userImage", "Rm9vYmFyIQ==".getBytes());
g.writeEndObject();
g.writeRaw('\n');
g.close();
}
示例9: serialize_GivesEmptyResult_WhenQueryIsEmpty
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void serialize_GivesEmptyResult_WhenQueryIsEmpty() throws IOException, JSONException {
// Arrange
JsonFactory factory = new JsonFactory();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = factory.createGenerator(outputStream);
when(tupleQueryResult.hasNext()).thenReturn(false);
// Act
jsonSerializer.serialize(tupleQueryResult, jsonGenerator, null);
// Assert
jsonGenerator.close();
String result = outputStream.toString();
JSONAssert.assertEquals("[]", result, true);
}
示例10: buildJSONFromFields
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private String buildJSONFromFields(Collection<SearchHitField> values) {
JsonFactory nodeFactory = new JsonFactory();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
generator.writeStartObject();
for (SearchHitField value : values) {
if (value.getValues().size() > 1) {
generator.writeArrayFieldStart(value.getName());
for (Object val : value.getValues()) {
generator.writeObject(val);
}
generator.writeEndArray();
} else {
generator.writeObjectField(value.getName(), value.getValue());
}
}
generator.writeEndObject();
generator.flush();
return new String(stream.toByteArray(), Charset.forName("UTF-8"));
} catch (IOException e) {
return null;
}
}
示例11: toJSONString
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
/**
* Output the minimal schema info as JSON. This is different from the default serialization supported by {@link Field}
* as it has lot more information (such as vector types) which is not needed for understanding the schema.
* @return
* @throws Exception
*/
public String toJSONString() throws Exception {
final JsonFactory factory = new JsonFactory();
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final JsonGenerator jsonGenerator = factory.createGenerator(outputStream);
toJSONString("root", null, getFields(), jsonGenerator);
jsonGenerator.flush();
return outputStream.toString();
}
示例12: writeJson
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public static void writeJson(Writer writer, Consumer<JsonGenerator> consumer) {
Objects.requireNonNull(writer);
Objects.requireNonNull(consumer);
JsonFactory factory = new JsonFactory();
try (JsonGenerator generator = factory.createGenerator(writer)) {
generator.useDefaultPrettyPrinter();
consumer.accept(generator);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例13: sendMessage
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
protected void sendMessage(DataFormat dataFormat, WebSocketSession webSocketSession,
WampMessage msg) throws IOException {
JsonFactory useFactory = this.jsonFactory;
if (dataFormat == DataFormat.MSGPACK) {
useFactory = this.msgpackFactory;
}
else if (dataFormat == DataFormat.CBOR) {
useFactory = this.cborFactory;
}
else if (dataFormat == DataFormat.SMILE) {
useFactory = this.smileFactory;
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
JsonGenerator generator = useFactory.createGenerator(bos)) {
generator.writeStartArray();
msg.serialize(generator);
generator.writeEndArray();
generator.close();
if (dataFormat == DataFormat.MSGPACK || dataFormat == DataFormat.CBOR
|| dataFormat == DataFormat.SMILE) {
webSocketSession.sendMessage(new BinaryMessage(bos.toByteArray()));
}
else {
webSocketSession.sendMessage(new TextMessage(bos.toByteArray()));
}
}
}
示例14: serialize_GivesJsonResult_WhenQueryGivesData
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void serialize_GivesJsonResult_WhenQueryGivesData() throws IOException, JSONException {
// Arrange
JsonFactory factory = new JsonFactory();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = factory.createGenerator(outputStream);
BindingSet bindingSet = new ListBindingSet(
ImmutableList.of("identifier", "name", "yearOfFoundation", "craftMember", "fte"),
DBEERPEDIA.BROUWTOREN, DBEERPEDIA.BROUWTOREN_NAME, DBEERPEDIA.BROUWTOREN_YEAR_OF_FOUNDATION,
DBEERPEDIA.BROUWTOREN_CRAFT_MEMBER, DBEERPEDIA.BROUWTOREN_FTE);
when(tupleQueryResult.hasNext()).thenReturn(true, false);
when(tupleQueryResult.next()).thenReturn(bindingSet);
// Act
jsonSerializer.serialize(tupleQueryResult, jsonGenerator, null);
// Assert
jsonGenerator.close();
String result = outputStream.toString();
JSONArray expected = new JSONArray().put(
new JSONObject().put("identifier", DBEERPEDIA.BROUWTOREN.stringValue()).put("name",
DBEERPEDIA.BROUWTOREN_NAME.stringValue()).put("yearOfFoundation",
DBEERPEDIA.BROUWTOREN_YEAR_OF_FOUNDATION.shortValue()).put("craftMember",
DBEERPEDIA.BROUWTOREN_CRAFT_MEMBER.booleanValue()).put("fte",
DBEERPEDIA.BROUWTOREN_FTE.decimalValue()));
JSONAssert.assertEquals(expected.toString(), result, true);
}
示例15: SdkJsonGenerator
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public SdkJsonGenerator(JsonFactory factory, String contentType) {
try {
/**
* A {@link JsonGenerator} created is by default enabled with
* UTF-8 encoding
*/
this.generator = factory.createGenerator(baos);
this.contentType = contentType;
} catch (IOException e) {
throw new JsonGenerationException(e);
}
}