This is an automated email from the ASF dual-hosted git repository.
jungm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push:
new a94c3d01 JOHNZON-412 - Default mapping for java.util.UUID
a94c3d01 is described below
commit a94c3d018bf4207e12c2decb918721ae14314a77
Author: Markus Jung <[email protected]>
AuthorDate: Sun Apr 6 11:03:46 2025 +0200
JOHNZON-412 - Default mapping for java.util.UUID
---
.../johnzon/mapper/converter/UUIDConverter.java | 35 ++++++++++++++++
.../johnzon/mapper/map/LazyConverterMap.java | 5 +++
.../mapper/converter/UUIDConverterTest.java | 47 ++++++++++++++++++++++
3 files changed, 87 insertions(+)
diff --git
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java
new file mode 100644
index 00000000..c6a724a6
--- /dev/null
+++
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/converter/UUIDConverter.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.mapper.converter;
+
+import org.apache.johnzon.mapper.Converter;
+
+import java.util.UUID;
+
+public class UUIDConverter implements Converter<UUID> {
+ @Override
+ public String toString(UUID instance) {
+ return instance.toString();
+ }
+
+ @Override
+ public UUID fromString(String text) {
+ return UUID.fromString(text);
+ }
+}
diff --git
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java
index 22854c7f..cd1b1699 100644
---
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java
+++
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/map/LazyConverterMap.java
@@ -29,6 +29,7 @@ import org.apache.johnzon.mapper.converter.LocaleConverter;
import org.apache.johnzon.mapper.converter.StringConverter;
import org.apache.johnzon.mapper.converter.URIConverter;
import org.apache.johnzon.mapper.converter.URLConverter;
+import org.apache.johnzon.mapper.converter.UUIDConverter;
import org.apache.johnzon.mapper.internal.AdapterKey;
import org.apache.johnzon.mapper.internal.ConverterAdapter;
import org.apache.johnzon.mapper.util.DateUtil;
@@ -58,6 +59,7 @@ import java.util.Locale;
import java.util.Set;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
+import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
@@ -298,6 +300,9 @@ public class LazyConverterMap extends
ConcurrentHashMap<AdapterKey, Adapter<?, ?
}
}, OffsetTime.class));
}
+ if (from == UUID.class) {
+ return add(key, new ConverterAdapter<>(new UUIDConverter(),
UUID.class));
+ }
return null;
}
diff --git
a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java
new file mode 100644
index 00000000..393895cf
--- /dev/null
+++
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/UUIDConverterTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.mapper.converter;
+
+import org.apache.johnzon.mapper.Mapper;
+import org.apache.johnzon.mapper.MapperBuilder;
+import org.junit.jupiter.api.Test;
+
+import java.util.UUID;
+
+import static org.junit.Assert.assertEquals;
+
+public class UUIDConverterTest {
+ private static final UUID THE_UUID = UUID.randomUUID();
+
+ @Test
+ public void serialize() {
+ Mapper mapper = new MapperBuilder().build();
+ String json = mapper.writeObjectAsString(THE_UUID);
+
+ assertEquals("\"" + THE_UUID + "\"", json);
+ }
+
+ @Test
+ public void deserialize() {
+ Mapper mapper = new MapperBuilder().build();
+ UUID uuid = mapper.readObject("\"" + THE_UUID + "\"", UUID.class);
+
+ assertEquals(THE_UUID, uuid);
+ }
+}