Implement [JACKSON-789]
diff --git a/release-notes/VERSION b/release-notes/VERSION
index 55de853..eb49238 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -17,6 +17,7 @@
(reported by Ittai Z)
* [JACKSON-779]: Problems with multi-byte UTF-8 chars in JSON comments
(reported by Alexander K)
+ * [JACKSON-789]: Add support for 'java.nio.charset.Charset'
------------------------------------------------------------------------
=== History: ===
diff --git a/src/mapper/java/org/codehaus/jackson/map/deser/std/FromStringDeserializer.java b/src/mapper/java/org/codehaus/jackson/map/deser/std/FromStringDeserializer.java
index d3a5e39..ce53dfc 100644
--- a/src/mapper/java/org/codehaus/jackson/map/deser/std/FromStringDeserializer.java
+++ b/src/mapper/java/org/codehaus/jackson/map/deser/std/FromStringDeserializer.java
@@ -4,6 +4,7 @@
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
+import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;
@@ -39,6 +40,8 @@
// 1.8:
all.add(new InetAddressDeserializer());
all.add(new TimeZoneDeserializer());
+ // 1.9
+ all.add(new CharsetDeserializer());
return all;
}
@@ -228,6 +231,20 @@
}
}
+ // [JACKSON-789] (since 1.9.5)
+ protected static class CharsetDeserializer
+ extends FromStringDeserializer<Charset>
+ {
+ public CharsetDeserializer() { super(Charset.class); }
+
+ @Override
+ protected Charset _deserialize(String value, DeserializationContext ctxt)
+ throws IOException
+ {
+ return Charset.forName(value);
+ }
+ }
+
/**
* As per [JACKSON-522], also need special handling for InetAddress...
*
diff --git a/src/mapper/java/org/codehaus/jackson/map/ser/BasicSerializerFactory.java b/src/mapper/java/org/codehaus/jackson/map/ser/BasicSerializerFactory.java
index 0ad25bc..e36e23c 100644
--- a/src/mapper/java/org/codehaus/jackson/map/ser/BasicSerializerFactory.java
+++ b/src/mapper/java/org/codehaus/jackson/map/ser/BasicSerializerFactory.java
@@ -295,6 +295,10 @@
if (TimeZone.class.isAssignableFrom(raw)) {
return TimeZoneSerializer.instance;
}
+ // and yet one more [JACKSON-789]
+ if (java.nio.charset.Charset.class.isAssignableFrom(raw)) {
+ return ToStringSerializer.instance;
+ }
// Then check for optional/external serializers [JACKSON-386]
JsonSerializer<?> ser = optionalHandlers.findSerializer(config, type);
diff --git a/src/test/org/codehaus/jackson/map/deser/TestJdkTypes.java b/src/test/org/codehaus/jackson/map/deser/TestJdkTypes.java
index c91421b..447a377 100644
--- a/src/test/org/codehaus/jackson/map/deser/TestJdkTypes.java
+++ b/src/test/org/codehaus/jackson/map/deser/TestJdkTypes.java
@@ -2,6 +2,7 @@
import java.io.*;
import java.net.*;
+import java.nio.charset.Charset;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
@@ -317,4 +318,11 @@
assertTrue(b.primitive);
assertEquals(Boolean.TRUE, b.ctor);
}
+
+ // [JACKSON-789]
+ public void testCharset() throws Exception
+ {
+ Charset UTF8 = Charset.forName("UTF-8");
+ assertSame(UTF8, mapper.readValue(quote("UTF-8"), Charset.class));
+ }
}
diff --git a/src/test/org/codehaus/jackson/map/ser/TestJdkTypes.java b/src/test/org/codehaus/jackson/map/ser/TestJdkTypes.java
index ad70631..4739fc5 100644
--- a/src/test/org/codehaus/jackson/map/ser/TestJdkTypes.java
+++ b/src/test/org/codehaus/jackson/map/ser/TestJdkTypes.java
@@ -2,6 +2,7 @@
import java.io.*;
import java.net.InetAddress;
+import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;
@@ -70,4 +71,9 @@
assertEquals(quote("void"), mapper.writeValueAsString(Void.TYPE));
}
+ // [JACKSON-789]
+ public void testCharset() throws IOException {
+ ObjectMapper mapper = new ObjectMapper();
+ assertEquals(quote("UTF-8"), mapper.writeValueAsString(Charset.forName("UTF-8")));
+ }
}