Skip to content

Commit

Permalink
Merge pull request #280 from tlrx/writeutf8string
Browse files Browse the repository at this point in the history
FilteringGeneratorDelegate.writeUTF8String() should delegate to writeUTF8String()
  • Loading branch information
cowtowncoder committed May 6, 2016
2 parents 931e0e6 + 27aa8b8 commit a8a9beb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public void writeUTF8String(byte[] text, int offset, int length) throws IOExcept
{
// not exact match, but best we can do
if (_checkRawValueWrite()) {
delegate.writeRawUTF8String(text, offset, length);
delegate.writeUTF8String(text, offset, length);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.fasterxml.jackson.core.json;

import java.io.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
import com.fasterxml.jackson.core.filter.JsonPointerBasedFilter;
import com.fasterxml.jackson.core.io.CharTypes;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.util.BufferRecycler;

import java.io.ByteArrayOutputStream;

public class TestUtf8Generator extends BaseTest
{
private final JsonFactory JSON_F = new JsonFactory();
Expand Down Expand Up @@ -60,4 +67,51 @@ public void testSurrogatesWithRaw() throws Exception
assertToken(JsonToken.END_ARRAY, jp.nextToken());
jp.close();
}

public void testFilteringWithEscapedChars() throws Exception
{
final String SAMPLE_WITH_QUOTES = "\b\t\f\n\r\"foo\"\u0000";

ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator jgen = JSON_F.createGenerator(out);

FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(jgen,
new JsonPointerBasedFilter("/escapes"),
true, // includePath
false // multipleMatches
);

//final String JSON = "{'a':123,'array':[1,2],'escapes':'\b\t\f\n\r\"foo\"\u0000'}";

gen.writeStartObject();

gen.writeFieldName("a");
gen.writeNumber((int) 123);

gen.writeFieldName("array");
gen.writeStartArray();
gen.writeNumber((short) 1);
gen.writeNumber((short) 2);
gen.writeEndArray();

gen.writeFieldName("escapes");

final byte[] raw = SAMPLE_WITH_QUOTES.toString().getBytes("UTF-8");
gen.writeUTF8String(raw, 0, raw.length);

gen.writeEndObject();
gen.close();

JsonParser p = JSON_F.createParser(out.toByteArray());

assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("escapes", p.getCurrentName());

assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(SAMPLE_WITH_QUOTES, p.getText());
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());
p.close();
}
}

0 comments on commit a8a9beb

Please sign in to comment.