下面列出了怎么用com.fasterxml.jackson.annotation.JsonFormat.Shape的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Helper method called to check whether serialization should be done using
* index (number) or not.
*/
protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass,
JsonFormat.Value format, boolean fromClass,
Boolean defaultValue)
{
JsonFormat.Shape shape = (format == null) ? null : format.getShape();
if (shape == null) {
return defaultValue;
}
// i.e. "default", check dynamically
if (shape == Shape.ANY || shape == Shape.SCALAR) {
return defaultValue;
}
// 19-May-2016, tatu: also consider "natural" shape
if (shape == Shape.STRING || shape == Shape.NATURAL) {
return Boolean.FALSE;
}
// 01-Oct-2014, tatu: For convenience, consider "as-array" to also mean 'yes, use index')
if (shape.isNumeric() || (shape == Shape.ARRAY)) {
return Boolean.TRUE;
}
// 07-Mar-2017, tatu: Also means `OBJECT` not available as property annotation...
throw new IllegalArgumentException(String.format(
"Unsupported serialization shape (%s) for Enum %s, not supported as %s annotation",
shape, enumClass.getName(), (fromClass? "class" : "property")));
}
protected boolean useTimestamp(SerializerProvider provider) {
if (_useTimestamp != null) {
return _useTimestamp.booleanValue();
}
if (_shape != null) {
if (_shape == Shape.STRING) {
return false;
}
if (_shape == Shape.NUMBER_INT) {
return true;
}
}
// assume that explicit formatter definition implies use of textual format
return (_formatter == null) && (provider != null)
&& provider.isEnabled(getTimestampsFeature());
}
/**
* Add the common property parameters to the code builder.
*
* @param paramBuilder the code builder
* @param property the information about the property
*/
protected final void buildCommonPropertyParameters( CodeBlock.Builder paramBuilder, PropertyInfo property ) {
if ( property.getFormat().isPresent() ) {
JsonFormat format = property.getFormat().get();
if ( !Strings.isNullOrEmpty( format.pattern() ) ) {
paramBuilder.add( "\n.setPattern($S)", format.pattern() );
}
paramBuilder.add( "\n.setShape($T.$L)", Shape.class, format.shape().name() );
if ( !Strings.isNullOrEmpty( format.locale() ) && !JsonFormat.DEFAULT_LOCALE.equals( format.locale() ) ) {
logger.log( Type.WARN, "JsonFormat.locale is not supported by default" );
paramBuilder.add( "\n.setLocale($S)", format.locale() );
}
}
if ( property.getIgnoredProperties().isPresent() ) {
for ( String ignoredProperty : property.getIgnoredProperties().get() ) {
paramBuilder.add( "\n.addIgnoredProperty($S)", ignoredProperty );
}
}
}
@Override
protected void doSerialize(JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params) {
if ((ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING) {
writer.value(value.getTime());
} else {
String date = JacksonContextProvider.get().dateFormat().format(params, value);
if (null == params.getPattern()) {
writer.unescapeValue(date);
} else {
writer.value(date);
}
}
}
protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase<?> base,
DateTimeFormatter dtf,
Boolean useTimestamp, Boolean useNanoseconds,
JsonFormat.Shape shape)
{
super(base.handledType());
_useTimestamp = useTimestamp;
_useNanoseconds = useNanoseconds;
_formatter = dtf;
_shape = shape;
}
protected boolean useNanoseconds(SerializerProvider provider) {
if (_useNanoseconds != null) {
return _useNanoseconds.booleanValue();
}
if (_shape != null) {
if (_shape == Shape.NUMBER_INT) {
return false;
}
if (_shape == Shape.NUMBER_FLOAT) {
return true;
}
}
return (provider != null)
&& provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
}
@Override
protected void doSerialize( JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params ) {
if ( (ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING ) {
writer.value( value.getTime() );
} else {
String date = DateFormat.format( params, value );
if ( null == params.getPattern() ) {
writer.unescapeValue( date );
} else {
writer.value( date );
}
}
}
/**
* {@inheritDoc}
*
* <p>Setter for the field <code>shape</code>.</p>
*/
@Override
public JsonDeserializerParameters setShape(Shape shape) {
this.shape = shape;
return this;
}
/**
* {@inheritDoc}
*
* <p>Setter for the field <code>shape</code>.</p>
*/
@Override
public JsonSerializerParameters setShape(Shape shape) {
this.shape = shape;
return this;
}
/**
* {@inheritDoc}
*
* <p>Setter for the field <code>shape</code>.</p>
*/
@Override
public JsonDeserializerParameters setShape(Shape shape) {
this.shape = shape;
return this;
}
/**
* {@inheritDoc}
*
* <p>Setter for the field <code>shape</code>.</p>
*/
@Override
public JsonSerializerParameters setShape(Shape shape) {
this.shape = shape;
return this;
}
protected abstract JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape);
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
if (format != null) {
Boolean useTimestamp = null;
// Simple case first: serialize as numeric timestamp?
JsonFormat.Shape shape = format.getShape();
if (shape == JsonFormat.Shape.ARRAY || shape.isNumeric() ) {
useTimestamp = Boolean.TRUE;
} else {
useTimestamp = (shape == JsonFormat.Shape.STRING) ? Boolean.FALSE : null;
}
DateTimeFormatter dtf = _formatter;
// If not, do we have a pattern?
if (format.hasPattern()) {
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : prov.getLocale();
if (locale == null) {
dtf = DateTimeFormatter.ofPattern(pattern);
} else {
dtf = DateTimeFormatter.ofPattern(pattern, locale);
}
//Issue #69: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
dtf = dtf.withZone(format.getTimeZone().toZoneId());
}
}
JSR310FormattedSerializerBase<?> ser = this;
if ((shape != _shape) || (useTimestamp != _useTimestamp) || (dtf != _formatter)) {
ser = ser.withFormat(dtf, useTimestamp, shape);
}
Boolean writeZoneId = format.getFeature(JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID);
Boolean writeNanoseconds = format.getFeature(JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
if ((writeZoneId != null) || (writeNanoseconds != null)) {
ser = ser.withFeatures(writeZoneId, writeNanoseconds);
}
return ser;
}
return this;
}
protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
Shape shape) {
super(base);
_formatter = base._formatter;
_shape = shape;
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
JSR310DateTimeDeserializerBase<?> deser = this;
if (format != null) {
// 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
if (format.hasLenient()) {
Boolean leniency = format.getLenient();
if (leniency != null) {
deser = deser.withLeniency(leniency);
}
}
if (format.hasPattern()) {
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
if (acceptCaseInsensitiveValues(ctxt, format)) {
builder.parseCaseInsensitive();
}
builder.appendPattern(pattern);
DateTimeFormatter df;
if (locale == null) {
df = builder.toFormatter();
} else {
df = builder.toFormatter(locale);
}
// [#148]: allow strict parsing
if (!deser.isLenient()) {
df = df.withResolverStyle(ResolverStyle.STRICT);
}
// [#69]: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
df = df.withZone(format.getTimeZone().toZoneId());
}
deser = deser.withDateFormat(df);
}
// [#58]: For LocalDate deserializers we need to configure the formatter with
//a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
JsonFormat.Shape shape = format.getShape();
if (shape != null && shape != _shape) {
deser = deser.withShape(shape);
}
// any use for TimeZone?
}
return deser;
}
@Override
public JsonFormat.Value findFormat(MapperConfig<?> config, Annotated a) {
return new JsonFormat.Value().withShape(JsonFormat.Shape.ARRAY);
}
public FormatDateBean( @JsonProperty( "dateInParameter" ) @JsonFormat( shape = Shape.STRING,
pattern = "'P'yyyy/MM/dd" ) Date dateParameter ) {
this.dateInParameter = dateParameter;
}
/**
* {@inheritDoc}
*
* <p>Getter for the field <code>shape</code>.</p>
*/
@Override
public Shape getShape() {
return shape;
}
/**
* {@inheritDoc}
*
* <p>Getter for the field <code>shape</code>.</p>
*/
@Override
public Shape getShape() {
return shape;
}
/**
* {@inheritDoc}
*
* <p>Getter for the field <code>shape</code>.</p>
*/
@Override
public Shape getShape() {
return shape;
}
/**
* {@inheritDoc}
*
* <p>Getter for the field <code>shape</code>.</p>
*/
@Override
public Shape getShape() {
return shape;
}
/**
* <p>Getter for the field <code>shape</code>.</p>
*
* @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
*/
public Shape getShape() {
return shape;
}
/**
* <p>Setter for the field <code>shape</code>.</p>
*
* @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
* @return a {@link com.github.nmorel.gwtjackson.client.JsonDeserializerParameters} object.
*/
public JsonDeserializerParameters setShape( Shape shape ) {
this.shape = shape;
return this;
}
/**
* <p>Getter for the field <code>shape</code>.</p>
*
* @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
*/
public Shape getShape() {
return shape;
}
/**
* <p>Setter for the field <code>shape</code>.</p>
*
* @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
* @return a {@link com.github.nmorel.gwtjackson.client.JsonSerializerParameters} object.
*/
public JsonSerializerParameters setShape( Shape shape ) {
this.shape = shape;
return this;
}
protected abstract JSR310DateTimeDeserializerBase<T> withShape(Shape shape);