Skip to content

Commit 9077a58

Browse files
committed
Fix #362
1 parent c4a0032 commit 9077a58

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Version: 2.3.1 (xx-Dec-2013)
66
(reported by gaff78@github)
77
#358: `IterableSerializer` ignoring annotated content serializer
88
(reported by Florian S)
9+
#362: UUID output as Base64 String with ObjectMapper.convertValue()
10+
(reported by jknack@github)
911

1012
------------------------------------------------------------------------
1113
=== History: ===

src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.JsonGenerationException;
77
import com.fasterxml.jackson.core.JsonGenerator;
88
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.util.TokenBuffer;
910

1011
/**
1112
* Specialized {@link JsonSerializer} to output {@link java.util.UUID}s.
@@ -41,8 +42,15 @@ public void serialize(UUID value, JsonGenerator jgen, SerializerProvider provide
4142
{
4243
// First: perhaps we could serialize it as raw binary data?
4344
if (jgen.canWriteBinaryNatively()) {
44-
jgen.writeBinary(_asBytes(value));
45-
return;
45+
/* 07-Dec-2013, tatu: One nasty case; that of TokenBuffer. While it can
46+
* technically retain binary data, we do not want to do use binary
47+
* with it, as that results in UUIDs getting converted to Base64 for
48+
* most conversions.
49+
*/
50+
if (!(jgen instanceof TokenBuffer)) {
51+
jgen.writeBinary(_asBytes(value));
52+
return;
53+
}
4654
}
4755

4856
// UUID.toString() works ok functionally, but we can make it go much faster

src/test/java/com/fasterxml/jackson/databind/ser/TestJdkTypes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class TestJdkTypes
1919
extends com.fasterxml.jackson.databind.BaseMapTest
2020
{
21-
private final ObjectMapper MAPPER = new ObjectMapper();
21+
private final ObjectMapper MAPPER = objectMapper();
2222

2323
/**
2424
* Unit test to catch bug [JACKSON-8].
@@ -144,6 +144,10 @@ public void testUUIDs() throws IOException
144144
UUID uuid = UUID.fromString(value);
145145
String json = MAPPER.writeValueAsString(uuid);
146146
assertEquals(quote(uuid.toString()), json);
147+
148+
// Also, wrt [#362], should convert cleanly
149+
String str = MAPPER.convertValue(uuid, String.class);
150+
assertEquals(value, str);
147151
}
148152

149153
// then use templating; note that these are not exactly valid UUIDs

src/test/java/com/fasterxml/jackson/databind/util/TestTokenBuffer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ public void testWithUUID() throws IOException
237237
UUID out = mapper.readValue(buf.asParser(), UUID.class);
238238
assertEquals(uuid.toString(), out.toString());
239239

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

0 commit comments

Comments
 (0)