Skip to content

Commit e1bb5ea

Browse files
committed
Update release notes wrt #717, minor clean up
1 parent 4c2f048 commit e1bb5ea

File tree

8 files changed

+74
-43
lines changed

8 files changed

+74
-43
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ Nik Everett (nik9000@github)
310310
* Contributed #715: Allow TokenFilters to keep empty arrays and objects
311311
(2.14.0)
312312

313+
Richard Kwasnicki (Richie94@github)
314+
* Contributed #717: Hex capitalization for JsonWriter should be configurable
315+
(2.14.0)
316+
313317
Illia Ovchynnikov (wingsofovnia@github)
314318
* Reported #759: JsonGenerator to provide current value to the context before
315319
starting objects

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ JSON library.
2626
(contributed by Ilya G)
2727
#715: Allow TokenFilters to keep empty arrays and objects
2828
(contributed by Nik E)
29+
#717: Hex capitalization for JsonWriter should be configurable (add
30+
`JsonWriteFeature.WRITE_HEX_UPPER_CASE`)
31+
(contributed Richard K)
2932
#733: Add `StreamReadCapability.EXACT_FLOATS` to indicate whether parser reports exact
3033
floating-point values or not
3134
(contributed Doug R)

src/main/java/com/fasterxml/jackson/core/io/CharTypes.java

+8
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ public static void appendQuoted(StringBuilder sb, String content) {
288288
}
289289
}
290290

291+
/**
292+
* @deprecated Since 2.14
293+
*/
294+
@Deprecated // since 2.14
291295
public static char[] copyHexChars() {
292296
return copyHexChars(true);
293297
}
@@ -296,6 +300,10 @@ public static char[] copyHexChars(boolean uppercase) {
296300
return (uppercase) ? HC.clone() : HClower.clone();
297301
}
298302

303+
/**
304+
* @deprecated Since 2.14
305+
*/
306+
@Deprecated // since 2.14
299307
public static byte[] copyHexBytes() {
300308
return copyHexBytes(true);
301309
}

src/main/java/com/fasterxml/jackson/core/json/JsonWriteFeature.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ public enum JsonWriteFeature
7575
/**
7676
* Feature that specifies that hex values are encoded with capital letters.
7777
*<p>
78-
* Can be disabled to have a better possibility to compare between other Json
78+
* Can be disabled to have a better possibility to compare between other JSON
7979
* writer libraries, such as JSON.stringify from Javascript.
8080
*<p>
81-
* Feature is enabled by default.
81+
* Feature is enabled by default for backwards compatibility with earlier
82+
* versions.
8283
*
8384
* @since 2.14
8485
*/

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ public class UTF8JsonGenerator
2929
// intermediate copies only made up to certain length...
3030
private final static int MAX_BYTES_TO_BUFFER = 512;
3131

32-
private final static byte[] HEX_CHARS_UPPER = CharTypes.copyHexBytes(true);
33-
private final static byte[] HEX_CHARS_LOWER = CharTypes.copyHexBytes(false);
32+
private final static byte[] HEX_BYTES_UPPER = CharTypes.copyHexBytes(true);
33+
private final static byte[] HEX_BYTES_LOWER = CharTypes.copyHexBytes(false);
3434

3535
private final static byte[] NULL_BYTES = { 'n', 'u', 'l', 'l' };
3636
private final static byte[] TRUE_BYTES = { 't', 'r', 'u', 'e' };
3737
private final static byte[] FALSE_BYTES = { 'f', 'a', 'l', 's', 'e' };
3838

39-
private byte[] getHexChars() {
40-
return _cfgWriteHexUppercase ? HEX_CHARS_UPPER : HEX_CHARS_LOWER;
41-
}
42-
4339
/*
4440
/**********************************************************
4541
/* Configuration
@@ -2141,7 +2137,7 @@ protected final void _outputSurrogates(int surr1, int surr2) throws IOException
21412137
*/
21422138
private final int _outputMultiByteChar(int ch, int outputPtr) throws IOException
21432139
{
2144-
byte[] HEX_CHARS = getHexChars();
2140+
byte[] HEX_CHARS = getHexBytes();
21452141
byte[] bbuf = _outputBuffer;
21462142
if (ch >= SURR1_FIRST && ch <= SURR2_LAST) { // yes, outside of BMP; add an escape
21472143
// 23-Nov-2015, tatu: As per [core#223], may or may not want escapes;
@@ -2181,7 +2177,7 @@ private final void _writeNull() throws IOException
21812177
private int _writeGenericEscape(int charToEscape, int outputPtr) throws IOException
21822178
{
21832179
final byte[] bbuf = _outputBuffer;
2184-
byte[] HEX_CHARS = getHexChars();
2180+
byte[] HEX_CHARS = getHexBytes();
21852181
bbuf[outputPtr++] = BYTE_BACKSLASH;
21862182
bbuf[outputPtr++] = BYTE_u;
21872183
if (charToEscape > 0xFF) {
@@ -2207,4 +2203,9 @@ protected final void _flushBuffer() throws IOException
22072203
_outputStream.write(_outputBuffer, 0, len);
22082204
}
22092205
}
2206+
2207+
private byte[] getHexBytes() {
2208+
return _cfgWriteHexUppercase ? HEX_BYTES_UPPER : HEX_BYTES_LOWER;
2209+
}
22102210
}
2211+

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

+4
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ protected static byte[] utf8Bytes(String str) {
575575
return str.getBytes(StandardCharsets.UTF_8);
576576
}
577577

578+
protected static String fromUTF8(ByteArrayOutputStream bytes) {
579+
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
580+
}
581+
578582
protected void fieldNameFor(StringBuilder sb, int index)
579583
{
580584
/* let's do something like "f1.1" to exercise different

src/test/java/com/fasterxml/jackson/core/json/GeneratorFeaturesTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,45 @@ private void _testNonNumericQuoting(JsonFactory f, boolean quoted)
354354
assertEquals("{\"double\":NaN} {\"float\":NaN}", result);
355355
}
356356
}
357+
358+
// [core#717]: configurable hex digits; lower-case
359+
public void testHexLowercase() throws Exception {
360+
JsonFactory f = JsonFactory.builder()
361+
.disable(JsonWriteFeature.WRITE_HEX_UPPER_CASE)
362+
.build();
363+
_testHexOutput(f, false, "\u001b", q("\\u001b"));
364+
_testHexOutput(f, true, "\u001b", q("\\u001b"));
365+
}
366+
367+
// [core#717]: configurable hex digits; upper-case (default)
368+
public void testHexUppercase() throws Exception
369+
{
370+
JsonFactory f = JsonFactory.builder()
371+
.enable(JsonWriteFeature.WRITE_HEX_UPPER_CASE)
372+
.build();
373+
_testHexOutput(f, false, "\u001b", q("\\u001B"));
374+
_testHexOutput(f, true, "\u001b", q("\\u001B"));
375+
}
376+
377+
private void _testHexOutput(JsonFactory f, boolean useBytes,
378+
String input, String exp) throws Exception
379+
{
380+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
381+
StringWriter sw = new StringWriter();
382+
JsonGenerator g;
383+
if (useBytes) {
384+
g = f.createGenerator(bytes);
385+
} else {
386+
g = f.createGenerator(sw);
387+
}
388+
389+
g.writeString(input);
390+
g.flush();
391+
g.close();
392+
393+
String result = useBytes ? fromUTF8(bytes) : sw.toString();
394+
assertEquals(exp, result);
395+
396+
g.close();
397+
}
357398
}

src/test/java/com/fasterxml/jackson/core/write/UTF8GeneratorTest.java

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.core.write;
22

3+
import java.io.ByteArrayOutputStream;
4+
35
import com.fasterxml.jackson.core.*;
46
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
57
import com.fasterxml.jackson.core.filter.JsonPointerBasedFilter;
@@ -9,10 +11,6 @@
911
import com.fasterxml.jackson.core.json.UTF8JsonGenerator;
1012
import com.fasterxml.jackson.core.util.BufferRecycler;
1113

12-
import java.io.ByteArrayOutputStream;
13-
14-
import static com.fasterxml.jackson.core.json.JsonWriteFeature.WRITE_HEX_UPPER_CASE;
15-
1614
public class UTF8GeneratorTest extends BaseTest
1715
{
1816
private final JsonFactory JSON_F = new JsonFactory();
@@ -116,33 +114,4 @@ public void testFilteringWithEscapedChars() throws Exception
116114
assertNull(p.nextToken());
117115
p.close();
118116
}
119-
120-
public void testHexLowercase() throws Exception
121-
{
122-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
123-
JsonFactory factory = JsonFactory.builder().disable(WRITE_HEX_UPPER_CASE).build();
124-
JsonGenerator gen = factory.createGenerator(bytes);
125-
String str = "\u001b";
126-
gen.writeString(str);
127-
gen.flush();
128-
gen.close();
129-
130-
String result = bytes.toString();
131-
assertEquals("\"\\u001b\"", result);
132-
}
133-
134-
public void testHexUppercase() throws Exception
135-
{
136-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
137-
JsonFactory factory = JsonFactory.builder().enable(WRITE_HEX_UPPER_CASE).build();
138-
JsonGenerator gen = factory.createGenerator(bytes);
139-
String str = "\u001b";
140-
gen.writeString(str);
141-
gen.flush();
142-
gen.close();
143-
144-
String result = bytes.toString();
145-
assertEquals("\"\\u001B\"", result);
146-
}
147-
148117
}

0 commit comments

Comments
 (0)