下面列出了怎么用com.fasterxml.jackson.core.PrettyPrinter的API类实例代码及写法,或者点击链接到github查看源代码。
public static DataShape generateInspections(URL[] classpath, DataShape shape) throws IOException, ClassNotFoundException {
JavaClass c = inspect(classpath, shape);
List<DataShape> variants = new ArrayList<>(shape.getVariants().size());
for (DataShape s : shape.getVariants()) {
variants.add(generateInspections(classpath, s));
}
String inspection = io.atlasmap.v2.Json.mapper().writer((PrettyPrinter) null).writeValueAsString(c);
return new DataShape.Builder()
.createFrom(shape)
.specification(inspection)
.variants(variants)
.compress()
.build();
}
private static PrettyPrinter buildDefaultPrettyPrinter() {
DefaultPrettyPrinter pp =
new DefaultPrettyPrinter("") {
@Override
public DefaultPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces =
separators.getObjectFieldValueSeparator() + " ";
return this;
}
};
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
pp.indentArraysWith(lfOnlyIndenter);
pp.indentObjectsWith(lfOnlyIndenter);
return pp;
}
@Override
protected void writeFieldValue(final JsonGenerator generator) throws IOException {
final PrettyPrinter prettyPrinter = generator.getPrettyPrinter();
if (prettyPrinter == null) {
super.writeFieldValue(generator);
} else {
final JsonFactory factory = generator.getCodec().getFactory();
// append to existing tree event by event
try (final JsonParser parser = factory.createParser(super.getFieldValue().toString())) {
while (parser.nextToken() != null) {
generator.copyCurrentEvent(parser);
}
}
}
}
public void writeJSON( File target, DocumentContext contents ) throws ManipulationException
{
try
{
PrettyPrinter dpp = new MyPrettyPrinter( getCharset() );
ObjectMapper mapper = new ObjectMapper();
String jsonString = contents.jsonString();
String pretty = mapper.writer( dpp ).writeValueAsString( mapper.readValue( jsonString, Object.class ) );
Charset cs = getCharset();
FileOutputStream fileOutputStream = new FileOutputStream( target );
try (OutputStreamWriter p = new OutputStreamWriter( fileOutputStream, cs ) )
{
p.write( pretty );
p.append( getEOL() );
}
}
catch ( IOException e )
{
logger.error( "Unable to write JSON string: ", e );
throw new ManipulationException( "Unable to write JSON string", e );
}
}
static String includeResources(String specification, String... resourceTypes) throws IOException {
if (resourceTypes != null && resourceTypes.length != 0) {
XmlDocument document = MAPPER.readValue(specification, XmlDocument.class);
includeResources(null, document, resourceTypes);
return MAPPER.writer((PrettyPrinter) null).writeValueAsString(document);
}
return specification;
}
static Function<Object, String> defaultJsonFunction() {
ObjectMapper objectMapper = buildObjectMapper();
PrettyPrinter pp = buildDefaultPrettyPrinter();
return (object) -> {
try {
return objectMapper.writer(pp).writeValueAsString(object);
} catch (Exception e) {
throw new SnapshotMatchException(e.getMessage());
}
};
}
private static PrettyPrinter initSsePrettyPrinter() {
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.indentObjectsWith(new DefaultIndenter(" ", "\ndata:"));
return printer;
}
private static PrettyPrinter initSsePrettyPrinter() {
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.indentObjectsWith(new DefaultIndenter(" ", "\ndata:"));
return printer;
}
@Override
protected PrettyPrinter newCompactPrinter() {
return new MinimalPrettyPrinter();
}
@Override
protected PrettyPrinter newPrettyPrinter() {
return new DefaultPrettyPrinter();
}
public ObjectWriter with(PrettyPrinter pp) {
return new FilterObjectWriter(this, _config, _generatorSettings.with(pp), _prefetch);
}
@Override
protected PrettyPrinter newCompactPrinter() {
return new MinimalPrettyPrinter();
}
@Override
protected PrettyPrinter newPrettyPrinter() {
return new DefaultPrettyPrinter();
}
private MessageMarshaller(@Nullable PrettyPrinter prettyPrinter, MarshallerRegistry registry) {
this.prettyPrinter = prettyPrinter;
this.registry = registry;
}
public ExtendedObjectWriter(ObjectMapper objectMapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp) {
super(objectMapper, config, rootType, pp);
}
public ObjectWriter _newWriter(SerializationConfig config, JavaType rootType, PrettyPrinter pp) {
return new ExtendedObjectWriter(this, config, rootType, pp);
}
public static PrettyPrinter getPrettyPrinter() {
CustomPrettyPrinter prettyPrinter = new CustomPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
return prettyPrinter;
}
@Override
protected PrettyPrinter newCompactPrinter() {
return new MinimalPrettyPrinter();
}
@Override
protected PrettyPrinter newPrettyPrinter() {
return new DefaultPrettyPrinter();
}
@Override
protected PrettyPrinter newCompactPrinter() {
// Yes, null is the proper answer.
return null;
}
@Override
protected PrettyPrinter newPrettyPrinter() {
return new Log4jXmlPrettyPrinter(DEFAULT_INDENT);
}
@Override
protected PrettyPrinter newCompactPrinter() {
return new MinimalPrettyPrinter();
}
@Override
protected PrettyPrinter newPrettyPrinter() {
return new DefaultPrettyPrinter();
}
/**
* Instantiates a new Registered service json serializer.
*
* @param prettyPrinter the pretty printer
*/
public AbstractJacksonBackedJsonSerializer(final PrettyPrinter prettyPrinter) {
this.objectMapper = initializeObjectMapper();
this.prettyPrinter = prettyPrinter;
}
/**
* Instantiates a new Registered service json serializer.
*
* @param objectMapper the object mapper
* @param prettyPrinter the pretty printer
*/
public AbstractJacksonBackedJsonSerializer(final ObjectMapper objectMapper, final PrettyPrinter prettyPrinter) {
this.objectMapper = objectMapper;
this.prettyPrinter = prettyPrinter;
}
/**
* Sets the pretty printer to pretty-print the JSON with. Note that this
* method implicitly enables indenting, so {@code setPrettyPrint(true)} does
* not also need to be called.
* @param prettyPrinter the custom pretty printer (defaults to an instance
* of {@link JCalPrettyPrinter}, if {@code setPrettyPrint(true)} has been
* called)
*/
public void setPrettyPrinter(PrettyPrinter prettyPrinter) {
prettyPrint = true;
this.prettyPrinter = prettyPrinter;
}
abstract protected PrettyPrinter newCompactPrinter();
abstract protected PrettyPrinter newPrettyPrinter();
protected abstract PrettyPrinter newCompactPrinter();
protected abstract PrettyPrinter newPrettyPrinter();