Skip to content

Commit 2ebaaf1

Browse files
committed
Minor refactoring wrt java.nio.file.Path handling
1 parent 3f933e8 commit 2ebaaf1

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
268268
writeStringLikeField(fieldName, value.toString(), type);
269269
return;
270270
case SER_PATH:
271-
writeStringLikeField(fieldName, ((Path) value).toUri().toString(), type);
271+
writeStringLikeField(fieldName, pathToString((Path) value), type);
272272
return;
273273

274274
// Others
@@ -398,7 +398,7 @@ protected void _writeValue(Object value, int type) throws IOException
398398
writeStringLikeValue(value.toString(), type);
399399
return;
400400
case SER_PATH:
401-
writeStringLikeValue(((Path) value).toUri().toString(), type);
401+
writeStringLikeValue(pathToString((Path) value), type);
402402
return;
403403

404404
case SER_ITERABLE:
@@ -785,6 +785,13 @@ protected String dateToString(Date v) {
785785
return v.toString();
786786
}
787787

788+
/**
789+
* @since 2.17
790+
*/
791+
protected String pathToString(Path value) {
792+
return value.toUri().toString();
793+
}
794+
788795
/*
789796
/**********************************************************************
790797
/* Other internal methods

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

Lines changed: 15 additions & 9 deletions
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.Path;
910
import java.nio.file.Paths;
1011
import java.util.*;
1112

@@ -241,15 +242,7 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
241242
}
242243
return URI.create(p.getValueAsString());
243244
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-
}
245+
return _readPath(p);
253246

254247
// case SER_MAP:
255248
// case SER_LIST:
@@ -278,6 +271,19 @@ protected byte[] _readBinary(JsonParser p) throws IOException {
278271
return p.getBinaryValue();
279272
}
280273

274+
// @since 2.17
275+
protected Path _readPath(JsonParser p) throws IOException {
276+
if (p.hasToken(JsonToken.VALUE_NULL)) {
277+
return null;
278+
}
279+
String v = p.getValueAsString();
280+
try {
281+
return Paths.get(new URI(v));
282+
} catch (Exception e) {
283+
throw new JSONObjectException("Failed to bind `java.nio.file.Path` from value '"+v+"'");
284+
}
285+
}
286+
281287
protected int[] _readIntArray(JsonParser p) throws IOException
282288
{
283289
// !!! TODO

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ public void testMiscUriTypes() throws Exception
186186
assertEquals(url, JSON.std.beanFrom(URL.class, q(URL_STR)));
187187

188188
Path p = Paths.get(new URI("file:///foo/bar.txt"));
189+
String json = JSON.std.asString(p);
189190
assertEquals(p,
190-
JSON.std.beanFrom(Path.class, q("file:///foo/bar.txt")));
191+
JSON.std.beanFrom(Path.class, json));
191192
}
192193

193194
public void testMiscScalarFail() throws Exception {

0 commit comments

Comments
 (0)