JUNEAU-127 Clean up usage of PojoSwaps in ClassMeta
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripMapsTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripMapsTest.java
index d447881..a6f753b 100755
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripMapsTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripMapsTest.java
@@ -40,7 +40,7 @@
@Override /* RoundTripTest */
public Class<?>[] getPojoSwaps() {
return new Class<?>[]{
- ByteArrayBase64Swap.class,
+ ByteArraySwap.Base64.class,
DateSwap.ISO8601DTZ.class,
CalendarLongSwap.class,
};
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripNumericConstructorsTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripNumericConstructorsTest.java
deleted file mode 100644
index 5a1c988..0000000
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripNumericConstructorsTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.juneau.a.rttests;
-
-import static org.junit.Assert.*;
-
-import java.util.*;
-
-import org.apache.juneau.parser.*;
-import org.apache.juneau.serializer.*;
-import org.junit.*;
-
-/**
- * Tests designed to serialize and parse objects to make sure we end up
- * with the same objects for all serializers and parsers.
- */
-@SuppressWarnings({"deprecation"})
-public class RoundTripNumericConstructorsTest extends RoundTripTest {
-
- public RoundTripNumericConstructorsTest(String label, SerializerBuilder s, ParserBuilder p, int flags) throws Exception {
- super(label, s, p, flags);
- }
-
- //====================================================================================================
- // Test parsing numbers to dates.
- //====================================================================================================
- @Test
- public void testParseNumberToDate() throws Exception {
- if (isValidationOnly())
- return;
-
- Serializer s = getSerializer();
- Parser p = getParser();
- Date d = new Date(100, 1, 1);
-
- Object r = s.serialize(d.getTime());
- Date d2 = p.parse(r, Date.class);
- assertEquals(d.getTime(), d2.getTime());
- }
-}
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java
index 7101204..822192a 100755
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/a/rttests/RoundTripTransformBeansTest.java
@@ -49,7 +49,7 @@
@Test
public void testSwapBeans1() throws Exception {
Class<?>[] f = {
- ByteArrayBase64Swap.class,
+ ByteArraySwap.Base64.class,
TemporalCalendarSwap.IsoInstant.class,
TemporalDateSwap.IsoInstant.class
};
@@ -167,7 +167,7 @@
@Test
public void testSwapBeans2() throws Exception {
Class<?>[] f = {
- ByteArrayBase64Swap.class,
+ ByteArraySwap.Base64.class,
TemporalCalendarSwap.IsoInstant.class,
TemporalDateSwap.IsoInstant.class,
};
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/BeanMapTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/BeanMapTest.java
index 72fb861..3937383 100755
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/BeanMapTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/BeanMapTest.java
@@ -25,7 +25,7 @@
//====================================================================================================
@Test
public void testFilteredEntry() throws Exception {
- BeanSession session = BeanContext.create().pojoSwaps(ByteArrayBase64Swap.class).build().createSession();
+ BeanSession session = BeanContext.create().pojoSwaps(ByteArraySwap.Base64.class).build().createSession();
BeanMap<A> m = session.toBeanMap(new A());
assertEquals("AQID", m.get("f1"));
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapComboTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapComboTest.java
index 9d23435..200fd9a 100644
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapComboTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapComboTest.java
@@ -428,12 +428,12 @@
@Override
protected Serializer applySettings(Serializer s) throws Exception {
- return s.builder().pojoSwaps(ByteArrayBase64Swap.class).trimNullProperties(false).build();
+ return s.builder().pojoSwaps(ByteArraySwap.Base64.class).trimNullProperties(false).build();
}
@Override
protected Parser applySettings(Parser p) throws Exception {
- return p.builder().pojoSwaps(ByteArrayBase64Swap.class).build();
+ return p.builder().pojoSwaps(ByteArraySwap.Base64.class).build();
}
public static class BeanWithByteArrayField {
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
index f15511b..5e9ea61 100644
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
@@ -29,7 +29,7 @@
//------------------------------------------------------------------------------------------------------------------
private static BeanSession BS = BeanContext.DEFAULT.createBeanSession();
- private static InputStreamBase64Swap SWAP = new InputStreamBase64Swap();
+ private static InputStreamSwap SWAP = new InputStreamSwap.Base64();
public InputStreamBase64SwapTest(String label, InputStream o, StringSwap<InputStream> s, String r, BeanSession bs) throws Exception {
super(label, o, s, r, bs);
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index ff9e58d..edbf7ba 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -616,10 +616,6 @@
);
}
- PojoSwap defaultSwap = DefaultTransforms.findDefaultSwap(c);
- if (defaultSwap != null)
- this.pojoSwaps.add(defaultSwap);
-
if (pojoSwaps != null)
this.pojoSwaps.addAll(Arrays.asList(pojoSwaps));
@@ -778,6 +774,9 @@
if (swaps != null)
for (Swap s : swaps.value())
l.add(createPojoSwap(s));
+ PojoSwap defaultSwap = DefaultTransforms.findDefaultSwap(innerClass);
+ if (defaultSwap != null)
+ l.add(defaultSwap);
}
private PojoSwap<T,?> createPojoSwap(Swap s) {
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultTransforms.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultTransforms.java
index 445e12e..e7050a8 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultTransforms.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultTransforms.java
@@ -12,6 +12,8 @@
// ***************************************************************************************************************************
package org.apache.juneau.transform;
+import java.time.*;
+import java.time.temporal.*;
import java.util.*;
import java.util.concurrent.*;
@@ -27,15 +29,23 @@
private static final Map<Class<?>,PojoSwap<?,?>> POJO_SWAPS = new ConcurrentHashMap<>();
static {
-// POJO_SWAPS.put(byte[].class, new ByteArrayBase64Swap());
POJO_SWAPS.put(Enumeration.class, new EnumerationSwap());
-// POJO_SWAPS.put(InputStream.class, new InputStreamBase64Swap());
POJO_SWAPS.put(Iterator.class, new IteratorSwap());
POJO_SWAPS.put(Locale.class, new LocaleSwap());
-// POJO_SWAPS.put(Reader.class, new ReaderSwap());
-// POJO_SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoInstant());
-// POJO_SWAPS.put(Date.class, new TemporalCalendarSwap.IsoInstant());
-// POJO_SWAPS.put(Temporal.class, new TemporalCalendarSwap.IsoInstant());
+ POJO_SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoInstant());
+ POJO_SWAPS.put(Date.class, new TemporalCalendarSwap.IsoLocalDateTime());
+ POJO_SWAPS.put(Instant.class, new TemporalSwap.IsoInstant());
+ POJO_SWAPS.put(ZonedDateTime.class, new TemporalSwap.IsoDateTime());
+ POJO_SWAPS.put(LocalDate.class, new TemporalSwap.IsoLocalDate());
+ POJO_SWAPS.put(LocalDateTime.class, new TemporalSwap.IsoLocalDateTime());
+ POJO_SWAPS.put(LocalTime.class, new TemporalSwap.IsoLocalTime());
+ POJO_SWAPS.put(OffsetDateTime.class, new TemporalSwap.IsoOffsetDateTime());
+ POJO_SWAPS.put(OffsetTime.class, new TemporalSwap.IsoOffsetTime());
+ POJO_SWAPS.put(LocalDateTime.class, new TemporalSwap.IsoLocalDateTime());
+ POJO_SWAPS.put(LocalTime.class, new TemporalSwap.IsoLocalTime());
+ POJO_SWAPS.put(Year.class, new TemporalSwap.IsoYear());
+ POJO_SWAPS.put(YearMonth.class, new TemporalSwap.IsoYearMonth());
+ POJO_SWAPS.put(Temporal.class, new TemporalSwap.IsoInstant());
POJO_SWAPS.put(TimeZone.class, new TimeZoneSwap());
POJO_SWAPS.put(XMLGregorianCalendar.class, new XMLGregorianCalendarSwap());
}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArrayBase64Swap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArrayBase64Swap.java
index bff57ad..a9d3ac1 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArrayBase64Swap.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArrayBase64Swap.java
@@ -19,7 +19,9 @@
/**
* Transforms <code><jk>byte</jk>[]</code> arrays to BASE-64 encoded {@link String Strings}.
+ * @deprecated Use {@link ByteArraySwap}
*/
+@Deprecated
public class ByteArrayBase64Swap extends StringSwap<byte[]> {
/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArraySwap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArraySwap.java
new file mode 100644
index 0000000..135fc26
--- /dev/null
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ByteArraySwap.java
@@ -0,0 +1,87 @@
+// ***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import static org.apache.juneau.internal.StringUtils.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.transform.*;
+
+/**
+ * Transforms <code><jk>byte</jk>[]</code> arrays to Strings.
+ */
+public abstract class ByteArraySwap extends StringSwap<byte[]> {
+
+ /**
+ * Converts byte arrays to BASE-64 encoding.
+ */
+ public static class Base64 extends ByteArraySwap {
+ /**
+ * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, byte[] b) throws Exception {
+ return base64Encode(b);
+ }
+
+ /**
+ * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>.
+ */
+ @Override /* PojoSwap */
+ public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return base64Decode(s);
+ }
+ }
+
+ /**
+ * Converts byte arrays to hex encoding.
+ */
+ public static class Hex extends ByteArraySwap {
+ /**
+ * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, byte[] b) throws Exception {
+ return toHex(b);
+ }
+
+ /**
+ * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>.
+ */
+ @Override /* PojoSwap */
+ public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return fromHex(s);
+ }
+ }
+
+ /**
+ * Converts byte arrays to spaced-hex encoding.
+ */
+ public static class SpacedHex extends ByteArraySwap {
+ /**
+ * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, byte[] b) throws Exception {
+ return toSpacedHex(b);
+ }
+
+ /**
+ * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>.
+ */
+ @Override /* PojoSwap */
+ public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return fromSpacedHex(s);
+ }
+ }
+}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamBase64Swap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamBase64Swap.java
deleted file mode 100644
index 39c8444..0000000
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamBase64Swap.java
+++ /dev/null
@@ -1,35 +0,0 @@
-// ***************************************************************************************************************************
-// * 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.juneau.transforms;
-
-import static org.apache.juneau.internal.StringUtils.*;
-
-import java.io.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.internal.*;
-import org.apache.juneau.transform.*;
-
-/**
- * Transforms <code>InputStreams</code> to BASE-64 encoded {@link String Strings}.
- */
-public class InputStreamBase64Swap extends StringSwap<InputStream> {
-
- /**
- * Converts the specified {@link InputStream} to a {@link String}.
- */
- @Override /* PojoSwap */
- public String swap(BeanSession session, InputStream is) throws Exception {
- return base64Encode(IOUtils.readBytes(is));
- }
-}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamSwap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamSwap.java
new file mode 100644
index 0000000..f4f5b22
--- /dev/null
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/InputStreamSwap.java
@@ -0,0 +1,116 @@
+// ***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import static org.apache.juneau.internal.StringUtils.*;
+
+import java.io.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.internal.*;
+import org.apache.juneau.transform.*;
+
+/**
+ * Transforms <code>InputStreams</code> to {@link String Strings}.
+ */
+public abstract class InputStreamSwap extends StringSwap<InputStream> {
+
+ /**
+ * Converts {@link InputStream InputStreams} to BASE-64 encoding.
+ */
+ public static class Base64 extends InputStreamSwap {
+ /**
+ * Converts the specified {@link InputStream} to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, InputStream is) throws Exception {
+ return base64Encode(toBytes(is));
+ }
+
+ /**
+ * Converts the specified {@link String} to an {@link InputStream}.
+ */
+ @Override /* PojoSwap */
+ public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return toStream(base64Decode(s), hint);
+ }
+ }
+
+ /**
+ * Converts {@link InputStream InputStreams} to hex encoding.
+ */
+ public static class Hex extends InputStreamSwap {
+ /**
+ * Converts the specified {@link InputStream} to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, InputStream is) throws Exception {
+ return toHex(toBytes(is));
+ }
+
+ /**
+ * Converts the specified {@link String} to an {@link InputStream}.
+ */
+ @Override /* PojoSwap */
+ public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return toStream(fromHex(s), hint);
+ }
+ }
+
+ /**
+ * Converts {@link InputStream InputStreams} to spaced-hex encoding.
+ */
+ public static class SpacedHex extends InputStreamSwap {
+ /**
+ * Converts the specified {@link InputStream} to a {@link String}.
+ */
+ @Override /* PojoSwap */
+ public String swap(BeanSession session, InputStream is) throws Exception {
+ return toSpacedHex(toBytes(is));
+ }
+
+ /**
+ * Converts the specified {@link String} to an {@link InputStream}.
+ */
+ @Override /* PojoSwap */
+ public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
+ return toStream(fromSpacedHex(s), hint);
+ }
+ }
+
+ /**
+ * Convert the specified input stream to a byte array.
+ *
+ * @param is The input stream to convert to bytes.
+ * @return The byte array.
+ * @throws IOException Thrown by input stream.
+ */
+ protected byte[] toBytes(InputStream is) throws IOException {
+ return IOUtils.readBytes(is);
+ }
+
+
+ /**
+ * Convert the specified byte array into an input stream.
+ *
+ * @param b The byte array.
+ * @param hint Contains a hint about what subtype is being requested.
+ * @return The byte array.
+ */
+ protected InputStream toStream(byte[] b, ClassMeta<?> hint) {
+ Class<?> c = hint == null ? InputStream.class : hint.getInnerClass();
+ if (c == InputStream.class || c == ByteArrayInputStream.class)
+ return new ByteArrayInputStream(b);
+ return null;
+ }
+}