Skip to content

Commit 2f99742

Browse files
committed
Minor fixes to JsonPointer tests, new implementation (wrt JDK serialization)
1 parent 17bc4ca commit 2f99742

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

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

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,10 @@ public static JsonPointer forPath(JsonStreamContext context,
227227
}
228228
approxLength += 2 + propName.length();
229229
next = new PointerSegment(next, propName, -1);
230-
// tail = new JsonPointer(_fullPath(tail, seg), 0, seg, tail);
231230
} else if (context.inArray() || includeRoot) {
232231
int ix = context.getCurrentIndex();
233232
approxLength += 6;
234233
next = new PointerSegment(next, null, ix);
235-
// tail = new JsonPointer(_fullPath(tail, ixStr), 0, ixStr, ix, tail);
236234
}
237235
// NOTE: this effectively drops ROOT node(s); should have 1 such node,
238236
// as the last one, but we don't have to care (probably some paths have
@@ -244,7 +242,6 @@ public static JsonPointer forPath(JsonStreamContext context,
244242

245243
// And here the fun starts! We have the head, need to traverse
246244
// to compose full path String
247-
// final PointerSegment head = next;
248245
StringBuilder pathBuilder = new StringBuilder(approxLength);
249246
PointerSegment last = null;
250247

@@ -295,29 +292,7 @@ private static void _appendEscaped(StringBuilder sb, String segment)
295292
sb.append(c);
296293
}
297294
}
298-
299-
/* Factory method that composes a pointer instance, given a set
300-
* of 'raw' segments: raw meaning that no processing will be done,
301-
* no escaping may is present.
302-
*
303-
* @param segments
304-
*
305-
* @return Constructed path instance
306-
*/
307-
/* TODO!
308-
public static JsonPointer fromSegment(String... segments)
309-
{
310-
if (segments.length == 0) {
311-
return EMPTY;
312-
}
313-
JsonPointer prev = null;
314-
315-
for (String segment : segments) {
316-
JsonPointer next = new JsonPointer()
317-
}
318-
}
319-
*/
320-
295+
321296
/*
322297
/**********************************************************
323298
/* Public API
@@ -575,7 +550,7 @@ public JsonPointer head() {
575550

576551
/*
577552
/**********************************************************
578-
/* Standard method overrides
553+
/* Standard method overrides (since 2.14)
579554
/**********************************************************
580555
*/
581556

@@ -853,38 +828,42 @@ public PointerSegment(PointerSegment next, String pn, int ix) {
853828

854829
// Since 2.14: needed for efficient JDK serializability
855830
private Object writeReplace() {
856-
return new Serialization(_asString);
831+
// 11-Oct-2022, tatu: very important, must serialize just contents!
832+
return new Serialization(toString());
857833
}
858834

859835
/**
860836
* This must only exist to allow both final properties and implementation of
861-
* Externalizable/Serializable for JsonPointer
837+
* Externalizable/Serializable for JsonPointer.
838+
* Note that here we do not store offset but simply use (and expect use)
839+
* full path, from which we need to decode actual structure.
862840
*
863841
* @since 2.14
864842
*/
865843
static class Serialization implements Externalizable
866844
{
867-
private String _asString;
845+
private String _fullPath;
868846

869847
public Serialization() { }
870848

871-
Serialization(String asString) {
872-
_asString = asString;
849+
Serialization(String fullPath) {
850+
_fullPath = fullPath;
873851
}
874852

875853
@Override
876854
public void writeExternal(ObjectOutput out) throws IOException {
877-
out.writeUTF(_asString);
855+
out.writeUTF(_fullPath);
878856
}
879857

880858
@Override
881859
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
882-
_asString = in.readUTF();
860+
_fullPath = in.readUTF();
883861
}
884862

885863
private Object readResolve() throws ObjectStreamException {
886-
// NOTE: method handles canonicalization of "empty":
887-
return compile(_asString);
864+
// NOTE: method handles canonicalization of "empty", as well as other
865+
// aspects of decoding.
866+
return compile(_fullPath);
888867
}
889868
}
890869
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ public void testPointerSerializationNonEmpty() throws Exception
161161
JsonPointer copy = jdkDeserialize(ser);
162162
assertNotSame(copy, original);
163163
assertEquals(original, copy);
164+
assertEquals(original.hashCode(), copy.hashCode());
165+
166+
// 11-Oct-2022, tatu: Let's verify sub-path serializations too,
167+
// since 2.14 has rewritten internal implementation
168+
JsonPointer branch = original.tail();
169+
assertEquals("/15/name", branch.toString());
170+
ser = jdkSerialize(branch);
171+
copy = jdkDeserialize(ser);
172+
assertEquals("/15/name", copy.toString());
173+
assertEquals(branch, copy);
174+
assertEquals(branch.hashCode(), copy.hashCode());
175+
176+
JsonPointer leaf = branch.tail();
177+
assertEquals("/name", leaf.toString());
178+
ser = jdkSerialize(leaf);
179+
copy = jdkDeserialize(ser);
180+
assertEquals("/name", copy.toString());
181+
assertEquals(leaf, copy);
182+
assertEquals(leaf.hashCode(), copy.hashCode());
164183
}
165184

166185
public void testPointerSerializationEmpty() throws Exception

0 commit comments

Comments
 (0)