下面列出了com.fasterxml.jackson.core.JsonGenerator#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Response getJWTRenewSuccessResponse(String jwt) throws IOException {
try (StringWriter sw = new StringWriter()) {
JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(sw);
jsonGenerator.writeStartObject();
jsonGenerator.writeBooleanField("executed", true);
jsonGenerator.writeStringField("access_token", jwt);
jsonGenerator.writeStringField("token_type", "Bearer");
jsonGenerator.writeEndObject();
jsonGenerator.flush();
jsonGenerator.close();
return Response.status(Response.Status.OK)
.header("Authorization", "Bearer " + jwt)
.entity(sw.toString())
.build();
}
}
/**
* for param serializer2 serialization
*/
private byte[] serializer2(Double[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
int fieldCount=0;
jacksonSerializer.writeStartObject();
if (value!=null) {
int n=value.length;
Double item;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value[i];
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueCharList serialization
*/
public static byte[] serializeValueCharList(LinkedList<Character> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.size();
Character item;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value.get(i);
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueIntegerSet serialization
*/
public static byte[] serializeValueIntegerSet(LinkedHashSet<Integer> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (Integer item: value) {
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueTimeList serialization
*/
protected String serializeValueTimeList(List<Time> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.size();
Time item;
// write wrapper tag
jacksonSerializer.writeFieldName("valueTimeList");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value.get(i);
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeString(SQLTimeUtils.write(item));
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueByteSet serialization
*/
protected String serializeValueByteSet(Set<Byte> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
// write wrapper tag
jacksonSerializer.writeFieldName("valueByteSet");
jacksonSerializer.writeStartArray();
for (Byte item: value) {
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* 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(Message message, JsonGenerator generator) throws IOException {
generator.writeStartObject();
printMessage(message, generator);
generator.writeEndObject();
generator.flush();
}
/**
* for attribute valueLongTypeArray serialization
*/
protected String serializeValueLongTypeArray(long[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.length;
long item;
// write wrapper tag
jacksonSerializer.writeFieldName("valueLongTypeArray");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value[i];
jacksonSerializer.writeNumber(item);
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute value2 serialization
*/
public static byte[] serializeValue2(Character[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.length;
Character item;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value[i];
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueDoubleSet serialization
*/
protected String serializeValueDoubleSet(HashSet<Double> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
// write wrapper tag
jacksonSerializer.writeFieldName("valueDoubleSet");
jacksonSerializer.writeStartArray();
for (Double item: value) {
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueCharList serialization
*/
protected String serializeValueCharList(LinkedList<Character> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.size();
Character item;
// write wrapper tag
jacksonSerializer.writeFieldName("valueCharList");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value.get(i);
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeNumber(item);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute value2 serialization
*/
public static byte[] serializeValue2(BeanInner[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.length;
BeanInner item;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value[i];
if (item==null) {
jacksonSerializer.writeNull();
} else {
beanInnerBindMap.serializeOnJackson(item, jacksonSerializer);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueBigDecimalSet serialization
*/
protected String serializeValueBigDecimalSet(HashSet<BigDecimal> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
// write wrapper tag
jacksonSerializer.writeFieldName("valueBigDecimalSet");
jacksonSerializer.writeStartArray();
for (BigDecimal item: value) {
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeString(BigDecimalUtils.write(item));
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
private void writeExcludedCreative(ExcludedCreative exCreat, JsonGenerator gen)
throws IOException {
gen.writeStartObject();
writeExcludedCreativeFields(exCreat, gen);
gen.writeEndObject();
gen.flush();
}
/**
* Serializes a {@link BidResponse} to JSON, with a provided {@link JsonGenerator}
* which allows several choices of output and encoding.
*/
public final void writeBidResponse(BidResponse resp, JsonGenerator gen) throws IOException {
gen.writeStartObject();
writeBidResponseFields(resp, gen);
writeExtensions(resp, gen);
gen.writeEndObject();
gen.flush();
}
/**
* for attribute valueBeanSet serialization
*/
public static byte[] serializeValueBeanSet(LinkedHashSet<Bean> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (Bean item: value) {
if (item==null) {
jacksonSerializer.writeNull();
} else {
beanBindMap.serializeOnJackson(item, jacksonSerializer);
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueTimeList serialization
*/
public static byte[] serializeValueTimeList(List<Time> value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
int n=value.size();
Time item;
// write wrapper tag
jacksonSerializer.writeFieldName("element");
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=value.get(i);
if (item==null) {
jacksonSerializer.writeNull();
} else {
jacksonSerializer.writeString(SQLTimeUtils.write(item));
}
}
jacksonSerializer.writeEndArray();
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toByteArray();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
private static <T> String headerSafeJson(StoneSerializer<T> serializer, T value) {
StringWriter out = new StringWriter();
try {
JsonGenerator g = JSON.createGenerator(out);
// Escape 0x7F, because it's not allowed in an HTTP header.
// Escape all non-ASCII because the new HTTP spec recommends against non-ASCII in headers.
g.setHighestNonEscapedChar(0x7E);
serializer.serialize(value, g);
g.flush();
} catch (IOException ex) {
throw LangUtil.mkAssert("Impossible", ex);
}
return out.toString();
}
/**
* Writes properties and their attributes (final and resource)
* to the given {@link Writer}.
*
* <li>
* When propertyName is not empty, and the property exists
* in the configuration, the format of the output would be,
* <pre>
* {
* "property": {
* "key" : "key1",
* "value" : "value1",
* "isFinal" : "key1.isFinal",
* "resource" : "key1.resource"
* }
* }
* </pre>
* </li>
*
* <li>
* When propertyName is null or empty, it behaves same as
* {@link #dumpConfiguration(Configuration, Writer)}, the
* output would be,
* <pre>
* { "properties" :
* [ { key : "key1",
* value : "value1",
* isFinal : "key1.isFinal",
* resource : "key1.resource" },
* { key : "key2",
* value : "value2",
* isFinal : "ke2.isFinal",
* resource : "key2.resource" }
* ]
* }
* </pre>
* </li>
*
* <li>
* When propertyName is not empty, and the property is not
* found in the configuration, this method will throw an
* {@link IllegalArgumentException}.
* </li>
* <p>
* @param config the configuration
* @param propertyName property name
* @param out the Writer to write to
* @throws IOException
* @throws IllegalArgumentException when property name is not
* empty and the property is not found in configuration
**/
public static void dumpConfiguration(Configuration config,
String propertyName, Writer out) throws IOException {
if(Strings.isNullOrEmpty(propertyName)) {
dumpConfiguration(config, out);
} else if (Strings.isNullOrEmpty(config.get(propertyName))) {
throw new IllegalArgumentException("Property " +
propertyName + " not found");
} else {
JsonFactory dumpFactory = new JsonFactory();
JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
dumpGenerator.writeStartObject();
dumpGenerator.writeFieldName("property");
appendJSONProperty(dumpGenerator, config, propertyName,
new ConfigRedactor(config));
dumpGenerator.writeEndObject();
dumpGenerator.flush();
}
}
/**
* Writes out all properties and their attributes (final and resource) to
* the given {@link Writer}, the format of the output would be,
*
* <pre>
* { "properties" :
* [ { key : "key1",
* value : "value1",
* isFinal : "key1.isFinal",
* resource : "key1.resource" },
* { key : "key2",
* value : "value2",
* isFinal : "ke2.isFinal",
* resource : "key2.resource" }
* ]
* }
* </pre>
*
* It does not output the properties of the configuration object which
* is loaded from an input stream.
* <p>
*
* @param config the configuration
* @param out the Writer to write to
* @throws IOException
*/
public static void dumpConfiguration(Configuration config,
Writer out) throws IOException {
JsonFactory dumpFactory = new JsonFactory();
JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
dumpGenerator.writeStartObject();
dumpGenerator.writeFieldName("properties");
dumpGenerator.writeStartArray();
dumpGenerator.flush();
ConfigRedactor redactor = new ConfigRedactor(config);
synchronized (config) {
for (Map.Entry<Object,Object> item: config.getProps().entrySet()) {
appendJSONProperty(dumpGenerator, config, item.getKey().toString(),
redactor);
}
}
dumpGenerator.writeEndArray();
dumpGenerator.writeEndObject();
dumpGenerator.flush();
}