Skip to content

Commit

Permalink
Backport #587 impl, minor changes, add release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 10, 2020
1 parent de07f1c commit 36a4195
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@ Valery (valery1707@github)
* Contributed #565: Synchronize variants of `JsonGenerator#writeNumberField`
with `JsonGenerator#writeNumber`
(2.11.0)

Volkan Yazıcı (vy@github)
* Contributed #587: Add JsonGenerator#writeNumber(char[], int, int) method
(2.11.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ JSON library.

#565: Synchronize variants of `JsonGenerator#writeNumberField` with `JsonGenerator#writeNumber`
(contributed by valery1707@github)
#587: Add JsonGenerator#writeNumber(char[], int, int) method
(contributed by Volkan Y)

2.10.3 (not released yet)

Expand Down
26 changes: 6 additions & 20 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1357,29 +1357,15 @@ 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}.
* Overloaded version of {@link #writeNumber(String)} with same semantics
* but possibly more efficient operation.
*
* @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)
* @since 2.11
*/
public abstract void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException;
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
writeNumber(new String(encodedValueBuffer, offset, length));
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testNonFiltering() throws Exception
{
// First, verify non-filtering
StringWriter w = new StringWriter();
JsonGenerator gen = JSON_F.createGenerator(w);
JsonGenerator gen = _createGenerator(w);
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
writeJsonDoc(JSON_F, JSON, gen);
assertEquals(aposToQuotes(
Expand All @@ -118,7 +118,7 @@ public void testNonFiltering() throws Exception
public void testSingleMatchFilteringWithoutPath() throws Exception
{
StringWriter w = new StringWriter();
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
JsonGenerator gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("value"),
false, // includePath
false // multipleMatches
Expand All @@ -137,7 +137,7 @@ public void testSingleMatchFilteringWithoutPath() throws Exception
public void testSingleMatchFilteringWithPath() throws Exception
{
StringWriter w = new StringWriter();
JsonGenerator origGen = JSON_F.createGenerator(w);
JsonGenerator origGen = _createGenerator(w);
NameMatchFilter filter = new NameMatchFilter("value");
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
filter,
Expand All @@ -159,7 +159,7 @@ public void testSingleMatchFilteringWithPath() throws Exception
public void testSingleMatchFilteringWithPathSkippedArray() throws Exception
{
StringWriter w = new StringWriter();
JsonGenerator origGen = JSON_F.createGenerator(w);
JsonGenerator origGen = _createGenerator(w);
NameMatchFilter filter = new NameMatchFilter("value");
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
filter,
Expand Down Expand Up @@ -190,7 +190,7 @@ private void _testSingleMatchFilteringWithPathAlternate1(boolean exclude) throws
TokenFilter tf = exclude
? new NameExcludeFilter(true, "value", "a")
: new NameMatchFilter("value");
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
tf,
true, // includePath
true // multipleMatches
Expand Down Expand Up @@ -237,7 +237,7 @@ private void _testSingleMatchFilteringWithPathAlternate1(boolean exclude) throws
public void testSingleMatchFilteringWithPathRawBinary() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("array"),
true, // includePath
false // multipleMatches
Expand Down Expand Up @@ -288,7 +288,7 @@ public void testSingleMatchFilteringWithPathRawBinary() throws Exception
public void testMultipleMatchFilteringWithPath1() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("value0", "value2"),
true, /* includePath */ true /* multipleMatches */ );
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
Expand All @@ -299,14 +299,14 @@ public void testMultipleMatchFilteringWithPath1() throws Exception
// also try with alternate filter implementation: first including arrays

w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameExcludeFilter(true, "ob"), true, true);
writeJsonDoc(JSON_F, JSON, gen);
assertEquals(aposToQuotes("{'a':123,'array':[1,2],'b':true}"), w.toString());

// then excluding them
w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameExcludeFilter(false, "ob"), true, true);
writeJsonDoc(JSON_F, JSON, gen);
assertEquals(aposToQuotes("{'a':123,'b':true}"), w.toString());
Expand All @@ -316,7 +316,7 @@ public void testMultipleMatchFilteringWithPath2() throws Exception
{
StringWriter w = new StringWriter();

FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("array", "b", "value"),
true, true);
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
Expand All @@ -329,7 +329,7 @@ public void testMultipleMatchFilteringWithPath3() throws Exception
{
StringWriter w = new StringWriter();

FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("value"),
true, true);
final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
Expand All @@ -341,7 +341,7 @@ public void testMultipleMatchFilteringWithPath3() throws Exception
public void testMultipleMatchFilteringWithPath4() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new NameMatchFilter("b0"),
true, true);
final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
Expand All @@ -353,15 +353,15 @@ public void testMultipleMatchFilteringWithPath4() throws Exception
public void testIndexMatchWithPath1() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(1),
true, true);
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':'abc'},'b':true}";
writeJsonDoc(JSON_F, JSON, gen);
assertEquals(aposToQuotes("{'array':[2]}"), w.toString());

w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(0),
true, true);
writeJsonDoc(JSON_F, JSON, gen);
Expand All @@ -372,7 +372,7 @@ public void testIndexMatchWithPath1() throws Exception
public void testIndexMatchWithPath2() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(0,1),
true, true);
String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
Expand All @@ -382,7 +382,7 @@ public void testIndexMatchWithPath2() throws Exception
gen.close();

w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(1, 3, 5),
true, true);
JSON = "{'a':123,'misc':[1,2, null, true, false, 'abc', 123],'ob':null,'b':true}";
Expand All @@ -391,7 +391,7 @@ public void testIndexMatchWithPath2() throws Exception
assertEquals(3, gen.getMatchCount());

w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(2,6),
true, true);
JSON = "{'misc':[1,2, null, 0.25, false, 'abc', 11234567890]}";
Expand All @@ -400,7 +400,7 @@ public void testIndexMatchWithPath2() throws Exception
assertEquals(2, gen.getMatchCount());

w = new StringWriter();
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
gen = new FilteringGeneratorDelegate(_createGenerator(w),
new IndexMatchFilter(1),
true, true);
JSON = "{'misc':[1,0.25,11234567890]}";
Expand All @@ -413,7 +413,7 @@ public void testWriteStartObjectWithObject() throws Exception
{
StringWriter w = new StringWriter();

FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
TokenFilter.INCLUDE_ALL,
true, true);

Expand All @@ -438,7 +438,7 @@ public void testWriteStartObjectWithObject() throws Exception
public void testRawValueDelegationWithArray() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
TokenFilter.INCLUDE_ALL, true, true);

gen.writeStartArray();
Expand All @@ -458,7 +458,7 @@ public void testRawValueDelegationWithArray() throws Exception
public void testRawValueDelegationWithObject() throws Exception
{
StringWriter w = new StringWriter();
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(ObjectWriteContext.empty(), w),
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
TokenFilter.INCLUDE_ALL, true, true);

gen.writeStartObject();
Expand All @@ -472,4 +472,7 @@ public void testRawValueDelegationWithObject() throws Exception
assertEquals(aposToQuotes("{'f1':1,'f2':12.3,'f3':3}"), w.toString());
}

private JsonGenerator _createGenerator(Writer w) throws IOException {
return JSON_F.createGenerator(w);
}
}

0 comments on commit 36a4195

Please sign in to comment.