下面列出了com.fasterxml.jackson.core.JsonFactory#disable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public StreamingJsonDataValueSet( OutputStream out )
{
try
{
JsonFactory factory = new ObjectMapper().getFactory();
// Disables flushing every time that an object property is written to the stream
factory.disable( JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM );
// Do not attempt to balance unclosed tags
factory.disable( JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT );
generator = factory.createGenerator( out );
generator.writeStartObject();
}
catch ( IOException ignored )
{
}
}
/**
* Get an instance of JsonFactory configured using the settings from {@link #getParserConfig()}.
*
* @return A newly configured JsonFactory based on the currently enabled settings
*/
private JsonFactory configureNewJsonFactory() {
final JsonFactory nextJsonFactory = new JsonFactory();
// Disable features that may work for most JSON where the field names are
// in limited supply,
// but does not work for RDF/JSON where a wide range of URIs are used for
// subjects and predicates
nextJsonFactory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
nextJsonFactory.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
nextJsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
return nextJsonFactory;
}
@Provides
@Singleton
public JsonFactory provideJsonFactory() {
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
return jsonFactory;
}
@Override
public ObjectMapper getObject() throws Exception {
JsonFactory jsonFactory = new JsonFactory();
// disable the thread local to prevent memory leak
jsonFactory.disable(Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING);
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
return objectMapper;
}
/**
* Get an instance of JsonFactory configured using the settings from {@link #getParserConfig()}.
*
* @return A newly configured JsonFactory based on the currently enabled settings
*/
private JsonFactory configureNewJsonFactory() {
final JsonFactory nextJsonFactory = new JsonFactory();
// Disable features that may work for most JSON where the field names are
// in limited supply,
// but does not work for SPARQL/JSON where a wide range of URIs are used for
// subjects and predicates
nextJsonFactory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
nextJsonFactory.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
nextJsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
if (getParserConfig().isSet(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
getParserConfig().get(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_COMMENTS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS,
getParserConfig().get(JSONSettings.ALLOW_COMMENTS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS,
getParserConfig().get(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS,
getParserConfig().get(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_SINGLE_QUOTES)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,
getParserConfig().get(JSONSettings.ALLOW_SINGLE_QUOTES));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
getParserConfig().get(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
getParserConfig().get(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_YAML_COMMENTS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS,
getParserConfig().get(JSONSettings.ALLOW_YAML_COMMENTS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_TRAILING_COMMA)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_TRAILING_COMMA,
getParserConfig().get(JSONSettings.ALLOW_TRAILING_COMMA));
}
if (getParserConfig().isSet(JSONSettings.INCLUDE_SOURCE_IN_LOCATION)) {
nextJsonFactory.configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION,
getParserConfig().get(JSONSettings.INCLUDE_SOURCE_IN_LOCATION));
}
if (getParserConfig().isSet(JSONSettings.STRICT_DUPLICATE_DETECTION)) {
nextJsonFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION,
getParserConfig().get(JSONSettings.STRICT_DUPLICATE_DETECTION));
}
return nextJsonFactory;
}
/**
* Get an instance of JsonFactory configured using the settings from {@link #getParserConfig()}.
*
* @return A newly configured JsonFactory based on the currently enabled settings
*/
private JsonFactory configureNewJsonFactory() {
final JsonFactory nextJsonFactory = new JsonFactory();
// Disable features that may work for most JSON where the field names are
// in limited supply,
// but does not work for RDF/JSON where a wide range of URIs are used for
// subjects and predicates
nextJsonFactory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
nextJsonFactory.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
nextJsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
if (getParserConfig().isSet(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
getParserConfig().get(JSONSettings.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_COMMENTS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS,
getParserConfig().get(JSONSettings.ALLOW_COMMENTS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS,
getParserConfig().get(JSONSettings.ALLOW_NON_NUMERIC_NUMBERS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS,
getParserConfig().get(JSONSettings.ALLOW_NUMERIC_LEADING_ZEROS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_SINGLE_QUOTES)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,
getParserConfig().get(JSONSettings.ALLOW_SINGLE_QUOTES));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
getParserConfig().get(JSONSettings.ALLOW_UNQUOTED_CONTROL_CHARS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
getParserConfig().get(JSONSettings.ALLOW_UNQUOTED_FIELD_NAMES));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_YAML_COMMENTS)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS,
getParserConfig().get(JSONSettings.ALLOW_YAML_COMMENTS));
}
if (getParserConfig().isSet(JSONSettings.ALLOW_TRAILING_COMMA)) {
nextJsonFactory.configure(JsonParser.Feature.ALLOW_TRAILING_COMMA,
getParserConfig().get(JSONSettings.ALLOW_TRAILING_COMMA));
}
if (getParserConfig().isSet(JSONSettings.INCLUDE_SOURCE_IN_LOCATION)) {
nextJsonFactory.configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION,
getParserConfig().get(JSONSettings.INCLUDE_SOURCE_IN_LOCATION));
}
if (getParserConfig().isSet(JSONSettings.STRICT_DUPLICATE_DETECTION)) {
nextJsonFactory.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION,
getParserConfig().get(JSONSettings.STRICT_DUPLICATE_DETECTION));
}
return nextJsonFactory;
}
public void load(String fname, String... wikis) throws IOException {
JsonFactory jackf = new JsonFactory();
jackf.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
try (InputStream in = Util.openInput(fname);
JsonParser parser = jackf.createParser(in)) {
parser.setCodec(new ObjectMapper());
parser.nextToken();
assert (parser.getCurrentToken() == JsonToken.START_ARRAY);
parser.nextToken();
StringBuilder buf = new StringBuilder();
buf.append("WikiDataID");
for(int i = 0; i < wikis.length; i++) {
buf.append('\t').append(wikis[i]);
}
buf.append('\n');
System.out.print(buf.toString());
lines: while(parser.getCurrentToken() != JsonToken.END_ARRAY) {
assert (parser.getCurrentToken() == JsonToken.START_OBJECT);
JsonNode tree = parser.readValueAsTree();
JsonNode idn = tree.path("id");
if(!idn.isTextual()) {
System.err.println("Skipping entry without ID. " + parser.getCurrentLocation().toString());
continue;
}
// Check for instance-of for list and category pages:
JsonNode claims = tree.path("claims");
JsonNode iof = claims.path("P31");
if(iof.isArray()) {
for(Iterator<JsonNode> it = iof.elements(); it.hasNext();) {
final JsonNode child = it.next();
JsonNode ref = child.path("mainsnak").path("datavalue").path("value").path("numeric-id");
if(ref.isInt()) {
if(ref.asInt() == 13406463) { // "Wikimedia list article"
continue lines;
}
if(ref.asInt() == 4167836) { // "Wikimedia category article"
continue lines;
}
if(ref.asInt() == 4167410) { // "Wikimedia disambiguation page"
continue lines;
}
// Not reliable: if(ref.asInt() == 14204246) { // "Wikimedia
// project page"
}
}
}
buf.setLength(0);
buf.append(idn.asText());
JsonNode sl = tree.path("sitelinks");
boolean good = false;
for(int i = 0; i < wikis.length; i++) {
JsonNode wln = sl.path(wikis[i]).path("title");
buf.append('\t');
if(wln.isTextual()) {
buf.append(wln.asText());
good |= true;
}
}
if(good) {
buf.append('\n');
System.out.print(buf.toString());
}
parser.nextToken();
}
}
}