Skip to content

Commit bb35f63

Browse files
committed
Add enum and URL support to JSONEncoder.
1 parent 52e2b0d commit bb35f63

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ Method arguments may be any of the following types:
100100
* `java.time.LocalDate` ("yyyy-mm-dd")
101101
* `java.time.LocalTime` ("hh:mm")
102102
* `java.time.LocalDateTime` ("yyyy-mm-ddThh:mm")
103-
* `java.util.List`
104103
* `java.net.URL`
104+
* `java.util.List`
105105

106106
Missing or `null` values are automatically converted to `0` or `false` for primitive types.
107107

@@ -206,8 +206,10 @@ Return values are converted to their JSON equivalents as follows:
206206
* `CharSequence`: string
207207
* `Number`: number
208208
* `Boolean`: true/false
209-
* `java.util.Date`: long value representing epoch time in milliseconds
209+
* `Enum`: string
210+
* `java.util.Date`: number representing epoch time in milliseconds
210211
* `java.time.TemporalAccessor`: string
212+
* `java.net.URL`: string
211213
* `Iterable`: array
212214
* `java.util.Map` or Java bean: object
213215

@@ -660,7 +662,10 @@ If the value is already an instance of the requested type, it is returned as is.
660662
* If the target type is a number or boolean, the value is parsed or coerced using the appropriate conversion method (e.g. `Integer#valueOf()`). Missing or `null` values are automatically converted to `0` or `false` for primitive types.
661663
* If the target type is a `String`, the value is adapted via its `toString()` method.
662664
* If the target type is `java.util.Date`, the value is parsed or coerced to a long value representing epoch time in milliseconds and then converted to a `Date`.
663-
* If the target type is `java.time.Instant`, `java.time.LocalDate`, `java.time.LocalTime`, or `java.time.LocalDateTime`, the value is converted to a string and parsed using the appropriate `parse()` method.
665+
* If the target type is `java.time.Instant` and the value is an instance of `Date`, the value is adapted via `Date#toInstant()`. Otherwise, the value's string representation is parsed using `Instant#parse(CharSequence)`.
666+
* If the target type is `java.time.LocalDate`, the value's string representation is parsed using `LocalDate#parse()`.
667+
* If the target type is `java.time.LocalTime`, the value's string representation is parsed using `LocalTime#parse()`.
668+
* If the target type is `java.time.LocalDateTime`, the value's string representation is parsed using `LocalDateTime#parse()`.
664669
* If the target type is `java.util.List` or `java.util.Map`, the value is wrapped in an adapter of the same type that automatically adapts its sub-elements.
665670

666671
Otherwise, the target is assumed to be a bean, and the value is assumed to be a map:

httprpc-client/src/main/java/org/httprpc/beans/BeanAdapter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,10 @@ private static Object adapt(Object value, Map<Class<?>, Map<String, Property>> p
443443
* representation is parsed using {@link LocalTime#parse(CharSequence)}.</li>
444444
* <li>If the target type is {@link LocalDateTime}, the value's string
445445
* representation is parsed using {@link LocalDateTime#parse(CharSequence)}.</li>
446+
* <li>If the target type is {@link List} or {@link Map}, the value is wrapped
447+
* in an adapter of the same type that automatically adapts its sub-elements.</li>
446448
* </ul>
447449
*
448-
* If the target type is a {@link List}, the value is wrapped in an adapter
449-
* that will recursively adapt the list's elements. If the target type is a
450-
* {@link Map}, the value is wrapped in an adapter that will recursively adapt
451-
* the map's values.
452-
*
453450
* Otherwise, the target is assumed to be a bean, and the value is assumed to
454451
* be a map:
455452
*

httprpc-client/src/main/java/org/httprpc/io/JSONEncoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.io.Writer;
19+
import java.net.URL;
1920
import java.nio.charset.StandardCharsets;
2021
import java.time.temporal.TemporalAccessor;
2122
import java.util.Date;
@@ -87,9 +88,11 @@ private void encode(Object value, Writer writer) throws IOException {
8788
writer.write("\"");
8889
} else if (value instanceof Number || value instanceof Boolean) {
8990
writer.write(value.toString());
91+
} else if (value instanceof Enum<?>) {
92+
encode(value.toString(), writer);
9093
} else if (value instanceof Date) {
9194
encode(((Date)value).getTime(), writer);
92-
} else if (value instanceof TemporalAccessor) {
95+
} else if (value instanceof TemporalAccessor || value instanceof URL) {
9396
encode(value.toString(), writer);
9497
} else if (value instanceof Iterable<?>) {
9598
writer.write("[");

httprpc-client/src/test/java/org/httprpc/beans/BeanAdapterTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ public void testPrimitiveCoercion() {
144144

145145
@Test
146146
public void testInstantCoercion() {
147-
Date date = new Date(1);
148-
149-
Instant instant = BeanAdapter.adapt(date, Instant.class);
150-
151-
assertEquals(Instant.ofEpochMilli(1), instant);
147+
assertEquals(Instant.ofEpochMilli(1), BeanAdapter.adapt(new Date(1), Instant.class));
152148
}
153149

154150
@Test

httprpc-client/src/test/java/org/httprpc/io/JSONEncoderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.StringWriter;
21+
import java.net.URL;
22+
import java.time.DayOfWeek;
2123
import java.time.Instant;
2224
import java.util.Date;
2325
import java.util.List;
@@ -50,6 +52,11 @@ public void testBoolean() throws IOException {
5052
assertEquals("false", encode(false));
5153
}
5254

55+
@Test
56+
public void testEnum() throws Exception {
57+
assertEquals("\"MONDAY\"", encode(DayOfWeek.MONDAY));
58+
}
59+
5360
@Test
5461
public void testDate() throws IOException {
5562
assertEquals("0", encode(new Date(0)));
@@ -60,6 +67,11 @@ public void testInstant() throws IOException {
6067
assertEquals("\"1970-01-01T00:00:00.001Z\"", encode(Instant.ofEpochMilli(1)));
6168
}
6269

70+
@Test
71+
public void testURL() throws Exception {
72+
assertEquals("\"http://localhost:8080\"", encode(new URL("http://localhost:8080")));
73+
}
74+
6375
@Test
6476
public void testArray() throws IOException {
6577
String expected = "[\n"

httprpc-server/src/main/java/org/httprpc/WebService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ private void describeType(Type type, XMLStreamWriter xmlStreamWriter) throws XML
895895
}
896896

897897
private void describeType(Class<?> type, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
898-
if (type.isArray() || Enum.class.isAssignableFrom(type)) {
898+
if (type.isArray() || type.isEnum()) {
899899
throw new IllegalArgumentException();
900900
}
901901

0 commit comments

Comments
 (0)