Skip to content

Commit

Permalink
Add scaffolding for #2815
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 11, 2020
1 parent 240fcbf commit 472c071
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public InetAddressSerializer(boolean asNumeric) {
super(InetAddress.class);
_asNumeric = asNumeric;
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.io.IOException;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.util.TokenBuffer;

/**
Expand All @@ -19,6 +21,7 @@
@SuppressWarnings("serial")
public class UUIDSerializer
extends StdScalarSerializer<UUID>
implements ContextualSerializer // since 2.11.3 (for databind#2815)
{
final static char[] HEX_CHARS = "0123456789abcdef".toCharArray();

Expand All @@ -35,6 +38,26 @@ public boolean isEmpty(SerializerProvider prov, UUID value)
return false;
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, handledType());
Boolean asBinary = null;
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (shape == JsonFormat.Shape.BINARY) {
asBinary = true;
} else if (shape == JsonFormat.Shape.STRING) {
asBinary = false;
}
// otherwise leave as `null` meaning about same as NATURAL
}
// !!! TODO:
return this;
}

@Override
public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider)
throws IOException
Expand All @@ -50,7 +73,7 @@ public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider
return;
}
}

// UUID.toString() works ok functionally, but we can make it go much faster
// (by 4x with micro-benchmark)

Expand Down Expand Up @@ -93,7 +116,6 @@ private static void _appendShort(int bits, char[] ch, int offset)
ch[++offset] = HEX_CHARS[(bits >> 8) & 0xF];
ch[++offset] = HEX_CHARS[(bits >> 4) & 0xF];
ch[++offset] = HEX_CHARS[bits & 0xF];

}

private final static byte[] _asBytes(UUID uuid)
Expand Down

0 comments on commit 472c071

Please sign in to comment.