Skip to content

Commit 4f95115

Browse files
committed
Add release notes wrt #762, minor cleanup
1 parent ca18393 commit 4f95115

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,7 @@ Illia Ovchynnikov (wingsofovnia@github)
303303
* Reported #759: JsonGenerator to provide current value to the context before
304304
starting objects
305305
(2.14.0)
306+
307+
Evan Galpin (egalpin@github)
308+
* Contributed #762: Make `JsonPointer` `java.io.Serializable`
309+
(2.14.0)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ JSON library.
3232
(contributed by @pjfanning)
3333
#759: JsonGenerator to provide current value to the context before starting objects
3434
(reported by Illia O)
35+
#762: Make `JsonPointer` `java.io.Serializable`
36+
(contributed by Evan G)
3537
#763: `JsonFactory.createParser()` with `File` may leak `InputStream`s
3638
#764: `JsonFactory.createGenerator()` with `File` may leak `OutputStream`s
3739

src/main/java/com/fasterxml/jackson/core/JsonPointer.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class JsonPointer implements Serializable
2727
{
2828
private static final long serialVersionUID = 1L;
29+
2930
/**
3031
* Character used to separate segments.
3132
*
@@ -524,7 +525,7 @@ public JsonPointer head() {
524525
if (!(o instanceof JsonPointer)) return false;
525526
return _asString.equals(((JsonPointer) o)._asString);
526527
}
527-
528+
528529
/*
529530
/**********************************************************
530531
/* Internal methods
@@ -651,21 +652,30 @@ private static void _appendEscape(StringBuilder sb, char c) {
651652
sb.append(c);
652653
}
653654

655+
/*
656+
/**********************************************************
657+
/* Support for JDK serialization (2.14+)
658+
/**********************************************************
659+
*/
660+
661+
// Since 2.14: needed for efficient JDK serializability
654662
private Object writeReplace() {
655-
return new SerializableJsonPointer(_asString);
663+
return new Serialization(_asString);
656664
}
657665

658666
/**
659667
* This must only exist to allow both final properties and implementation of
660668
* Externalizable/Serializable for JsonPointer
669+
*
670+
* @since 2.14
661671
*/
662-
private static class SerializableJsonPointer implements Externalizable {
672+
static class Serialization implements Externalizable
673+
{
663674
private String _asString;
664675

665-
public SerializableJsonPointer() {
666-
}
676+
public Serialization() { }
667677

668-
public SerializableJsonPointer(String asString) {
678+
Serialization(String asString) {
669679
_asString = asString;
670680
}
671681

@@ -678,7 +688,9 @@ public void writeExternal(ObjectOutput out) throws IOException {
678688
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
679689
_asString = in.readUTF();
680690
}
691+
681692
private Object readResolve() throws ObjectStreamException {
693+
// NOTE: method handles canonicalization of "empty":
682694
return compile(_asString);
683695
}
684696
}

src/test/java/com/fasterxml/jackson/core/TestJDKSerializability.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
*/
1111
public class TestJDKSerializability extends BaseTest
1212
{
13+
/*
14+
/**********************************************************************
15+
/* Main factory type(s)
16+
/**********************************************************************
17+
*/
18+
1319
public void testJsonFactorySerializable() throws Exception
1420
{
1521
JsonFactory f = new JsonFactory();
@@ -26,6 +32,12 @@ public void testJsonFactorySerializable() throws Exception
2632
assertEquals(origJson, _copyJson(f2, origJson, true));
2733
}
2834

35+
/*
36+
/**********************************************************************
37+
/* Parser-related types
38+
/**********************************************************************
39+
*/
40+
2941
public void testBase64Variant() throws Exception
3042
{
3143
{
@@ -92,6 +104,12 @@ public void testSourceReference() throws Exception
92104
assertSame(ref2, ContentReference.unknown());
93105
}
94106

107+
/*
108+
/**********************************************************************
109+
/* Exception types
110+
/**********************************************************************
111+
*/
112+
95113
public void testParseException() throws Exception
96114
{
97115
JsonFactory jf = new JsonFactory();
@@ -127,7 +145,34 @@ public void testGenerationException() throws Exception
127145
JsonGenerationException result = jdkDeserialize(stuff);
128146
assertNotNull(result);
129147
}
130-
148+
149+
/*
150+
/**********************************************************************
151+
/* Misc other types
152+
/**********************************************************************
153+
*/
154+
155+
public void testPointerSerializationNonEmpty() throws Exception
156+
{
157+
// First, see that we can write and read a general JsonPointer
158+
final String INPUT = "/Image/15/name";
159+
JsonPointer original = JsonPointer.compile(INPUT);
160+
byte[] ser = jdkSerialize(original);
161+
JsonPointer copy = jdkDeserialize(ser);
162+
assertNotSame(copy, original);
163+
assertEquals(original, copy);
164+
}
165+
166+
public void testPointerSerializationEmpty() throws Exception
167+
{
168+
// and then verify that "empty" instance gets canonicalized
169+
final JsonPointer emptyP = JsonPointer.empty();
170+
byte[] ser = jdkSerialize(emptyP);
171+
JsonPointer result = jdkDeserialize(ser);
172+
assertSame("Should get same 'empty' instance when JDK serialize+deserialize",
173+
emptyP, result);
174+
}
175+
131176
/*
132177
/**********************************************************
133178
/* Helper methods

src/test/java/com/fasterxml/jackson/core/TestJsonPointer.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package com.fasterxml.jackson.core;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.ByteArrayOutputStream;
5-
import java.io.IOException;
6-
import java.io.ObjectInputStream;
7-
import java.io.ObjectOutputStream;
8-
import java.io.Serializable;
9-
103
public class TestJsonPointer extends BaseTest
114
{
125
public void testSimplePath() throws Exception
@@ -235,32 +228,4 @@ public void testLongNumbers() throws Exception
235228
assertTrue(ptr.matches());
236229
assertNull(ptr.tail());
237230
}
238-
239-
private static <T extends Serializable> byte[] pickle(T obj)
240-
throws IOException
241-
{
242-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
243-
ObjectOutputStream oos = new ObjectOutputStream(baos);
244-
oos.writeObject(obj);
245-
oos.close();
246-
return baos.toByteArray();
247-
}
248-
249-
private static <T extends Serializable> T unpickle(byte[] b, Class<T> cl)
250-
throws IOException, ClassNotFoundException
251-
{
252-
ByteArrayInputStream bais = new ByteArrayInputStream(b);
253-
ObjectInputStream ois = new ObjectInputStream(bais);
254-
Object o = ois.readObject();
255-
return cl.cast(o);
256-
}
257-
public void testPointerSerialization() throws Exception {
258-
259-
final String INPUT = "/Image/15/name";
260-
JsonPointer original = JsonPointer.compile(INPUT);
261-
JsonPointer copy = unpickle(pickle(original), JsonPointer.class);
262-
assertNotSame(copy, original);
263-
assertEquals(original, copy);
264-
265-
}
266231
}

0 commit comments

Comments
 (0)