Skip to content

Commit

Permalink
Fix #116
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 17, 2024
1 parent 552a543 commit e598afa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Path;
import java.util.*;

import com.fasterxml.jackson.core.*;
Expand Down Expand Up @@ -266,6 +267,9 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
case SER_URI:
writeStringLikeField(fieldName, value.toString(), type);
return;
case SER_PATH:
writeStringLikeField(fieldName, ((Path) value).toUri().toString(), type);
return;

// Others

Expand Down Expand Up @@ -393,6 +397,9 @@ protected void _writeValue(Object value, int type) throws IOException
case SER_URI:
writeStringLikeValue(value.toString(), type);
return;
case SER_PATH:
writeStringLikeValue(((Path) value).toUri().toString(), type);
return;

case SER_ITERABLE:
writeIterableValue((Iterable<?>) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.*;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -212,7 +213,7 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
try {
return Class.forName(v);
} catch (Exception e) {
throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
throw new JSONObjectException("Failed to bind `java.lang.Class` from value '"+v+"'");
}
}
case SER_FILE:
Expand All @@ -239,6 +240,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
return null;
}
return URI.create(p.getValueAsString());
case SER_PATH:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
String v = p.getValueAsString();
try {
return Paths.get(new URI(v));
} catch (Exception e) {
throw new JSONObjectException("Failed to bind `java.nio.file.Path` from value '"+v+"'");
}

// case SER_MAP:
// case SER_LIST:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.*;

import com.fasterxml.jackson.core.TreeNode;
Expand Down Expand Up @@ -111,15 +112,15 @@ public abstract class ValueLocatorBase
public final static int SER_UUID = 33;
public final static int SER_URL = 34;
public final static int SER_URI = 35;

public final static int SER_PATH = 36; // since 2.17

// // // Iterate-able types

/**
* Anything that implements {@link java.lang.Iterable}, but not
* {@link java.util.Collection}.
*/
public final static int SER_ITERABLE = 36;
public final static int SER_ITERABLE = 37;

/*
/**********************************************************************
Expand Down Expand Up @@ -236,6 +237,9 @@ protected int _findSimpleType(Class<?> raw, boolean forSer)
if (UUID.class.isAssignableFrom(raw)) {
return SER_UUID;
}
if (Path.class.isAssignableFrom(raw)) {
return SER_PATH;
}
// May or may not help with deser, but recognized nonetheless;
// on assumption that Beans should rarely implement `CharSequence`
if (CharSequence.class.isAssignableFrom(raw)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

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

public void testMiscUriTypes() throws Exception
{
final String URL_STR = "http://fasterxml.com";
final URL url = new URL(URL_STR);
assertEquals(url, JSON.std.beanFrom(URL.class, q(URL_STR)));

Path p = Paths.get(new URI("file:///foo/bar.txt"));
assertEquals(p,
JSON.std.beanFrom(Path.class, q("file:///foo/bar.txt")));
}

public void testMiscScalarFail() throws Exception {
for (String input : new String[] { " false ", "true", "[ ]", "{ }" } ) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.StringWriter;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import com.fasterxml.jackson.jr.ob.JSON.Feature;
Expand Down Expand Up @@ -103,12 +105,15 @@ public void testNest() throws Exception
public void testKnownSimpleTypes() throws Exception
{
final String URL_STR = "http://fasterxml.com";
assertEquals(q(URL_STR),
JSON.std.asString(new URI(URL_STR)));
final URI uri = new URI(URL_STR);
assertEquals(q(URL_STR), JSON.std.asString(uri));
final String PATH = "/foo/bar.txt";
assertEquals(q(PATH),
JSON.std.asString(new File(PATH)));

Path p = Paths.get(new URI("file:///foo/bar.txt"));
assertEquals(q("file:///foo/bar.txt"), JSON.std.asString(p));

assertEquals(q("B"), JSON.std.asString(ABC.B));
assertEquals("1", JSON.std.with(Feature.WRITE_ENUMS_USING_INDEX).asString(ABC.B));
}
Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Modules:
#112: `overrideStandardValueWriter` only applied to first `java.nio.file.Path`
valued field of bean
(reported by Julian H)
#116: Add read/write support for `java.nio.file.Path`

2.16.1 (24-Dec-2023)

Expand Down

0 comments on commit e598afa

Please sign in to comment.