下面列出了怎么用com.fasterxml.jackson.core.json.UTF8JsonGenerator的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String,Object> httpHeaders,
OutputStream out) throws IOException, WebApplicationException {
// TODO: Figure out a method to add the proto file location in the response headers.
// This map must be mofified before any data is written to out,
// since at that time the response headers will be flushed.
Schema<Object> schema = null;
if (message instanceof Message) {
Message<Object> msg = (Message<Object>) message;
schema = msg.cachedSchema();
} else {
schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
}
try {
if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
XmlIOUtil.writeTo(out, message, schema);
} else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
YamlIOUtil.writeTo(out, message, schema, buffer);
} else if ("application/x-protobuf".equals(media.toString())) {
ProtobufIOUtil.writeTo(out, message, schema, buffer);
} else if ("application/x-protostuff".equals(media.toString())) {
ProtostuffIOUtil.writeTo(out, message, schema, buffer);
} else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
try {
JsonIOUtil.writeTo(generator, message, schema, false);
} finally {
generator.close();
}
}
} finally {
buffer.clear();
}
}
/**
* Creates a {@link UTF8JsonGenerator} for the outputstream with the supplied buf {@code outBuffer} to use.
*/
static UTF8JsonGenerator newJsonGenerator(OutputStream out, byte[] buf, int offset,
boolean bufferRecyclable, IOContext context) {
context.setEncoding(JsonEncoding.UTF8);
return new UTF8JsonGenerator(context,
DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
DEFAULT_JSON_FACTORY.getCodec(),
out,
buf,
offset,
bufferRecyclable);
}
/**
* Creates a {@link UTF8JsonGenerator} for the outputstream with the supplied buf {@code outBuffer} to use.
*/
static UTF8JsonGenerator newJsonGenerator(OutputStream out, byte[] buf, int offset,
boolean bufferRecyclable, IOContext context)
{
context.setEncoding(JsonEncoding.UTF8);
return new UTF8JsonGenerator(context,
DEFAULT_JSON_FACTORY.getGeneratorFeaturesImpl(),
DEFAULT_JSON_FACTORY.getCodec(),
out,
buf,
offset,
bufferRecyclable);
}
/**
* Creates a {@link UTF8JsonGenerator} for the outputstream with the supplied buf {@code outBuffer} to use.
*/
public static UTF8JsonGenerator newJsonGenerator(OutputStream out, byte[] buf) {
return newJsonGenerator(out, buf, 0, false, new IOContext(
DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false));
}
/**
* Creates a {@link UTF8JsonGenerator} for the outputstream with the supplied buf {@code outBuffer} to use.
*/
public static UTF8JsonGenerator newJsonGenerator(OutputStream out, byte[] buf)
{
return newJsonGenerator(out, buf, 0, false, new IOContext(
DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false));
}