Skip to content

Commit de07f1c

Browse files
vycowtowncoder
authored andcommitted
test for #587 (#589)
Fix #587 Add JsonGenerator#writeNumber(char[],int,int) methods, tests
1 parent aae7769 commit de07f1c

File tree

7 files changed

+120
-6
lines changed

7 files changed

+120
-6
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,30 @@ public abstract int writeBinary(Base64Variant bv,
13571357
*/
13581358
public abstract void writeNumber(String encodedValue) throws IOException;
13591359

1360+
1361+
/**
1362+
* Write method that can be used for custom numeric types that can
1363+
* not be (easily?) converted to "standard" Java number types.
1364+
* Because numbers are not surrounded by double quotes, regular
1365+
* {@link #writeString} method can not be used; nor
1366+
* {@link #writeRaw} because that does not properly handle
1367+
* value separators needed in Array or Object contexts.
1368+
*<p>
1369+
* Note: because of lack of type safety, some generator
1370+
* implementations may not be able to implement this
1371+
* method. For example, if a binary JSON format is used,
1372+
* it may require type information for encoding; similarly
1373+
* for generator-wrappers around Java objects or JSON nodes.
1374+
* If implementation does not implement this method,
1375+
* it needs to throw {@link UnsupportedOperationException}.
1376+
*
1377+
* @throws UnsupportedOperationException If underlying data format does not
1378+
* support numbers serialized textually AND if generator is not allowed
1379+
* to just output a String instead (Schema-based formats may require actual
1380+
* number, for example)
1381+
*/
1382+
public abstract void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException;
1383+
13601384
/*
13611385
/**********************************************************
13621386
/* Public API, write methods, other value types

src/main/java/com/fasterxml/jackson/core/filter/FilteringGeneratorDelegate.java

+21
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,27 @@ public void writeNumber(String encodedValue) throws IOException, UnsupportedOper
764764
delegate.writeNumber(encodedValue);
765765
}
766766

767+
@Override
768+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException
769+
{
770+
if (_itemFilter == null) {
771+
return;
772+
}
773+
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
774+
TokenFilter state = _filterContext.checkValue(_itemFilter);
775+
if (state == null) {
776+
return;
777+
}
778+
if (state != TokenFilter.INCLUDE_ALL) {
779+
if (!state.includeRawValue()) { // close enough?
780+
return;
781+
}
782+
}
783+
_checkParentPath();
784+
}
785+
delegate.writeNumber(encodedValueBuffer, offset, length);
786+
}
787+
767788
@Override
768789
public void writeBoolean(boolean v) throws IOException
769790
{

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,16 @@ public void writeNumber(String encodedValue) throws IOException
10611061
}
10621062
}
10631063

1064+
@Override
1065+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
1066+
_verifyValueWrite(WRITE_NUMBER);
1067+
if (_cfgNumbersAsStrings) {
1068+
_writeQuotedRaw(encodedValueBuffer, offset, length);
1069+
} else {
1070+
writeRaw(encodedValueBuffer, offset, length);
1071+
}
1072+
}
1073+
10641074
private final void _writeQuotedRaw(String value) throws IOException
10651075
{
10661076
if (_outputTail >= _outputEnd) {
@@ -1073,7 +1083,20 @@ private final void _writeQuotedRaw(String value) throws IOException
10731083
}
10741084
_outputBuffer[_outputTail++] = _quoteChar;
10751085
}
1076-
1086+
1087+
private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
1088+
{
1089+
if (_outputTail >= _outputEnd) {
1090+
_flushBuffer();
1091+
}
1092+
_outputBuffer[_outputTail++] = _quoteChar;
1093+
writeRaw(text, offset, length);
1094+
if (_outputTail >= _outputEnd) {
1095+
_flushBuffer();
1096+
}
1097+
_outputBuffer[_outputTail++] = _quoteChar;
1098+
}
1099+
10771100
@Override
10781101
public void writeBoolean(boolean state) throws IOException
10791102
{

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

+23
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,16 @@ public void writeNumber(String encodedValue) throws IOException
836836
}
837837
}
838838

839+
@Override
840+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
841+
_verifyValueWrite(WRITE_NUMBER);
842+
if (_cfgNumbersAsStrings) {
843+
_writeQuotedRaw(encodedValueBuffer, offset, length);
844+
} else {
845+
writeRaw(encodedValueBuffer, offset, length);
846+
}
847+
}
848+
839849
private void _writeQuotedRaw(String value) throws IOException
840850
{
841851
if (_outputTail >= _outputEnd) {
@@ -849,6 +859,19 @@ private void _writeQuotedRaw(String value) throws IOException
849859
_outputBuffer[_outputTail++] = _quoteChar;
850860
}
851861

862+
private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
863+
{
864+
if (_outputTail >= _outputEnd) {
865+
_flushBuffer();
866+
}
867+
_outputBuffer[_outputTail++] = _quoteChar;
868+
writeRaw(text, offset, length);
869+
if (_outputTail >= _outputEnd) {
870+
_flushBuffer();
871+
}
872+
_outputBuffer[_outputTail++] = _quoteChar;
873+
}
874+
852875
@Override
853876
public void writeBoolean(boolean state) throws IOException
854877
{

src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ public void writeString(Reader reader, int len) throws IOException {
336336
@Override
337337
public void writeNumber(String encodedValue) throws IOException, UnsupportedOperationException { delegate.writeNumber(encodedValue); }
338338

339+
@Override
340+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException { delegate.writeNumber(encodedValueBuffer, offset, length); }
341+
339342
@Override
340343
public void writeBoolean(boolean state) throws IOException { delegate.writeBoolean(state); }
341344

src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public void testWriteStartObjectWithObject() throws Exception
435435
}
436436

437437
// [core#580]
438-
public void testRawValueDelegation() throws Exception
438+
public void testRawValueDelegationWithArray() throws Exception
439439
{
440440
StringWriter w = new StringWriter();
441441
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
@@ -453,4 +453,23 @@ public void testRawValueDelegation() throws Exception
453453
gen.close();
454454
assertEquals("[1,3,/* comment */,42]", w.toString());
455455
}
456+
457+
// [core#588]
458+
public void testRawValueDelegationWithObject() throws Exception
459+
{
460+
StringWriter w = new StringWriter();
461+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(ObjectWriteContext.empty(), w),
462+
TokenFilter.INCLUDE_ALL, true, true);
463+
464+
gen.writeStartObject();
465+
gen.writeNumberField("f1", 1);
466+
gen.writeFieldName("f2");
467+
gen.writeRawValue(new char[]{'1', '2', '.', '3', '-'}, 0, 4);
468+
gen.writeNumberField("f3", 3);
469+
gen.writeEndObject();
470+
471+
gen.close();
472+
assertEquals(aposToQuotes("{'f1':1,'f2':12.3,'f3':3}"), w.toString());
473+
}
474+
456475
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ public void testNumbersAsJSONStrings() throws IOException
9595
{
9696
JsonFactory f = new JsonFactory();
9797
// by default should output numbers as-is:
98-
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, false));
99-
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, true));
98+
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, false));
99+
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, true));
100100

101101
// but if overridden, quotes as Strings
102102
f = JsonFactory.builder()
103103
.enable(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS)
104104
.build();
105-
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
105+
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
106106
_writeNumbers(f, false));
107-
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
107+
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
108108
_writeNumbers(f, true));
109109

110110

@@ -227,6 +227,7 @@ private String _writeNumbers(JsonFactory f, boolean useBytes) throws IOException
227227
g.writeNumber(BigInteger.valueOf(3001));
228228
g.writeNumber(BigDecimal.valueOf(0.5));
229229
g.writeNumber("-1");
230+
g.writeNumber(new char[]{'1', '2', '.', '3', '-'}, 0, 4);
230231
g.writeEndArray();
231232
g.close();
232233

0 commit comments

Comments
 (0)