本文整理匯總了Java中com.fasterxml.jackson.core.JsonGenerator.flush方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonGenerator.flush方法的具體用法?Java JsonGenerator.flush怎麽用?Java JsonGenerator.flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.fasterxml.jackson.core.JsonGenerator
的用法示例。
在下文中一共展示了JsonGenerator.flush方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: _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 {
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: _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();
}
示例3: _testDupFieldNameWrites
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的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();
}
示例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: writeTo
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
@Override
public void writeTo(
Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream os) throws IOException {
String fields = uriInfo.getQueryParameters() == null ? null
: uriInfo.getQueryParameters().getFirst("fields");
FieldFilter fieldFilter = FieldFilter.create(fields);
if (!fieldFilter.hasFilters()) {
super.writeTo(o, type, genericType, annotations, mediaType, httpHeaders, os);
return;
}
JsonGenerator jgen = objectMapper.getFactory().createGenerator(os);
TokenBuffer tokenBuffer = new TokenBuffer(objectMapper, false);
objectMapper.writeValue(tokenBuffer, o);
JsonParser jsonParser = tokenBuffer.asParser();
fieldFilter.writeJson(jsonParser, jgen);
jgen.flush();
}
示例6: doTestBigInteger
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
protected void doTestBigInteger(JsonGenerator generator, ByteArrayOutputStream os) throws Exception {
// Big integers cannot be handled explicitly, but if some values happen to be big ints,
// we can still call parser.map() and get the bigint value so that eg. source filtering
// keeps working
BigInteger bigInteger = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);
generator.writeStartObject();
generator.writeFieldName("foo");
generator.writeString("bar");
generator.writeFieldName("bigint");
generator.writeNumber(bigInteger);
generator.writeEndObject();
generator.flush();
byte[] serialized = os.toByteArray();
XContentParser parser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, serialized);
Map<String, Object> map = parser.map();
assertEquals("bar", map.get("foo"));
assertEquals(bigInteger, map.get("bigint"));
}
示例7: getBeakerCodeCells
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
private List<BeakerCodeCell> getBeakerCodeCells(Object value) {
List<BeakerCodeCell> beakerCodeCellList = null;
StringWriter sw = new StringWriter();
JsonGenerator jgen = null;
try {
jgen = objectMapper.getFactory().createGenerator(sw);
objectSerializer.writeObject(value, jgen, true);
jgen.flush();
sw.flush();
beakerCodeCellList = Arrays.asList(objectMapper.readValue(sw.toString(), BeakerCodeCell[].class));
} catch (IOException e) {
e.printStackTrace();
}
return beakerCodeCellList;
}
示例8: toString
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
public String toString(boolean pretty)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator json = new JsonFactory().createGenerator(baos);
if (pretty)
json.useDefaultPrettyPrinter();
write(json, new HashSet());
json.flush();
baos.flush();
return new String(baos.toByteArray());
}
catch (Exception ex)
{
throw new RuntimeException(ex);
}
}
示例9: buildJSONFromFields
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的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;
}
}
示例10: encodeFrame
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
TextWebSocketFrame encodeFrame() throws IOException {
StringWriter w = new StringWriter();
JsonGenerator generator = factory.createGenerator(w);
generator.writeStartObject();
generator.writeFieldName("type");
generator.writeString("subscribe");
generator.writeFieldName("product_ids");
generator.writeStartArray();
products.forEach(product -> {
try {
generator.writeString(product.getCode());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
generator.writeEndArray();
generator.writeFieldName("channels");
generator.writeStartArray();
generator.writeString("full");
generator.writeString("heartbeat");
generator.writeEndArray();
generator.writeEndObject();
generator.flush();
String txt = w.toString();
return new TextWebSocketFrame(true, 0, txt);
}
示例11: encodeFrame
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
String encodeFrame() throws IOException {
StringWriter w = new StringWriter();
JsonGenerator generator = factory.createGenerator(w);
generator.writeStartObject();
generator.writeFieldName("client_oid");
generator.writeString(getClientOrderId().getId());
generator.writeFieldName("size");
generator.writeString(getQuantity().getValue().toString());
if (price != null && stopPrice == null) {
generator.writeStringField("type", "limit");
generator.writeFieldName("price");
generator.writeString(getPrice().getValue().toString());
generator.writeBooleanField("post_only", true);
} else if (stopPrice != null) {
generator.writeStringField("type", "stop");
generator.writeFieldName("price");
generator.writeString(getStopPrice().getValue().toString());
}
generator.writeFieldName("side");
generator.writeString(getSide().toString().toLowerCase());
generator.writeFieldName("product_id");
generator.writeString(getProduct().getCode());
generator.writeFieldName("time_in_force");
generator.writeString(getTimeInForceCode(getTimeInForce()));
if (getTimeInForce() == TimeInForce.DAY) {
generator.writeFieldName("cancel_after");
generator.writeString("day");
}
generator.writeEndObject();
generator.flush();
return w.toString();
}
示例12: testUtf8Issue462
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
public void testUtf8Issue462() throws Exception
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
IOContext ioc = new IOContext(new BufferRecycler(), bytes, true);
JsonGenerator gen = new UTF8JsonGenerator(ObjectWriteContext.empty(), ioc, 0, bytes,
JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR, null, null);
String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04";
int length = 4000 - 38;
for (int i = 1; i <= length; ++i) {
gen.writeNumber(1);
}
gen.writeString(str);
gen.flush();
gen.close();
// Also verify it's parsable?
JsonParser p = JSON_F.createParser(ObjectReadContext.empty(), bytes.toByteArray());
for (int i = 1; i <= length; ++i) {
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getIntValue());
}
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(str, p.getText());
assertNull(p.nextToken());
p.close();
}
示例13: toJSONString
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的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();
}
示例14: writeInternal
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
@Override
@SuppressWarnings("deprecation")
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
try {
JavaType javaType = null;
if (jackson26Available && type != null && object != null && TypeUtils.isAssignable(type, object.getClass())) {
javaType = getJavaType(type, null);
}
ObjectWriter objectWriter;
objectWriter = this.objectMapper.writer();
if (javaType != null && javaType.isContainerType()) {
objectWriter = objectWriter.withType(javaType);
}
objectWriter.writeValue(generator, object);
generator.flush();
}
catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
}
}
開發者ID:JetBrains,項目名稱:teamcity-hashicorp-vault-plugin,代碼行數:28,代碼來源:AbstractJackson2HttpMessageConverter.java
示例15: getJson
import com.fasterxml.jackson.core.JsonGenerator; //導入方法依賴的package包/類
protected String getJson(Object value) throws IOException{
StringWriter sw = new StringWriter();
JsonGenerator jgen = objectMapper.getFactory().createGenerator(sw);
objectSerializer.writeObject(value, jgen, true);
jgen.flush();
sw.flush();
return sw.toString();
}