本文整理汇总了Java中com.fasterxml.jackson.core.JsonFactory类的典型用法代码示例。如果您正苦于以下问题:Java JsonFactory类的具体用法?Java JsonFactory怎么用?Java JsonFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonFactory类属于com.fasterxml.jackson.core包,在下文中一共展示了JsonFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initialize
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
private StructObjectInspector initialize(final ObjectInspector jsonArg, final ObjectInspector jqArg, final List<ObjectInspector> nameAndTypeArgs) throws UDFArgumentException {
this.in = Arguments.asString(jsonArg, "JSON");
try {
this.jq = JsonQuery.compile(Arguments.asConstantNonNullString(jqArg, "JQ"));
} catch (final JsonQueryException e) {
throw new UDFArgumentException("JQ is invalid: " + e.getMessage());
}
this.marshaller = ResultObjectMarshallers.create(Arguments.asConstantNonNullStrings(nameAndTypeArgs, "TYPE or NAME:TYPE"));
this.scope = new Scope();
this.mapper = new ObjectMapper(new JsonFactory().enable(Feature.ALLOW_UNQUOTED_CONTROL_CHARS));
return marshaller.objectInspector();
}
示例2: _testTokenAccess
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception
{
final String DOC = "[ ]";
JsonParser jp = useStream ?
jf.createParser(ObjectReadContext.empty(), new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(ObjectReadContext.empty(), DOC);
assertNull(jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
assertNull(jp.getEmbeddedObject());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.START_ARRAY, jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
// Also: no codec defined by default
try {
jp.readValueAsTree();
fail("Should get exception without codec");
} catch (UnsupportedOperationException e) {
verifyException(e, "Operation not supported");
}
jp.close();
}
示例3: parse
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
/**
* @param content
* @throws JsonParseException
* @throws IOException
*/
public PageInstance parse(String content) throws JsonParseException, IOException {
JsonFactory f = new JsonFactory();
JsonParser parser = f.createJsonParser(content);
JsonToken current = parser.nextToken(); // move to the start of the
// object
String instanceId = getNextTextValue("sid", parser); // get instanceId
String pageId = getNextTextValue("cid", parser); // get pageId
current = parser.nextToken(); // move to field: segments
assertExpectedFiled(parser.getCurrentName(), "segments", parser.getCurrentLocation());
PageInstance pageInstance = new PageInstance();
pageInstance.setCid(pageId);
pageInstance.setSid(Long.valueOf(instanceId));
pageInstance.setSegments(parseSegments(parser));
return pageInstance;
// 构建组件树,用于递归渲染
// pageInstance.buildComponentTree();
}
示例4: CharsToNameCanonicalizer
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
/**
* Internal constructor used when creating child instances.
*/
private CharsToNameCanonicalizer(CharsToNameCanonicalizer parent, int flags, int seed,
TableInfo parentState)
{
_parent = parent;
_seed = seed;
_tableInfo = null; // not used by child tables
_flags = flags;
_canonicalize = JsonFactory.Feature.CANONICALIZE_FIELD_NAMES.enabledIn(flags);
// Then copy shared state
_symbols = parentState.symbols;
_buckets = parentState.buckets;
_size = parentState.size;
_longestCollisionList = parentState.longestCollisionList;
// Hard-coded fill factor, 75%
int arrayLen = (_symbols.length);
_sizeThreshold = _thresholdSize(arrayLen);
_indexMask = (arrayLen - 1);
// Need to make copies of arrays, if/when adding new entries
_hashShared = true;
}
示例5: _testFailOnWritingStringFromReaderWithTooFewCharacters
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的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();
}
示例6: _testFailOnWritingStringFromNullReader
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的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();
}
示例7: _testDupFieldNameWrites
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的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();
}
示例8: _testFailOnWritingFieldNameInRoot
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的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();
}
示例9: write
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
try (
Writer writer = new BufferedWriter(new OutputStreamWriter(output));
JsonGenerator jsonGenerator = new JsonFactory(new ObjectMapper()).createGenerator(writer)
) {
BigInteger index = BigInteger.ONE;
jsonGenerator.writeStartArray();
for (BigInteger v : (Iterable<BigInteger>) bigIntegerStream::iterator) {
try {
jsonGenerator.writeObject(new Sequence(index, v));
index = index.add(BigInteger.ONE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
jsonGenerator.writeEndArray();
} catch (UncheckedIOException ignored) {}
}
示例10: EclipseParser
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
/**
* Creates a new EclipseParser
*
* @param sourceFile String of source file to read
* @param outJ JSON parsed out
* @throws IOException when file can't be opened or errors in reading/writing
*/
public EclipseParser(String sourceFile, PrintStream outJ, boolean prettyprint) throws IOException {
File file = new File(sourceFile);
final BufferedReader reader = new BufferedReader(new FileReader(file));
char[] source = IOUtils.toCharArray(reader);
reader.close();
this.parser = ASTParser.newParser(AST.JLS8);
parser.setSource(source);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final JsonFactory jsonF = new JsonFactory();
jG = jsonF.createGenerator(outJ);
if (prettyprint) {
jG.setPrettyPrinter(new DefaultPrettyPrinter());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
SimpleModule module = new SimpleModule();
module.addSerializer(ASTNode.class, new NodeSerializer());
mapper.registerModule(module);
}
示例11: testParseException
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
public void testParseException() throws Exception
{
JsonFactory jf = new JsonFactory();
JsonParser p = jf.createParser(ObjectReadContext.empty(), " { garbage! }");
JsonParseException exc = null;
try {
p.nextToken();
p.nextToken();
fail("Should not get here");
} catch (JsonParseException e) {
exc = e;
}
p.close();
byte[] stuff = jdkSerialize(exc);
JsonParseException result = jdkDeserialize(stuff);
assertNotNull(result);
}
示例12: readJSONFile
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
@Test
public void readJSONFile() {
// new error-free endpoint
Endpoint endpoint = resources.createEndpoint("GET", "/test-json-file", "200");
// write json file on temporary directory
ObjectMapper mapperJson = new ObjectMapper(new JsonFactory());
File file = TempIO.buildFile("read-json-file", ".lyre", resources.getDirectory(0));
try {
mapperJson.writeValue(file, endpoint);
} catch (IOException e) {
fail("Couldn't write on temporary file: " + e.getMessage());
}
reader.read(file);
//should have a object node with json file on it.
assertThat(reader.getObjectNodes()).isNotEmpty();
assertThat(reader.getObjectNodes().get(file.getAbsolutePath())).isNotNull();
}
示例13: createMapper
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
private ObjectMapper createMapper(JsonFactory mapping, ClassLoader classLoader) {
ObjectMapper mapper = new ObjectMapper(mapping);
mapper.addMixIn(MasterSlaveServersConfig.class, MasterSlaveServersConfigMixIn.class);
mapper.addMixIn(SingleServerConfig.class, SingleSeverConfigMixIn.class);
mapper.addMixIn(Config.class, ConfigMixIn.class);
mapper.addMixIn(CodecProvider.class, ClassMixIn.class);
mapper.addMixIn(ResolverProvider.class, ClassMixIn.class);
mapper.addMixIn(Codec.class, ClassMixIn.class);
mapper.addMixIn(RedissonNodeInitializer.class, ClassMixIn.class);
mapper.addMixIn(LoadBalancer.class, ClassMixIn.class);
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("classFilter", SimpleBeanPropertyFilter.filterOutAllExcept());
mapper.setFilterProvider(filterProvider);
mapper.setSerializationInclusion(Include.NON_NULL);
if (classLoader != null) {
TypeFactory tf = TypeFactory.defaultInstance()
.withClassLoader(classLoader);
mapper.setTypeFactory(tf);
}
return mapper;
}
示例14: toJson
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的package包/类
/**
* @return A json representation of the topology. Intended for printing only.
*/
public static final String toJson(ITopology topo, boolean pretty) throws IOException {
ObjectMapper mapper = new ObjectMapper();
if (pretty)
mapper.enable(SerializationFeature.INDENT_OUTPUT);
StringWriter sw = new StringWriter();
JsonFactory f = mapper.getFactory();
JsonGenerator g = f.createGenerator(sw);
g.writeStartObject();
// use TreeSet to sort the list
g.writeObjectField("switches", new TreeSet<String>(topo.getSwitches().keySet()));
g.writeObjectField("links", new TreeSet<String>(topo.getLinks().keySet()));
g.writeEndObject();
g.close();
return sw.toString();
}
示例15: OutputStreamDemo
import com.fasterxml.jackson.core.JsonFactory; //导入依赖的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();
}