Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for #587 #589

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,30 @@ public abstract int writeBinary(Base64Variant bv,
*/
public abstract void writeNumber(String encodedValue) throws IOException;


/**
* Write method that can be used for custom numeric types that can
* not be (easily?) converted to "standard" Java number types.
* Because numbers are not surrounded by double quotes, regular
* {@link #writeString} method can not be used; nor
* {@link #writeRaw} because that does not properly handle
* value separators needed in Array or Object contexts.
*<p>
* Note: because of lack of type safety, some generator
* implementations may not be able to implement this
* method. For example, if a binary JSON format is used,
* it may require type information for encoding; similarly
* for generator-wrappers around Java objects or JSON nodes.
* If implementation does not implement this method,
* it needs to throw {@link UnsupportedOperationException}.
*
* @throws UnsupportedOperationException If underlying data format does not
* support numbers serialized textually AND if generator is not allowed
* to just output a String instead (Schema-based formats may require actual
* number, for example)
*/
public abstract void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException;

/*
/**********************************************************************
/* Public API, write methods, other value types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,27 @@ public void writeNumber(String encodedValue) throws IOException, UnsupportedOper
delegate.writeNumber(encodedValue);
}

@Override
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException
{
if (_itemFilter == null) {
return;
}
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
TokenFilter state = _filterContext.checkValue(_itemFilter);
if (state == null) {
return;
}
if (state != TokenFilter.INCLUDE_ALL) {
if (!state.includeRawValue()) { // close enough?
return;
}
}
_checkParentPath();
}
delegate.writeNumber(encodedValueBuffer, offset, length);
}

@Override
public void writeBoolean(boolean v) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,16 @@ public void writeNumber(String encodedValue) throws IOException
}
}

@Override
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
_verifyValueWrite(WRITE_NUMBER);
if (_cfgNumbersAsStrings) {
_writeQuotedRaw(encodedValueBuffer, offset, length);
} else {
writeRaw(encodedValueBuffer, offset, length);
}
}

private final void _writeQuotedRaw(String value) throws IOException
{
if (_outputTail >= _outputEnd) {
Expand All @@ -1084,7 +1094,20 @@ private final void _writeQuotedRaw(String value) throws IOException
}
_outputBuffer[_outputTail++] = _quoteChar;
}


private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
{
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
writeRaw(text, offset, length);
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
}

@Override
public void writeBoolean(boolean state) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,16 @@ public void writeNumber(String encodedValue) throws IOException
}
}

@Override
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
_verifyValueWrite(WRITE_NUMBER);
if (_cfgNumbersAsStrings) {
_writeQuotedRaw(encodedValueBuffer, offset, length);
} else {
writeRaw(encodedValueBuffer, offset, length);
}
}

private void _writeQuotedRaw(String value) throws IOException
{
if (_outputTail >= _outputEnd) {
Expand All @@ -879,6 +889,19 @@ private void _writeQuotedRaw(String value) throws IOException
_outputBuffer[_outputTail++] = _quoteChar;
}

private void _writeQuotedRaw(char[] text, int offset, int length) throws IOException
{
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
writeRaw(text, offset, length);
if (_outputTail >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
}

@Override
public void writeBoolean(boolean state) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ public void writeString(Reader reader, int len) throws IOException {
@Override
public void writeNumber(String encodedValue) throws IOException, UnsupportedOperationException { delegate.writeNumber(encodedValue); }

@Override
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException, UnsupportedOperationException { delegate.writeNumber(encodedValueBuffer, offset, length); }

@Override
public void writeBoolean(boolean state) throws IOException { delegate.writeBoolean(state); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void testWriteStartObjectWithObject() throws Exception
}

// [core#580]
public void testRawValueDelegation() throws Exception
public void testRawValueDelegationWithArray() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(ObjectWriteContext.empty(), w),
Expand All @@ -354,4 +354,23 @@ public void testRawValueDelegation() throws Exception
gen.close();
assertEquals("[1,2]", w.toString());
}

// [core#588]
public void testRawValueDelegationWithObject() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(ObjectWriteContext.empty(), w),
TokenFilter.INCLUDE_ALL, true, true);

gen.writeStartObject();
gen.writeNumberField("f1", 1);
gen.writeFieldName("f2");
gen.writeRawValue(new char[]{'1', '2', '.', '3', '-'}, 0, 4);
gen.writeNumberField("f3", 3);
gen.writeEndObject();

gen.close();
assertEquals(aposToQuotes("{'f1':1,'f2':12.3,'f3':3}"), w.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public void testNumbersAsJSONStrings() throws IOException
{
JsonFactory f = new JsonFactory();
// by default should output numbers as-is:
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, false));
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1]", _writeNumbers(f, true));
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, false));
assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, true));

// but if overridden, quotes as Strings
f = f.rebuild().configure(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS, true)
.build();
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
_writeNumbers(f, false));
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\"]",
assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
_writeNumbers(f, true));
}

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

Expand Down