本文整理汇总了Java中com.fasterxml.jackson.core.JsonGenerator.close方法的典型用法代码示例。如果您正苦于以下问题:Java JsonGenerator.close方法的具体用法?Java JsonGenerator.close怎么用?Java JsonGenerator.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.core.JsonGenerator
的用法示例。
在下文中一共展示了JsonGenerator.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: OutputStreamDemo
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的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();
}
示例2: toJsonString
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
static String toJsonString(String summary, String detail, Markup markup, Revision nextRevision) {
try {
StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator = Jackson.createPrettyGenerator(stringWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(FIELD_NAME_SUMMARY, summary);
jsonGenerator.writeStringField(FIELD_NAME_DETAIL, detail);
jsonGenerator.writeStringField(FIELD_NAME_MARKUP, markup.nameLowercased());
jsonGenerator.writeStringField(FIELD_NAME_REVISION, nextRevision.text());
jsonGenerator.writeEndObject();
jsonGenerator.close();
return stringWriter.toString();
} catch (IOException e) {
throw new StorageException("failed to generate a JSON string", e);
}
}
示例3: _testFailOnWritingStringFromNullReader
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的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: _testFailOnWritingStringNotFieldName
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的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 {
gen.writeString("a");
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();
}
示例5: testSurrogatesWithRaw
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
public void testSurrogatesWithRaw() throws Exception
{
final String VALUE = quote("\ud83d\ude0c");
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
g.writeStartArray();
g.writeRaw(VALUE);
g.writeEndArray();
g.close();
final byte[] JSON = out.toByteArray();
JsonParser jp = JSON_F.createParser(ObjectReadContext.empty(), JSON);
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.VALUE_STRING, jp.nextToken());
String str = jp.getText();
assertEquals(2, str.length());
assertEquals((char) 0xD83D, str.charAt(0));
assertEquals((char) 0xDE0C, str.charAt(1));
assertToken(JsonToken.END_ARRAY, jp.nextToken());
jp.close();
}
示例6: testGenerationException
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的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: write
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
@Override
public void write(TupleQueryResult tupleQueryResult, OutputStream outputStream)
throws IOException {
JsonGenerator jsonGenerator = createFactory().createGenerator(outputStream);
createSerializer().serialize(tupleQueryResult, jsonGenerator, null);
jsonGenerator.close();
}
开发者ID:dotwebstack,项目名称:dotwebstack-framework,代码行数:10,代码来源:AbstractJsonGeneratorTupleEntityWriter.java
示例8: _testRawWithSurrogatesString
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
private void _testRawWithSurrogatesString(boolean useCharArray) throws Exception
{
// boundaries are not exact, may vary, so use this:
final int OFFSET = 3;
final int COUNT = 100;
for (int i = OFFSET; i < COUNT; ++i) {
StringBuilder sb = new StringBuilder(1000);
for (int j = 0; j < i; ++j) {
sb.append(' ');
}
sb.append(SURROGATES_307);
final String text = sb.toString();
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
JsonGenerator g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
if (useCharArray) {
char[] ch = text.toCharArray();
g.writeRawValue(ch, OFFSET, ch.length - OFFSET);
} else {
g.writeRawValue(text, OFFSET, text.length() - OFFSET);
}
g.close();
byte[] b = out.toByteArray();
assertNotNull(b);
}
}
示例9: serialize_GivesJsonResult_WhenQueryGivesData
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的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);
}
示例10: _copyJson
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
protected void _copyJson(JsonFactory f, String json, JsonGenerator g) throws IOException
{
JsonParser p = f.createParser(ObjectReadContext.empty(), json);
while (p.nextToken() != null) {
g.copyCurrentEvent(p);
}
p.close();
g.close();
}
示例11: serialize
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
/**
* Serialize an object to a JSON String.
*
* @param object The object to serialize.
*/
public String serialize(T object) throws IOException {
StringWriter sw = new StringWriter();
JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(sw);
serialize(object, jsonGenerator, true);
jsonGenerator.close();
return sw.toString();
}
示例12: serialize
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
private byte[] serialize(final List<TypedIncomingData> data) throws Exception {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final JsonGenerator json = JSON.getFactory().createGenerator(output);
json.writeStartArray();
for (final TypedIncomingData d : data) {
json.writeObject(d);
}
json.writeEndArray();
json.close();
return output.toByteArray();
}
示例13: getJSONByteArray
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
public byte[] getJSONByteArray() {
JsonFactory jf = new JsonFactory();
HeapDataOutputStream hdos = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
try {
JsonGenerator jg = jf.createJsonGenerator(hdos, JsonEncoding.UTF8);
enableDisableJSONGeneratorFeature(jg);
getJSONString(jg, m_pdxInstance);
jg.close();
return hdos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} finally {
hdos.close();
}
}
示例14: testLocationExtraction
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
public void testLocationExtraction() throws IOException {
// {
// "index" : "test",
// "source" : {
// value : "something"
// }
// }
BytesStreamOutput os = new BytesStreamOutput();
JsonGenerator gen = new JsonFactory().createGenerator(os);
gen.writeStartObject();
gen.writeStringField("index", "test");
gen.writeFieldName("source");
gen.writeStartObject();
gen.writeStringField("value", "something");
gen.writeEndObject();
gen.writeEndObject();
gen.close();
JsonParser parser = new JsonFactory().createParser(os.bytes().streamInput());
assertThat(parser.nextToken(), equalTo(JsonToken.START_OBJECT));
assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "index"
assertThat(parser.nextToken(), equalTo(JsonToken.VALUE_STRING));
assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "source"
// JsonLocation location1 = parser.getCurrentLocation();
// parser.skipChildren();
// JsonLocation location2 = parser.getCurrentLocation();
//
// byte[] sourceData = new byte[(int) (location2.getByteOffset() - location1.getByteOffset())];
// System.arraycopy(data, (int) location1.getByteOffset(), sourceData, 0, sourceData.length);
//
// JsonParser sourceParser = new JsonFactory().createJsonParser(new FastByteArrayInputStream(sourceData));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.START_OBJECT));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "value"
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.VALUE_STRING));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.END_OBJECT));
}
示例15: print
import com.fasterxml.jackson.core.JsonGenerator; //导入方法依赖的package包/类
/**
* Outputs a Smile representation of the Protocol Message supplied into the parameter output.
* (This representation is the new version of the classic "ProtocolPrinter" output from the
* original Protocol Buffer system)
*/
public void print(final Message message, OutputStream output, Charset cs) throws IOException {
JsonGenerator generator = createGenerator(output);
print(message, generator);
generator.close();
}