Skip to content

Commit b94a732

Browse files
committed
Fix #2451
1 parent ac7c0ce commit b94a732

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Project: jackson-databind
1515
(reported, fix contributed by Hesham M)
1616
#2446: Java 11: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
1717
(reported by David C)
18+
#2451: Add new `JsonValueFormat` value, `UUID`
1819
1920
2.10.0.pr2 (31-Aug-2019)
2021

src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonValueFormat.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public enum JsonValueFormat
7474
TIME("time"),
7575

7676
/**
77-
* This value SHOULD be a URI..
77+
* This value SHOULD be a URI.
7878
*/
7979
URI("uri"),
8080

@@ -85,6 +85,13 @@ public enum JsonValueFormat
8585
float).
8686
*/
8787
UTC_MILLISEC("utc-millisec"),
88+
89+
/**
90+
* Value should be valid <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUID</a>
91+
*
92+
* @since 2.10
93+
*/
94+
UUID("uuid")
8895
;
8996

9097
private final String _desc;

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import com.fasterxml.jackson.core.JsonGenerator;
77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
9+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
810
import com.fasterxml.jackson.databind.util.TokenBuffer;
911

1012
/**
@@ -39,11 +41,10 @@ public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider
3941
{
4042
// First: perhaps we could serialize it as raw binary data?
4143
if (gen.canWriteBinaryNatively()) {
42-
/* 07-Dec-2013, tatu: One nasty case; that of TokenBuffer. While it can
43-
* technically retain binary data, we do not want to do use binary
44-
* with it, as that results in UUIDs getting converted to Base64 for
45-
* most conversions.
46-
*/
44+
// 07-Dec-2013, tatu: One nasty case; that of TokenBuffer. While it can
45+
// technically retain binary data, we do not want to do use binary
46+
// with it, as that results in UUIDs getting converted to Base64 for
47+
// most conversions.
4748
if (!(gen instanceof TokenBuffer)) {
4849
gen.writeBinary(_asBytes(value));
4950
return;
@@ -72,12 +73,20 @@ public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider
7273
gen.writeString(ch, 0, 36);
7374
}
7475

76+
// Need to add bit of extra info, format
77+
@Override
78+
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
79+
throws JsonMappingException
80+
{
81+
visitStringFormat(visitor, typeHint, JsonValueFormat.UUID);
82+
}
83+
7584
private static void _appendInt(int bits, char[] ch, int offset)
7685
{
7786
_appendShort(bits >> 16, ch, offset);
7887
_appendShort(bits, ch, offset+4);
7988
}
80-
89+
8190
private static void _appendShort(int bits, char[] ch, int offset)
8291
{
8392
ch[offset] = HEX_CHARS[(bits >> 12) & 0xF];

src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public JsonMapFormatVisitor expectMapFormat(JavaType type) {
202202
/* Test methods
203203
/**********************************************************
204204
*/
205-
206-
private final ObjectMapper MAPPER = new ObjectMapper();
205+
206+
private final ObjectMapper MAPPER = newJsonMapper();
207207

208208
/* Silly little test for simply triggering traversal, without attempting to
209209
* verify what is being reported. Smoke test that should trigger problems
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fasterxml.jackson.databind.jsonschema;
2+
3+
import java.util.Set;
4+
import java.util.UUID;
5+
import java.util.concurrent.atomic.AtomicReference;
6+
7+
import com.fasterxml.jackson.databind.*;
8+
9+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
10+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
11+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
12+
13+
public class SchemaWithUUIDTest extends BaseMapTest
14+
{
15+
private final ObjectMapper MAPPER = newJsonMapper();
16+
17+
public void testUUIDSchema() throws Exception
18+
{
19+
final AtomicReference<JsonValueFormat> format = new AtomicReference<>();
20+
21+
MAPPER.acceptJsonFormatVisitor(UUID.class, new JsonFormatVisitorWrapper.Base() {
22+
@Override
23+
public JsonStringFormatVisitor expectStringFormat(JavaType type) {
24+
return new JsonStringFormatVisitor() {
25+
@Override
26+
public void enumTypes(Set<String> enums) { }
27+
28+
@Override
29+
public void format(JsonValueFormat f) {
30+
format.set(f);
31+
}
32+
};
33+
}
34+
});
35+
assertEquals(JsonValueFormat.UUID, format.get());
36+
}
37+
}

0 commit comments

Comments
 (0)