Skip to content

Commit 36a4195

Browse files
committed
Backport #587 impl, minor changes, add release notes
1 parent de07f1c commit 36a4195

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ Valery (valery1707@github)
213213
* Contributed #565: Synchronize variants of `JsonGenerator#writeNumberField`
214214
with `JsonGenerator#writeNumber`
215215
(2.11.0)
216+
217+
Volkan Yazıcı (vy@github)
218+
* Contributed #587: Add JsonGenerator#writeNumber(char[], int, int) method
219+
(2.11.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ JSON library.
1818

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

2224
2.10.3 (not released yet)
2325

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

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

1360-
13611360
/**
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}.
1361+
* Overloaded version of {@link #writeNumber(String)} with same semantics
1362+
* but possibly more efficient operation.
13761363
*
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)
1364+
* @since 2.11
13811365
*/
1382-
public abstract void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException;
1366+
public void writeNumber(char[] encodedValueBuffer, int offset, int length) throws IOException {
1367+
writeNumber(new String(encodedValueBuffer, offset, length));
1368+
}
13831369

13841370
/*
13851371
/**********************************************************

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

+24-21
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void testNonFiltering() throws Exception
107107
{
108108
// First, verify non-filtering
109109
StringWriter w = new StringWriter();
110-
JsonGenerator gen = JSON_F.createGenerator(w);
110+
JsonGenerator gen = _createGenerator(w);
111111
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
112112
writeJsonDoc(JSON_F, JSON, gen);
113113
assertEquals(aposToQuotes(
@@ -118,7 +118,7 @@ public void testNonFiltering() throws Exception
118118
public void testSingleMatchFilteringWithoutPath() throws Exception
119119
{
120120
StringWriter w = new StringWriter();
121-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
121+
JsonGenerator gen = new FilteringGeneratorDelegate(_createGenerator(w),
122122
new NameMatchFilter("value"),
123123
false, // includePath
124124
false // multipleMatches
@@ -137,7 +137,7 @@ public void testSingleMatchFilteringWithoutPath() throws Exception
137137
public void testSingleMatchFilteringWithPath() throws Exception
138138
{
139139
StringWriter w = new StringWriter();
140-
JsonGenerator origGen = JSON_F.createGenerator(w);
140+
JsonGenerator origGen = _createGenerator(w);
141141
NameMatchFilter filter = new NameMatchFilter("value");
142142
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
143143
filter,
@@ -159,7 +159,7 @@ public void testSingleMatchFilteringWithPath() throws Exception
159159
public void testSingleMatchFilteringWithPathSkippedArray() throws Exception
160160
{
161161
StringWriter w = new StringWriter();
162-
JsonGenerator origGen = JSON_F.createGenerator(w);
162+
JsonGenerator origGen = _createGenerator(w);
163163
NameMatchFilter filter = new NameMatchFilter("value");
164164
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
165165
filter,
@@ -190,7 +190,7 @@ private void _testSingleMatchFilteringWithPathAlternate1(boolean exclude) throws
190190
TokenFilter tf = exclude
191191
? new NameExcludeFilter(true, "value", "a")
192192
: new NameMatchFilter("value");
193-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
193+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
194194
tf,
195195
true, // includePath
196196
true // multipleMatches
@@ -237,7 +237,7 @@ private void _testSingleMatchFilteringWithPathAlternate1(boolean exclude) throws
237237
public void testSingleMatchFilteringWithPathRawBinary() throws Exception
238238
{
239239
StringWriter w = new StringWriter();
240-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
240+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
241241
new NameMatchFilter("array"),
242242
true, // includePath
243243
false // multipleMatches
@@ -288,7 +288,7 @@ public void testSingleMatchFilteringWithPathRawBinary() throws Exception
288288
public void testMultipleMatchFilteringWithPath1() throws Exception
289289
{
290290
StringWriter w = new StringWriter();
291-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
291+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
292292
new NameMatchFilter("value0", "value2"),
293293
true, /* includePath */ true /* multipleMatches */ );
294294
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
299299
// also try with alternate filter implementation: first including arrays
300300

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

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

319-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
319+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
320320
new NameMatchFilter("array", "b", "value"),
321321
true, true);
322322
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
329329
{
330330
StringWriter w = new StringWriter();
331331

332-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
332+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
333333
new NameMatchFilter("value"),
334334
true, true);
335335
final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
@@ -341,7 +341,7 @@ public void testMultipleMatchFilteringWithPath3() throws Exception
341341
public void testMultipleMatchFilteringWithPath4() throws Exception
342342
{
343343
StringWriter w = new StringWriter();
344-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
344+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
345345
new NameMatchFilter("b0"),
346346
true, true);
347347
final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
@@ -353,15 +353,15 @@ public void testMultipleMatchFilteringWithPath4() throws Exception
353353
public void testIndexMatchWithPath1() throws Exception
354354
{
355355
StringWriter w = new StringWriter();
356-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
356+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
357357
new IndexMatchFilter(1),
358358
true, true);
359359
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':'abc'},'b':true}";
360360
writeJsonDoc(JSON_F, JSON, gen);
361361
assertEquals(aposToQuotes("{'array':[2]}"), w.toString());
362362

363363
w = new StringWriter();
364-
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
364+
gen = new FilteringGeneratorDelegate(_createGenerator(w),
365365
new IndexMatchFilter(0),
366366
true, true);
367367
writeJsonDoc(JSON_F, JSON, gen);
@@ -372,7 +372,7 @@ public void testIndexMatchWithPath1() throws Exception
372372
public void testIndexMatchWithPath2() throws Exception
373373
{
374374
StringWriter w = new StringWriter();
375-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
375+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
376376
new IndexMatchFilter(0,1),
377377
true, true);
378378
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
382382
gen.close();
383383

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

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

402402
w = new StringWriter();
403-
gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
403+
gen = new FilteringGeneratorDelegate(_createGenerator(w),
404404
new IndexMatchFilter(1),
405405
true, true);
406406
JSON = "{'misc':[1,0.25,11234567890]}";
@@ -413,7 +413,7 @@ public void testWriteStartObjectWithObject() throws Exception
413413
{
414414
StringWriter w = new StringWriter();
415415

416-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
416+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
417417
TokenFilter.INCLUDE_ALL,
418418
true, true);
419419

@@ -438,7 +438,7 @@ public void testWriteStartObjectWithObject() throws Exception
438438
public void testRawValueDelegationWithArray() throws Exception
439439
{
440440
StringWriter w = new StringWriter();
441-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
441+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
442442
TokenFilter.INCLUDE_ALL, true, true);
443443

444444
gen.writeStartArray();
@@ -458,7 +458,7 @@ public void testRawValueDelegationWithArray() throws Exception
458458
public void testRawValueDelegationWithObject() throws Exception
459459
{
460460
StringWriter w = new StringWriter();
461-
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(ObjectWriteContext.empty(), w),
461+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
462462
TokenFilter.INCLUDE_ALL, true, true);
463463

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

475+
private JsonGenerator _createGenerator(Writer w) throws IOException {
476+
return JSON_F.createGenerator(w);
477+
}
475478
}

0 commit comments

Comments
 (0)