Skip to content

Commit f53ef84

Browse files
committed
Adds _serialized String and Externalizable methods
1 parent b0ca5ed commit f53ef84

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.fasterxml.jackson.core;
22

33
import com.fasterxml.jackson.core.io.NumberInput;
4+
import java.io.Externalizable;
5+
import java.io.IOException;
6+
import java.io.ObjectInput;
7+
import java.io.ObjectOutput;
8+
import java.io.ObjectStreamException;
49

510
/**
611
* Implementation of
@@ -17,7 +22,7 @@
1722
*
1823
* @since 2.3
1924
*/
20-
public class JsonPointer extends Serializable
25+
public class JsonPointer implements Externalizable
2126
{
2227
private static final long serialVersionUID = 1L;
2328
/**
@@ -60,7 +65,11 @@ public class JsonPointer extends Serializable
6065
* so that {@link #toString} should be as efficient as possible.
6166
*/
6267
protected final String _asString;
63-
68+
/**
69+
* Strictly used by serialization as an intermediate placeholder.
70+
*/
71+
private String _serialized;
72+
6473
protected final String _matchingPropertyName;
6574

6675
protected final int _matchingElementIndex;
@@ -73,9 +82,10 @@ public class JsonPointer extends Serializable
7382

7483
/**
7584
* Constructor used for creating "empty" instance, used to represent
76-
* state that matches current node.
85+
* state that matches current node. Note that this constructor must be public for
86+
* implementing {@link Externalizable}.
7787
*/
78-
protected JsonPointer() {
88+
public JsonPointer() {
7989
_nextSegment = null;
8090
_matchingPropertyName = "";
8191
_matchingElementIndex = -1;
@@ -644,4 +654,17 @@ private static void _appendEscape(StringBuilder sb, char c) {
644654
}
645655
sb.append(c);
646656
}
657+
658+
@Override
659+
public void writeExternal(ObjectOutput out) throws IOException {
660+
out.writeUTF(_asString);
661+
}
662+
private Object readResolve() throws ObjectStreamException {
663+
return compile(_serialized);
664+
}
665+
666+
@Override
667+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
668+
_serialized = in.readUTF();
669+
}
647670
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
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+
310
public class TestJsonPointer extends BaseTest
411
{
512
public void testSimplePath() throws Exception
@@ -228,4 +235,31 @@ public void testLongNumbers() throws Exception
228235
assertTrue(ptr.matches());
229236
assertNull(ptr.tail());
230237
}
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+
assertEquals(original, copy);
263+
264+
}
231265
}

0 commit comments

Comments
 (0)