Skip to content

Commit

Permalink
Add release notes wrt #762, minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 16, 2022
1 parent ca18393 commit 4f95115
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 42 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,7 @@ Illia Ovchynnikov (wingsofovnia@github)
* Reported #759: JsonGenerator to provide current value to the context before
starting objects
(2.14.0)

Evan Galpin (egalpin@github)
* Contributed #762: Make `JsonPointer` `java.io.Serializable`
(2.14.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ JSON library.
(contributed by @pjfanning)
#759: JsonGenerator to provide current value to the context before starting objects
(reported by Illia O)
#762: Make `JsonPointer` `java.io.Serializable`
(contributed by Evan G)
#763: `JsonFactory.createParser()` with `File` may leak `InputStream`s
#764: `JsonFactory.createGenerator()` with `File` may leak `OutputStream`s

Expand Down
24 changes: 18 additions & 6 deletions src/main/java/com/fasterxml/jackson/core/JsonPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class JsonPointer implements Serializable
{
private static final long serialVersionUID = 1L;

/**
* Character used to separate segments.
*
Expand Down Expand Up @@ -524,7 +525,7 @@ public JsonPointer head() {
if (!(o instanceof JsonPointer)) return false;
return _asString.equals(((JsonPointer) o)._asString);
}

/*
/**********************************************************
/* Internal methods
Expand Down Expand Up @@ -651,21 +652,30 @@ private static void _appendEscape(StringBuilder sb, char c) {
sb.append(c);
}

/*
/**********************************************************
/* Support for JDK serialization (2.14+)
/**********************************************************
*/

// Since 2.14: needed for efficient JDK serializability
private Object writeReplace() {
return new SerializableJsonPointer(_asString);
return new Serialization(_asString);
}

/**
* This must only exist to allow both final properties and implementation of
* Externalizable/Serializable for JsonPointer
*
* @since 2.14
*/
private static class SerializableJsonPointer implements Externalizable {
static class Serialization implements Externalizable
{
private String _asString;

public SerializableJsonPointer() {
}
public Serialization() { }

public SerializableJsonPointer(String asString) {
Serialization(String asString) {
_asString = asString;
}

Expand All @@ -678,7 +688,9 @@ public void writeExternal(ObjectOutput out) throws IOException {
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
_asString = in.readUTF();
}

private Object readResolve() throws ObjectStreamException {
// NOTE: method handles canonicalization of "empty":
return compile(_asString);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*/
public class TestJDKSerializability extends BaseTest
{
/*
/**********************************************************************
/* Main factory type(s)
/**********************************************************************
*/

public void testJsonFactorySerializable() throws Exception
{
JsonFactory f = new JsonFactory();
Expand All @@ -26,6 +32,12 @@ public void testJsonFactorySerializable() throws Exception
assertEquals(origJson, _copyJson(f2, origJson, true));
}

/*
/**********************************************************************
/* Parser-related types
/**********************************************************************
*/

public void testBase64Variant() throws Exception
{
{
Expand Down Expand Up @@ -92,6 +104,12 @@ public void testSourceReference() throws Exception
assertSame(ref2, ContentReference.unknown());
}

/*
/**********************************************************************
/* Exception types
/**********************************************************************
*/

public void testParseException() throws Exception
{
JsonFactory jf = new JsonFactory();
Expand Down Expand Up @@ -127,7 +145,34 @@ public void testGenerationException() throws Exception
JsonGenerationException result = jdkDeserialize(stuff);
assertNotNull(result);
}


/*
/**********************************************************************
/* Misc other types
/**********************************************************************
*/

public void testPointerSerializationNonEmpty() throws Exception
{
// First, see that we can write and read a general JsonPointer
final String INPUT = "/Image/15/name";
JsonPointer original = JsonPointer.compile(INPUT);
byte[] ser = jdkSerialize(original);
JsonPointer copy = jdkDeserialize(ser);
assertNotSame(copy, original);
assertEquals(original, copy);
}

public void testPointerSerializationEmpty() throws Exception
{
// and then verify that "empty" instance gets canonicalized
final JsonPointer emptyP = JsonPointer.empty();
byte[] ser = jdkSerialize(emptyP);
JsonPointer result = jdkDeserialize(ser);
assertSame("Should get same 'empty' instance when JDK serialize+deserialize",
emptyP, result);
}

/*
/**********************************************************
/* Helper methods
Expand Down
35 changes: 0 additions & 35 deletions src/test/java/com/fasterxml/jackson/core/TestJsonPointer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.fasterxml.jackson.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestJsonPointer extends BaseTest
{
public void testSimplePath() throws Exception
Expand Down Expand Up @@ -235,32 +228,4 @@ public void testLongNumbers() throws Exception
assertTrue(ptr.matches());
assertNull(ptr.tail());
}

private static <T extends Serializable> byte[] pickle(T obj)
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
return baos.toByteArray();
}

private static <T extends Serializable> T unpickle(byte[] b, Class<T> cl)
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
Object o = ois.readObject();
return cl.cast(o);
}
public void testPointerSerialization() throws Exception {

final String INPUT = "/Image/15/name";
JsonPointer original = JsonPointer.compile(INPUT);
JsonPointer copy = unpickle(pickle(original), JsonPointer.class);
assertNotSame(copy, original);
assertEquals(original, copy);

}
}

0 comments on commit 4f95115

Please sign in to comment.