本文整理汇总了Java中com.fasterxml.jackson.core.JsonEncoding类的典型用法代码示例。如果您正苦于以下问题:Java JsonEncoding类的具体用法?Java JsonEncoding怎么用?Java JsonEncoding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonEncoding类属于com.fasterxml.jackson.core包,在下文中一共展示了JsonEncoding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeInternal
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
@Override //Object就是springmvc返回值
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
// 从threadLocal中获取当前的Request对象
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes()).getRequest();
String callbackParam = request.getParameter(callbackName);
if (StringUtils.isEmpty(callbackParam)) {
// 没有找到callback参数,直接返回json数据
super.writeInternal(object, outputMessage);
} else {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
try {
//将对象转换为json串,然后用回调方法包括起来
String result = callbackParam + "(" + super.getObjectMapper().writeValueAsString(object)
+ ");";
IOUtils.write(result, outputMessage.getBody(), encoding.getJavaName());
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
}
开发者ID:jthinking,项目名称:linux-memory-monitor,代码行数:25,代码来源:CallbackMappingJackson2HttpMessageConverter.java
示例2: _testFailOnWritingStringNotFieldName
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例3: _testFailOnWritingStringFromReaderWithTooFewCharacters
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例4: _testFailOnWritingStringFromNullReader
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例5: _testDupFieldNameWrites
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例6: _testFailOnWritingStringNotFieldName
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例7: _testFailOnWritingFieldNameInRoot
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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();
}
示例8: GenericJacksonWayGraphOutputFormat
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
public GenericJacksonWayGraphOutputFormat(ISegmentOutputFormat<T> segmentOutputFormat,
IAdapter<IGraphVersionMetadataDTO, IWayGraphVersionMetadata> adapter,
OutputStream stream,
JsonGenerator generator)
{
this.segmentOutputFormat = segmentOutputFormat;
this.adapter = adapter;
if (generator == null) {
try {
this.generator = new MappingJsonFactory().createGenerator(stream, JsonEncoding.UTF8);
this.generator.useDefaultPrettyPrinter();
} catch (IOException e) {
log.error("error creating jackson json factory", e);
}
} else {
this.generator = generator;
}
}
示例9: GenericJacksonSegmentOutputFormat
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
public GenericJacksonSegmentOutputFormat(ISegmentAdapterRegistry<? extends IBaseSegmentDTO, T> adapterRegistry,
OutputStream stream, JsonGenerator generator, int flushBatchCount)
{
this.adapterRegistry = adapterRegistry;
if (generator == null) {
try {
this.generator = new MappingJsonFactory().createGenerator(new BufferedOutputStream(stream), JsonEncoding.UTF8);
this.generator.useDefaultPrettyPrinter();
} catch (IOException e) {
log.error("error creating jackson json factory", e);
}
}
if (flushBatchCount > 0 ) {
this.flushBatchCount = flushBatchCount;
} else {
log.warn("flushBatchCount ignored, can not be negative or 0");
}
}
示例10: buildJSONFromFields
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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: getJSON
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
public String getJSON() {
JsonFactory jf = new JsonFactory();
// OutputStream os = new ByteArrayOutputStream();
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 new String(hdos.toByteArray());
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} finally {
hdos.close();
}
}
示例12: checkEncoding
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
/**
* Check whether the encoding is valid.
* @param in input of bytes
* @throws IOException this is an IO exception
* @throws UnsupportedException this is an unsupported exception
*/
private static void checkEncoding(ByteBuf in) throws IOException {
int inputStart = 0;
int inputLength = 4;
fliterCharaters(in);
byte[] buff = new byte[4];
in.getBytes(in.readerIndex(), buff);
ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(new IOContext(new BufferRecycler(),
null,
false),
buff, inputStart,
inputLength);
JsonEncoding jsonEncoding = strapper.detectEncoding();
if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
throw new UnsupportedException("Only UTF-8 encoding is supported.");
}
}
示例13: toJsonWriter
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
/**
* Serialises the {@link DecisionTreeRuleSet} into the {@link OutputStream} in Json format.
*
* <p>Any exceptions are wrapped and thrown via the {@link EhSupport} API.
*
* @param out {@link OutputStream} to write into
* @param ruleSet {@link DecisionTreeRuleSet} to serialise
*/
public static void toJsonWriter(final OutputStream out, final DecisionTreeRuleSet ruleSet) {
EhSupport.propagate(() -> {
try (JsonGenerator generator = new JsonFactory().createGenerator(out, JsonEncoding.UTF8)) {
generator.useDefaultPrettyPrinter();
generator.writeStartObject();
generator.writeStringField("name", ruleSet.getName());
writeRuleSetDrivers(generator, ruleSet);
writeRuleSetGroups(generator, ruleSet.getValueGroups());
writeRules(generator, ruleSet);
generator.writeEndObject();
generator.flush();
}
});
}
示例14: buildJSONFromFields
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的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) {
LOG.error("IOException in buildJSONFromFields ", e);
return null;
}
}
示例15: getQueryResult
import com.fasterxml.jackson.core.JsonEncoding; //导入依赖的package包/类
public Response getQueryResult(final Query query) {
StreamingOutput stream = new StreamingOutput() {
@Override
public void write( OutputStream os ) throws IOException, WebApplicationException {
JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8);
jg.setPrettyPrinter(new DefaultPrettyPrinter());
jg.writeStartObject();
if (query != null && query.toCypher().length() > 0) {
writeQueryDetails(jg, query);
System.out.println(query.toCypher());
executeQuery(jg, query);
} else {
jg.writeStringField("error", "No query supplied.");
}
jg.writeEndObject();
jg.flush();
jg.close();
}
};
return Response.ok().entity( stream ).type( MediaType.APPLICATION_JSON ).build();
}