本文整理匯總了Java中com.fasterxml.jackson.core.util.DefaultPrettyPrinter類的典型用法代碼示例。如果您正苦於以下問題:Java DefaultPrettyPrinter類的具體用法?Java DefaultPrettyPrinter怎麽用?Java DefaultPrettyPrinter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DefaultPrettyPrinter類屬於com.fasterxml.jackson.core.util包,在下文中一共展示了DefaultPrettyPrinter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: marshalJson
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
public static String marshalJson(CASServiceResponse serviceResponse) {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//Force newlines to be LF (default is system dependent)
DefaultPrettyPrinter printer = new DefaultPrettyPrinter()
.withObjectIndenter(new DefaultIndenter(" ", "\n"));
//create wrapper node
Map<String, Object> casModel = new HashMap<>();
casModel.put("serviceResponse", serviceResponse);
try {
return mapper.writer(printer).writeValueAsString(casModel);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
示例2: testCustomSeparatorsWithPP
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
public void testCustomSeparatorsWithPP() throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.setPrettyPrinter(new DefaultPrettyPrinter().withSeparators(Separators.createDefaultInstance()
.withObjectFieldValueSeparator('=')
.withObjectEntrySeparator(';')
.withArrayValueSeparator('|')));
_writeTestDocument(gen);
gen.close();
assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF +
" \"f\" = null;" + DefaultIndenter.SYS_LF +
" \"f2\" = null" + DefaultIndenter.SYS_LF +
"} ]", sw.toString());
}
示例3: testCustomSeparatorsWithPPWithoutSpaces
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
public void testCustomSeparatorsWithPPWithoutSpaces() throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.setPrettyPrinter(new DefaultPrettyPrinter().withSeparators(Separators.createDefaultInstance()
.withObjectFieldValueSeparator('=')
.withObjectEntrySeparator(';')
.withArrayValueSeparator('|'))
.withoutSpacesInObjectEntries());
_writeTestDocument(gen);
gen.close();
assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF +
" \"f\"=null;" + DefaultIndenter.SYS_LF +
" \"f2\"=null" + DefaultIndenter.SYS_LF +
"} ]", sw.toString());
}
示例4: objectMapper
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
public static ObjectMapper objectMapper() {
return new ObjectMapper()
// Property visibility
.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS)
.setDefaultVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC))
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true)
// Property naming and order
.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE)
// Customised de/serializers
// Formats, locals, encoding, binary data
.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"))
.setDefaultPrettyPrinter(new DefaultPrettyPrinter())
.setLocale(Locale.CANADA);
}
示例5: exportTo
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
/**
* Exports data to a {@link Writer}.
*
* @param writer the writer
* @throws NitriteIOException if there is any error while writing the data.
*/
public void exportTo(Writer writer) {
JsonGenerator generator;
try {
generator = jsonFactory.createGenerator(writer);
generator.setPrettyPrinter(new DefaultPrettyPrinter());
} catch (IOException ioe) {
throw new NitriteIOException(EXPORT_WRITER_ERROR, ioe);
}
NitriteJsonExporter jsonExporter = new NitriteJsonExporter(db);
jsonExporter.setGenerator(generator);
jsonExporter.setOptions(options);
try {
jsonExporter.exportData();
} catch (IOException | ClassNotFoundException e) {
throw new NitriteIOException(EXPORT_WRITE_ERROR, e);
}
}
示例6: EclipseParser
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的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);
}
示例7: createJsonWriter
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Nonnull
private static ObjectWriter createJsonWriter(final boolean prettyPrint) {
final ObjectMapper mapper = new ObjectMapper();
if (!prettyPrint) {
return mapper.writer();
}
final DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);
final DefaultPrettyPrinter printer = new DefaultPrettyPrinter("");
printer.withoutSpacesInObjectEntries();
printer.indentObjectsWith(indenter);
printer.indentArraysWith(indenter);
return mapper.writer(printer);
}
示例8: serializationEntityTypeTest
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationEntityTypeTest() throws JsonProcessingException {
ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter());
EntityType entityType = new EntityType();
Map<String,AttributeType> attrs = new HashMap<>();
AttributeType tempAttribute = new AttributeType("urn:phenomenum:temperature");
attrs.put("temperature", tempAttribute);
AttributeType humidityAttribute = new AttributeType("percentage");
attrs.put("humidity", humidityAttribute);
AttributeType pressureAttribute = new AttributeType("null");
attrs.put("pressure", pressureAttribute);
entityType.setAttrs(attrs);
entityType.setCount(7);
String json = writer.writeValueAsString(entityType);
assertEquals(jsonString, json);
}
示例9: serializationRegistrationTest
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationRegistrationTest() throws JsonProcessingException, MalformedURLException {
ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter());
Registration registration = new Registration("abcde", new URL("http://localhost:1234"));
registration.setDuration("PT1M");
SubjectEntity subjectEntity = new SubjectEntity();
subjectEntity.setType(Optional.of("Room"));
SubjectRegistration subjectRegistration = new SubjectRegistration(Collections.singletonList(subjectEntity), Collections.singletonList("humidity"));
registration.setSubject(subjectRegistration);
String json = writer.writeValueAsString(registration);
String jsonString ="{\n" +
" \"id\" : \"abcde\",\n" +
" \"subject\" : {\n" +
" \"entities\" : [ {\n" +
" \"type\" : \"Room\"\n" +
" } ],\n" +
" \"attributes\" : [ \"humidity\" ]\n" +
" },\n" +
" \"callback\" : \"http://localhost:1234\",\n" +
" \"duration\" : \"PT1M\"\n" +
"}";
assertEquals(jsonString, json);
}
示例10: serializationEntityTest
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationEntityTest() throws JsonProcessingException {
ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter());
Entity entity = new Entity("Bcn-Welt", "Room");
HashMap<String,Attribute> attributes = new HashMap<String, Attribute>();
Attribute tempAttribute = new Attribute(21.7);
attributes.put("temperature", tempAttribute);
Attribute humidityAttribute = new Attribute(60);
attributes.put("humidity", humidityAttribute);
entity.setAttributes(attributes);
String json = writer.writeValueAsString(entity);
String jsonString = "{\n" +
" \"id\" : \"Bcn-Welt\",\n" +
" \"type\" : \"Room\",\n" +
" \"temperature\" : {\n" +
" \"value\" : 21.7,\n" +
" \"metadata\" : { }\n" +
" },\n" +
" \"humidity\" : {\n" +
" \"value\" : 60,\n" +
" \"metadata\" : { }\n" +
" }\n" +
"}";
assertEquals(jsonString, json);
}
示例11: serializationSubscriptionTest
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationSubscriptionTest() throws JsonProcessingException, MalformedURLException {
ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter());
SubjectEntity subjectEntity = new SubjectEntity();
subjectEntity.setId(Optional.of("Bcn_Welt"));
subjectEntity.setType(Optional.of("Room"));
Condition condition = new Condition();
condition.setAttributes(Collections.singletonList("temperature"));
condition.setExpression("q", "temperature>40");
SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity), condition);
List<String> attributes = new ArrayList<>();
attributes.add("temperature");
attributes.add("humidity");
Notification notification = new Notification(attributes, new URL("http://localhost:1234"));
notification.setThrottling(Optional.of(new Long(5)));
notification.setTimesSent(12);
notification.setLastNotification(Instant.parse("2015-10-05T16:00:00.10Z"));
notification.setHeader("X-MyHeader", "foo");
notification.setQuery("authToken", "bar");
notification.setAttrsFormat(Optional.of(Notification.Format.keyValues));
String json = writer.writeValueAsString(new Subscription("abcdefg", subjectSubscription, notification, Instant.parse("2016-04-05T14:00:00.20Z"), Subscription.Status.active));
assertEquals(jsonString, json);
}
示例12: toString
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
/**
@return The JSON pretty-printed representation of this configuration.
*/
@Override
public final String toString() {
final ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true);
final DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(
"\t", DefaultIndenter.SYS_LF
);
final DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.withObjectIndenter(indenter);
printer.withArrayIndenter(indenter);
try {
return mapper.writer(printer).writeValueAsString(this);
} catch(final JsonProcessingException e) {
throw new AssertionError(e);
}
}
示例13: getQueryResult
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的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();
}
示例14: serializationSimpleQueryContext
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationSimpleQueryContext() throws IOException {
QueryContext queryContext = createQueryContextTemperature();
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
String json = writer.writeValueAsString(queryContext);
List<EntityId> entityIdList = JsonPath.read(json, "$.entities[*]");
assertEquals(1, entityIdList.size());
assertEquals("S*", JsonPath.read(json, "$.entities[0].id"));
assertEquals("TempSensor", JsonPath.read(json, "$.entities[0].type"));
assertEquals(true, JsonPath.read(json, "$.entities[0].isPattern"));
List<String> attributeList = JsonPath.read(json, "$.attributes[*]");
assertEquals(1, attributeList.size());
assertEquals("temp", JsonPath.read(json, "$.attributes[0]"));
}
示例15: serializationSimpleRegisterContext
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; //導入依賴的package包/類
@Test
public void serializationSimpleRegisterContext() throws URISyntaxException, JsonProcessingException {
RegisterContext registerContext = createRegisterContextTemperature();
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
String json = writer.writeValueAsString(registerContext);
List<ContextRegistration> contextRegistrationList = JsonPath.read(json, "$.contextRegistrations[*]");
assertEquals(1, contextRegistrationList.size());
List<EntityId> entityIdList = JsonPath.read(json, "$.contextRegistrations[0].entities[*]");
assertEquals(1, entityIdList.size());
assertEquals("Room*", JsonPath.read(json, "$.contextRegistrations[0].entities[0].id"));
assertEquals("Room", JsonPath.read(json, "$.contextRegistrations[0].entities[0].type"));
assertEquals(true, JsonPath.read(json, "$.contextRegistrations[0].entities[0].isPattern"));
List<ContextRegistrationAttribute> attributes = JsonPath.read(json, "$.contextRegistrations[0].attributes[*]");
assertEquals(1, attributes.size());
assertEquals("temperature", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].name"));
assertEquals("float", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].type"));
assertEquals(false, JsonPath.read(json, "$.contextRegistrations[0].attributes[0].isDomain"));
assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.contextRegistrations[0].providingApplication"));
assertEquals("PT10S", JsonPath.read(json, "$.duration"));
}