Skip to content

Commit

Permalink
Merge branch '2.17'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 17, 2024
2 parents f406086 + 11d0611 commit a09c27b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Path;
import java.util.*;

import tools.jackson.core.*;
Expand Down Expand Up @@ -268,6 +269,9 @@ public void writeProperty(String propName, Object value, int type) throws Jackso
case SER_URI:
writeStringLikeProperty(propName, value.toString(), type);
return;
case SER_PATH:
writeStringLikeProperty(propName, ((Path) value).toUri().toString(), type);
return;

// Others

Expand Down Expand Up @@ -398,6 +402,9 @@ protected void _writeValue(Object value, int type) throws JacksonException
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 tools.jackson.core.*;
Expand Down Expand Up @@ -243,6 +244,16 @@ public Object read(JSONReader reader, JsonParser p) throws JacksonException
return null;
}
return URI.create(p.getValueAsString());
case SER_PATH:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
String pathStr = p.getValueAsString();
try {
return Paths.get(new URI(pathStr));
} catch (Exception e) {
throw new JSONObjectException("Failed to bind `java.nio.file.Path` from value '"+pathStr+"'");
}

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

import tools.jackson.core.TreeNode;
Expand Down Expand Up @@ -116,14 +117,15 @@ public abstract class ValueLocatorBase
public final static int SER_UUID = 34;
public final static int SER_URL = 35;
public final static int SER_URI = 36;
public final static int SER_PATH = 37; // 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 = 37;
public final static int SER_ITERABLE = 38;

/*
/**********************************************************************
Expand Down Expand Up @@ -240,6 +242,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
13 changes: 13 additions & 0 deletions jr-objects/src/test/java/tools/jackson/jr/ob/ReadSimpleTest.java
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 tools.jackson.core.JsonParser;
Expand Down Expand Up @@ -178,6 +180,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 tools.jackson.jr.ob.JSON.Feature;
Expand Down Expand Up @@ -105,12 +107,16 @@ public void testNest() throws Exception
public void testKnownSimpleTypes() throws Exception
{
final String URL_STR = "http://fasterxml.com";
final URI uri = new URI(URL_STR);
assertEquals(q("http:\\/\\/fasterxml.com"),
JSON.std.asString(new URI(URL_STR)));
JSON.std.asString(uri));
final String PATH = "/foo/bar.txt";
assertEquals(q("\\/foo\\/bar.txt"),
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 a09c27b

Please sign in to comment.