Snippet Preview
Snippet HTML Code
1
package com.fasterxml.jackson.databind.ser.std;
2
3
import java.io.IOException;
4
import java.net.InetAddress;
5
6
import com.fasterxml.jackson.core.*;
7
8
import com.fasterxml.jackson.databind.SerializerProvider;
9
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
java.net.InetAddress
14
15
public class InetAddressSerializer
16
extends StdScalarSerializer<InetAddress>
17
{
18
public final static InetAddressSerializer instance = new InetAddressSerializer();
19
20
public InetAddressSerializer() { super(InetAddress.class); }
21
22
@Override
23
public void serialize(InetAddress value, JsonGenerator jgen, SerializerProvider provider)
24
throws IOException, JsonGenerationException
25
26
// Ok: get textual description; choose "more specific" part
27
String str = value.toString().trim();
28
int ix = str.indexOf('/');
29
if (ix >= 0) {
30
if (ix == 0) { // missing host name; use address
31
str = str.substring(1);
32
} else { // otherwise use name
33
str = str.substring(0, ix);
34
}
35
36
jgen.writeString(str);
37
38
39
40
public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider,
41
TypeSerializer typeSer)
42
43
44
// Better ensure we don't use specific sub-classes...
45
typeSer.writeTypePrefixForScalar(value, jgen, InetAddress.class);
46
serialize(value, jgen, provider);
47
typeSer.writeTypeSuffixForScalar(value, jgen);
48
49