Skip to content

Commit 143d9cb

Browse files
committed
Merge branch '2.14' into double-parser
2 parents bdb2d13 + fac1930 commit 143d9cb

File tree

6 files changed

+122
-8
lines changed

6 files changed

+122
-8
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: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
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;
9+
import java.io.Serializable;
410

511
/**
612
* Implementation of
@@ -17,8 +23,10 @@
1723
*
1824
* @since 2.3
1925
*/
20-
public class JsonPointer
26+
public class JsonPointer implements Serializable
2127
{
28+
private static final long serialVersionUID = 1L;
29+
2230
/**
2331
* Character used to separate segments.
2432
*
@@ -59,7 +67,7 @@ public class JsonPointer
5967
* so that {@link #toString} should be as efficient as possible.
6068
*/
6169
protected final String _asString;
62-
70+
6371
protected final String _matchingPropertyName;
6472

6573
protected final int _matchingElementIndex;
@@ -69,7 +77,7 @@ public class JsonPointer
6977
/* Construction
7078
/**********************************************************
7179
*/
72-
80+
7381
/**
7482
* Constructor used for creating "empty" instance, used to represent
7583
* state that matches current node.
@@ -517,7 +525,7 @@ public JsonPointer head() {
517525
if (!(o instanceof JsonPointer)) return false;
518526
return _asString.equals(((JsonPointer) o)._asString);
519527
}
520-
528+
521529
/*
522530
/**********************************************************
523531
/* Internal methods
@@ -643,4 +651,47 @@ private static void _appendEscape(StringBuilder sb, char c) {
643651
}
644652
sb.append(c);
645653
}
654+
655+
/*
656+
/**********************************************************
657+
/* Support for JDK serialization (2.14+)
658+
/**********************************************************
659+
*/
660+
661+
// Since 2.14: needed for efficient JDK serializability
662+
private Object writeReplace() {
663+
return new Serialization(_asString);
664+
}
665+
666+
/**
667+
* This must only exist to allow both final properties and implementation of
668+
* Externalizable/Serializable for JsonPointer
669+
*
670+
* @since 2.14
671+
*/
672+
static class Serialization implements Externalizable
673+
{
674+
private String _asString;
675+
676+
public Serialization() { }
677+
678+
Serialization(String asString) {
679+
_asString = asString;
680+
}
681+
682+
@Override
683+
public void writeExternal(ObjectOutput out) throws IOException {
684+
out.writeUTF(_asString);
685+
}
686+
687+
@Override
688+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
689+
_asString = in.readUTF();
690+
}
691+
692+
private Object readResolve() throws ObjectStreamException {
693+
// NOTE: method handles canonicalization of "empty":
694+
return compile(_asString);
695+
}
696+
}
646697
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
216216
* Helper methods used for constructing an {@link InputStream} for
217217
* parsers to use, when input is to be read from given {@link File}.
218218
*
219+
* @param f File to open stream for
220+
*
221+
* @return {@link InputStream} constructed
222+
*
223+
* @throws IOException If there is a problem opening the stream
224+
*
219225
* @since 2.14
220226
*/
221227
protected InputStream _fileInputStream(File f) throws IOException {
@@ -226,6 +232,12 @@ protected InputStream _fileInputStream(File f) throws IOException {
226232
* Helper methods used for constructing an {@link OutputStream} for
227233
* generator to use, when target is to be written into given {@link File}.
228234
*
235+
* @param f File to open stream for
236+
*
237+
* @return {@link OutputStream} constructed
238+
*
239+
* @throws IOException If there is a problem opening the stream
240+
*
229241
* @since 2.14
230242
*/
231243
protected OutputStream _fileOutputStream(File f) throws IOException {

src/main/java/com/fasterxml/jackson/core/io/doubleparser/package-info.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* <dd>Copyright (c) Werner Randelshofer. Apache 2.0 License.
66
* <a href="https://github.com/wrandelshofer/FastDoubleParser">github.com</a>.</dd>
77
* </dl>
8-
*/
9-
10-
/**
8+
*
119
* Provides parsers that parse a {@code FloatValue} from a
1210
* {@link java.lang.CharSequence}, {@code char} array, or {@code byte} array
1311
* ({@code str});.
@@ -123,5 +121,7 @@
123121
* <dd><a href="https://docs.oracle.com/javase/specs/jls/se18/html/jls-3.html#jls-3.10.2">docs.oracle.com</a></dd>
124122
* </dl>
125123
* </p>
124+
*
125+
* @since 2.14
126126
*/
127127
package com.fasterxml.jackson.core.io.doubleparser;

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

0 commit comments

Comments
 (0)