Skip to content

Commit 662bddc

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

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
@@ -956,6 +956,30 @@ public abstract int writeBinary(Base64Variant bv,
956956
*/
957957
public abstract void writeNumber(String encodedValue) throws IOException;
958958

959+
960+
/**
961+
* Write method that can be used for custom numeric types that can
962+
* not be (easily?) converted to "standard" Java number types.
963+
* Because numbers are not surrounded by double quotes, regular
964+
* {@link #writeString} method can not be used; nor
965+
* {@link #writeRaw} because that does not properly handle
966+
* value separators needed in Array or Object contexts.
967+
*<p>
968+
* Note: because of lack of type safety, some generator
969+
* implementations may not be able to implement this
970+
* method. For example, if a binary JSON format is used,
971+
* it may require type information for encoding; similarly
972+
* for generator-wrappers around Java objects or JSON nodes.
973+
* If implementation does not implement this method,
974+
* it needs to throw {@link UnsupportedOperationException}.
975+
*
976+
* @throws UnsupportedOperationException If underlying data format does not
977+
* support numbers serialized textually AND if generator is not allowed
978+
* to just output a String instead (Schema-based formats may require actual
979+
* number, for example)
980+
*/
981+
public abstract void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException;
982+
959983
/*
960984
/**********************************************************************
961985
/* 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
@@ -722,6 +722,27 @@ public void writeNumber(String encodedValue) throws IOException, UnsupportedOper
722722
delegate.writeNumber(encodedValue);
723723
}
724724

725+
@Override
726+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException
727+
{
728+
if (_itemFilter == null) {
729+
return;
730+
}
731+
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
732+
TokenFilter state = _filterContext.checkValue(_itemFilter);
733+
if (state == null) {
734+
return;
735+
}
736+
if (state != TokenFilter.INCLUDE_ALL) {
737+
if (!state.includeRawValue()) { // close enough?
738+
return;
739+
}
740+
}
741+
_checkParentPath();
742+
}
743+
delegate.writeNumber(encodedValueBuffer, offset, length);
744+
}
745+
725746
@Override
726747
public void writeBoolean(boolean v) throws IOException
727748
{

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,16 @@ public void writeNumber(String encodedValue) throws IOException
10721072
}
10731073
}
10741074

1075+
@Override
1076+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
1077+
_verifyValueWrite(WRITE_NUMBER);
1078+
if (_cfgNumbersAsStrings) {
1079+
_writeQuotedRaw(encodedValueBuffer, offset, length);
1080+
} else {
1081+
writeRaw(encodedValueBuffer, offset, length);
1082+
}
1083+
}
1084+
10751085
private final void _writeQuotedRaw(String value) throws IOException
10761086
{
10771087
if (_outputTail >= _outputEnd) {
@@ -1084,7 +1094,20 @@ private final void _writeQuotedRaw(String value) throws IOException
10841094
}
10851095
_outputBuffer[_outputTail++] = _quoteChar;
10861096
}
1087-
1097+
1098+
private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
1099+
{
1100+
if (_outputTail >= _outputEnd) {
1101+
_flushBuffer();
1102+
}
1103+
_outputBuffer[_outputTail++] = _quoteChar;
1104+
writeRaw(text, offset, length);
1105+
if (_outputTail >= _outputEnd) {
1106+
_flushBuffer();
1107+
}
1108+
_outputBuffer[_outputTail++] = _quoteChar;
1109+
}
1110+
10881111
@Override
10891112
public void writeBoolean(boolean state) throws IOException
10901113
{

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

+23
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,16 @@ public void writeNumber(String encodedValue) throws IOException
866866
}
867867
}
868868

869+
@Override
870+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
871+
_verifyValueWrite(WRITE_NUMBER);
872+
if (_cfgNumbersAsStrings) {
873+
_writeQuotedRaw(encodedValueBuffer, offset, length);
874+
} else {
875+
writeRaw(encodedValueBuffer, offset, length);
876+
}
877+
}
878+
869879
private void _writeQuotedRaw(String value) throws IOException
870880
{
871881
if (_outputTail >= _outputEnd) {
@@ -879,6 +889,19 @@ private void _writeQuotedRaw(String value) throws IOException
879889
_outputBuffer[_outputTail++] = _quoteChar;
880890
}
881891

892+
private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
893+
{
894+
if (_outputTail >= _outputEnd) {
895+
_flushBuffer();
896+
}
897+
_outputBuffer[_outputTail++] = _quoteChar;
898+
writeRaw(text, offset, length);
899+
if (_outputTail >= _outputEnd) {
900+
_flushBuffer();
901+
}
902+
_outputBuffer[_outputTail++] = _quoteChar;
903+
}
904+
882905
@Override
883906
public void writeBoolean(boolean state) throws IOException
884907
{

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

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

292+
@Override
293+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException { delegate.writeNumber(encodedValueBuffer, offset, length); }
294+
292295
@Override
293296
public void writeBoolean(boolean state) throws IOException { delegate.writeBoolean(state); }
294297

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

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

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

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public void testNumbersAsJSONStrings() throws IOException
7171
{
7272
JsonFactory f = new JsonFactory();
7373
// by default should output numbers as-is:
74-
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, false));
75-
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, true));
74+
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, false));
75+
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, true));
7676

7777
// but if overridden, quotes as Strings
7878
f = f.rebuild().configure(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS, true)
7979
.build();
80-
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
80+
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
8181
_writeNumbers(f, false));
82-
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
82+
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
8383
_writeNumbers(f, true));
8484
}
8585

@@ -193,6 +193,7 @@ private String _writeNumbers(JsonFactory f, boolean useBytes) throws IOException
193193
g.writeNumber(BigInteger.valueOf(3001));
194194
g.writeNumber(BigDecimal.valueOf(0.5));
195195
g.writeNumber("-1");
196+
g.writeNumber(new char[]{'1', '2', '.', '3', '-'}, 0, 4);
196197
g.writeEndArray();
197198
g.close();
198199

0 commit comments

Comments
 (0)