From 36a4195716756a4196715dce487f251f9711b436 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 10 Jan 2020 10:15:14 -0800 Subject: [PATCH] Backport #587 impl, minor changes, add release notes --- release-notes/CREDITS-2.x | 4 ++ release-notes/VERSION-2.x | 2 + .../fasterxml/jackson/core/JsonGenerator.java | 26 +++-------- .../filter/BasicGeneratorFilteringTest.java | 45 ++++++++++--------- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 5e6c24cd74..8b50c90d44 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -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) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 15f071b0d0..44ac81e7f7 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -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) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index 22a090d311..214a5d996c 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -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. - *

- * 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)); + } /* /********************************************************** diff --git a/src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java b/src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java index 7e4661c1d1..ab3e395985 100644 --- a/src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java +++ b/src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java @@ -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( @@ -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 @@ -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, @@ -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, @@ -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 @@ -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 @@ -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}"; @@ -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()); @@ -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}"; @@ -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}"; @@ -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}"; @@ -353,7 +353,7 @@ 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}"; @@ -361,7 +361,7 @@ public void testIndexMatchWithPath1() throws Exception 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); @@ -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}"; @@ -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}"; @@ -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]}"; @@ -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]}"; @@ -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); @@ -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(); @@ -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(); @@ -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); + } }