Skip to content

Commit 11d0611

Browse files
authored
Merge pull request #117 from FasterXML/tatu/2.17/116-support-path-type
Fix #116: add read/write support for `java.nio.file.Path`
2 parents 552a543 + e598afa commit 11d0611

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/JSONWriter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.math.BigDecimal;
55
import java.math.BigInteger;
6+
import java.nio.file.Path;
67
import java.util.*;
78

89
import com.fasterxml.jackson.core.*;
@@ -266,6 +267,9 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
266267
case SER_URI:
267268
writeStringLikeField(fieldName, value.toString(), type);
268269
return;
270+
case SER_PATH:
271+
writeStringLikeField(fieldName, ((Path) value).toUri().toString(), type);
272+
return;
269273

270274
// Others
271275

@@ -393,6 +397,9 @@ protected void _writeValue(Object value, int type) throws IOException
393397
case SER_URI:
394398
writeStringLikeValue(value.toString(), type);
395399
return;
400+
case SER_PATH:
401+
writeStringLikeValue(((Path) value).toUri().toString(), type);
402+
return;
396403

397404
case SER_ITERABLE:
398405
writeIterableValue((Iterable<?>) value);

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.net.URI;
88
import java.net.URL;
9+
import java.nio.file.Paths;
910
import java.util.*;
1011

1112
import com.fasterxml.jackson.core.JsonParser;
@@ -212,7 +213,7 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
212213
try {
213214
return Class.forName(v);
214215
} catch (Exception e) {
215-
throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
216+
throw new JSONObjectException("Failed to bind `java.lang.Class` from value '"+v+"'");
216217
}
217218
}
218219
case SER_FILE:
@@ -239,6 +240,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
239240
return null;
240241
}
241242
return URI.create(p.getValueAsString());
243+
case SER_PATH:
244+
if (p.hasToken(JsonToken.VALUE_NULL)) {
245+
return null;
246+
}
247+
String v = p.getValueAsString();
248+
try {
249+
return Paths.get(new URI(v));
250+
} catch (Exception e) {
251+
throw new JSONObjectException("Failed to bind `java.nio.file.Path` from value '"+v+"'");
252+
}
242253

243254
// case SER_MAP:
244255
// case SER_LIST:

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueLocatorBase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.math.BigInteger;
66
import java.net.URI;
77
import java.net.URL;
8+
import java.nio.file.Path;
89
import java.util.*;
910

1011
import com.fasterxml.jackson.core.TreeNode;
@@ -111,15 +112,15 @@ public abstract class ValueLocatorBase
111112
public final static int SER_UUID = 33;
112113
public final static int SER_URL = 34;
113114
public final static int SER_URI = 35;
114-
115+
public final static int SER_PATH = 36; // since 2.17
115116

116117
// // // Iterate-able types
117118

118119
/**
119120
* Anything that implements {@link java.lang.Iterable}, but not
120121
* {@link java.util.Collection}.
121122
*/
122-
public final static int SER_ITERABLE = 36;
123+
public final static int SER_ITERABLE = 37;
123124

124125
/*
125126
/**********************************************************************
@@ -236,6 +237,9 @@ protected int _findSimpleType(Class<?> raw, boolean forSer)
236237
if (UUID.class.isAssignableFrom(raw)) {
237238
return SER_UUID;
238239
}
240+
if (Path.class.isAssignableFrom(raw)) {
241+
return SER_PATH;
242+
}
239243
// May or may not help with deser, but recognized nonetheless;
240244
// on assumption that Beans should rarely implement `CharSequence`
241245
if (CharSequence.class.isAssignableFrom(raw)) {

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/ReadSimpleTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.File;
66
import java.net.URI;
77
import java.net.URL;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
810
import java.util.*;
911

1012
import com.fasterxml.jackson.core.JsonParser;
@@ -177,6 +179,17 @@ public void testMiscScalars() throws Exception {
177179
assertEquals(Object.class, JSON.std.beanFrom(Class.class, q(Object.class.getName())));
178180
}
179181

182+
public void testMiscUriTypes() throws Exception
183+
{
184+
final String URL_STR = "http://fasterxml.com";
185+
final URL url = new URL(URL_STR);
186+
assertEquals(url, JSON.std.beanFrom(URL.class, q(URL_STR)));
187+
188+
Path p = Paths.get(new URI("file:///foo/bar.txt"));
189+
assertEquals(p,
190+
JSON.std.beanFrom(Path.class, q("file:///foo/bar.txt")));
191+
}
192+
180193
public void testMiscScalarFail() throws Exception {
181194
for (String input : new String[] { " false ", "true", "[ ]", "{ }" } ) {
182195
try {

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/WriteSimpleTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.StringWriter;
66
import java.net.URI;
77
import java.net.URL;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
810
import java.util.*;
911

1012
import com.fasterxml.jackson.jr.ob.JSON.Feature;
@@ -103,12 +105,15 @@ public void testNest() throws Exception
103105
public void testKnownSimpleTypes() throws Exception
104106
{
105107
final String URL_STR = "http://fasterxml.com";
106-
assertEquals(q(URL_STR),
107-
JSON.std.asString(new URI(URL_STR)));
108+
final URI uri = new URI(URL_STR);
109+
assertEquals(q(URL_STR), JSON.std.asString(uri));
108110
final String PATH = "/foo/bar.txt";
109111
assertEquals(q(PATH),
110112
JSON.std.asString(new File(PATH)));
111113

114+
Path p = Paths.get(new URI("file:///foo/bar.txt"));
115+
assertEquals(q("file:///foo/bar.txt"), JSON.std.asString(p));
116+
112117
assertEquals(q("B"), JSON.std.asString(ABC.B));
113118
assertEquals("1", JSON.std.with(Feature.WRITE_ENUMS_USING_INDEX).asString(ABC.B));
114119
}

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Modules:
1818
#112: `overrideStandardValueWriter` only applied to first `java.nio.file.Path`
1919
valued field of bean
2020
(reported by Julian H)
21+
#116: Add read/write support for `java.nio.file.Path`
2122

2223
2.16.1 (24-Dec-2023)
2324

0 commit comments

Comments
 (0)