Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.fasterxml.jackson.databind.ser.std;
 
 
Simple serializer for java.net.InetSocketAddress.
    public final static InetSocketAddressSerializer instance = new InetSocketAddressSerializer();
    public InetSocketAddressSerializer() { super(InetSocketAddress.class); }
    @Override
    public void serialize(InetSocketAddress valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        InetAddress addr = value.getAddress();
        String str = addr == null ? value.getHostName() : addr.toString().trim();
        int ix = str.indexOf('/');
        if (ix >= 0) {
            if (ix == 0) { // missing host name; use address
                str = addr instanceof Inet6Address
                        ? "[" + str.substring(1) + "]" // bracket IPv6 addresses with
                        : str.substring(1);
            } else { // otherwise use name
                str = str.substring(0, ix);
            }
        }
        jgen.writeString(str + ":" + value.getPort());
    }
    @Override
    public void serializeWithType(InetSocketAddress valueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonGenerationException
    {
        // Better ensure we don't use specific sub-classes...
        typeSer.writeTypePrefixForScalar(valuejgenInetSocketAddress.class);
        serialize(valuejgenprovider);
        typeSer.writeTypeSuffixForScalar(valuejgen);
    }
New to GrepCode? Check out our FAQ X