Skip to content

Commit

Permalink
Fix #362
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 8, 2013
1 parent c4a0032 commit 9077a58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Version: 2.3.1 (xx-Dec-2013)
(reported by gaff78@github)
#358: `IterableSerializer` ignoring annotated content serializer
(reported by Florian S)
#362: UUID output as Base64 String with ObjectMapper.convertValue()
(reported by jknack@github)

------------------------------------------------------------------------
=== History: ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.util.TokenBuffer;

/**
* Specialized {@link JsonSerializer} to output {@link java.util.UUID}s.
Expand Down Expand Up @@ -41,8 +42,15 @@ public void serialize(UUID value, JsonGenerator jgen, SerializerProvider provide
{
// First: perhaps we could serialize it as raw binary data?
if (jgen.canWriteBinaryNatively()) {
jgen.writeBinary(_asBytes(value));
return;
/* 07-Dec-2013, tatu: One nasty case; that of TokenBuffer. While it can
* technically retain binary data, we do not want to do use binary
* with it, as that results in UUIDs getting converted to Base64 for
* most conversions.
*/
if (!(jgen instanceof TokenBuffer)) {
jgen.writeBinary(_asBytes(value));
return;
}
}

// UUID.toString() works ok functionally, but we can make it go much faster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class TestJdkTypes
extends com.fasterxml.jackson.databind.BaseMapTest
{
private final ObjectMapper MAPPER = new ObjectMapper();
private final ObjectMapper MAPPER = objectMapper();

/**
* Unit test to catch bug [JACKSON-8].
Expand Down Expand Up @@ -144,6 +144,10 @@ public void testUUIDs() throws IOException
UUID uuid = UUID.fromString(value);
String json = MAPPER.writeValueAsString(uuid);
assertEquals(quote(uuid.toString()), json);

// Also, wrt [#362], should convert cleanly
String str = MAPPER.convertValue(uuid, String.class);
assertEquals(value, str);
}

// then use templating; note that these are not exactly valid UUIDs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ public void testWithUUID() throws IOException
UUID out = mapper.readValue(buf.asParser(), UUID.class);
assertEquals(uuid.toString(), out.toString());

// second part: ensure it's written as binary...
// second part: As per [#362], should NOT use binary with TokenBuffer
JsonParser jp = buf.asParser();
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken());
byte[] raw = jp.getBinaryValue();
assertEquals(16, raw.length);
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
String str = jp.getText();
assertEquals(value, str);
jp.close();
}
}
Expand Down

0 comments on commit 9077a58

Please sign in to comment.