|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.net.InetAddress;
|
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JsonFormat; |
6 | 7 | import com.fasterxml.jackson.core.*;
|
| 8 | +import com.fasterxml.jackson.databind.BeanProperty; |
| 9 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 10 | +import com.fasterxml.jackson.databind.JsonSerializer; |
7 | 11 | import com.fasterxml.jackson.databind.SerializerProvider;
|
8 | 12 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
| 13 | +import com.fasterxml.jackson.databind.ser.ContextualSerializer; |
9 | 14 |
|
10 | 15 | /**
|
11 | 16 | * Simple serializer for {@link java.net.InetAddress}. Main complexity is
|
12 | 17 | * with registration, since same serializer is to be used for sub-classes.
|
| 18 | + *<p> |
| 19 | + * Since 2.9 allows use of {@link JsonFormat} configuration (annotation, |
| 20 | + * per-type defaulting) so that if <code>JsonFormat.Shape.NUMBER</code> |
| 21 | + * (or <code>ARRAY</code>) is used, will serialize as "host address" |
| 22 | + * (dotted numbers) instead of simple conversion. |
13 | 23 | */
|
14 | 24 | @SuppressWarnings("serial")
|
15 | 25 | public class InetAddressSerializer
|
16 | 26 | extends StdScalarSerializer<InetAddress>
|
| 27 | + implements ContextualSerializer |
17 | 28 | {
|
18 |
| - public InetAddressSerializer() { super(InetAddress.class); } |
| 29 | + /** |
| 30 | + * @since 2.9 |
| 31 | + */ |
| 32 | + protected final boolean _asNumeric; |
19 | 33 |
|
| 34 | + public InetAddressSerializer() { |
| 35 | + this(false); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @since 2.9 |
| 40 | + */ |
| 41 | + public InetAddressSerializer(boolean asNumeric) { |
| 42 | + super(InetAddress.class); |
| 43 | + _asNumeric = asNumeric; |
| 44 | + } |
| 45 | + |
20 | 46 | @Override
|
21 |
| - public void serialize(InetAddress value, JsonGenerator jgen, SerializerProvider provider) throws IOException |
| 47 | + public JsonSerializer<?> createContextual(SerializerProvider serializers, |
| 48 | + BeanProperty property) throws JsonMappingException |
22 | 49 | {
|
23 |
| - // Ok: get textual description; choose "more specific" part |
24 |
| - String str = value.toString().trim(); |
25 |
| - int ix = str.indexOf('/'); |
26 |
| - if (ix >= 0) { |
27 |
| - if (ix == 0) { // missing host name; use address |
28 |
| - str = str.substring(1); |
29 |
| - } else { // otherwise use name |
30 |
| - str = str.substring(0, ix); |
| 50 | + JsonFormat.Value format = findFormatOverrides(serializers, |
| 51 | + property, handledType()); |
| 52 | + boolean asNumeric = false; |
| 53 | + if (format != null) { |
| 54 | + JsonFormat.Shape shape = format.getShape(); |
| 55 | + if (shape.isNumeric() || shape == JsonFormat.Shape.ARRAY) { |
| 56 | + asNumeric = true; |
| 57 | + } |
| 58 | + } |
| 59 | + if (asNumeric != _asNumeric) { |
| 60 | + return new InetAddressSerializer(asNumeric); |
| 61 | + } |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void serialize(InetAddress value, JsonGenerator g, SerializerProvider provider) throws IOException |
| 67 | + { |
| 68 | + String str; |
| 69 | + |
| 70 | + if (_asNumeric) { // since 2.9 |
| 71 | + str = value.getHostAddress(); |
| 72 | + } else { |
| 73 | + // Ok: get textual description; choose "more specific" part |
| 74 | + str = value.toString().trim(); |
| 75 | + int ix = str.indexOf('/'); |
| 76 | + if (ix >= 0) { |
| 77 | + if (ix == 0) { // missing host name; use address |
| 78 | + str = str.substring(1); |
| 79 | + } else { // otherwise use name |
| 80 | + str = str.substring(0, ix); |
| 81 | + } |
31 | 82 | }
|
32 | 83 | }
|
33 |
| - jgen.writeString(str); |
| 84 | + g.writeString(str); |
34 | 85 | }
|
35 | 86 |
|
36 | 87 | @Override
|
37 |
| - public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException, JsonGenerationException |
| 88 | + public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider, |
| 89 | + TypeSerializer typeSer) throws IOException, JsonGenerationException |
38 | 90 | {
|
39 | 91 | // Better ensure we don't use specific sub-classes...
|
40 | 92 | typeSer.writeTypePrefixForScalar(value, jgen, InetAddress.class);
|
|
0 commit comments