|
1 | 1 | package com.fasterxml.jackson.core.json;
|
2 | 2 |
|
3 |
| -import java.io.*; |
4 |
| - |
5 |
| -import com.fasterxml.jackson.core.*; |
| 3 | +import com.fasterxml.jackson.core.BaseTest; |
| 4 | +import com.fasterxml.jackson.core.JsonFactory; |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.core.JsonToken; |
| 8 | +import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate; |
| 9 | +import com.fasterxml.jackson.core.filter.JsonPointerBasedFilter; |
| 10 | +import com.fasterxml.jackson.core.io.CharTypes; |
6 | 11 | import com.fasterxml.jackson.core.io.IOContext;
|
7 | 12 | import com.fasterxml.jackson.core.util.BufferRecycler;
|
8 | 13 |
|
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | + |
9 | 16 | public class TestUtf8Generator extends BaseTest
|
10 | 17 | {
|
11 | 18 | private final JsonFactory JSON_F = new JsonFactory();
|
@@ -60,4 +67,51 @@ public void testSurrogatesWithRaw() throws Exception
|
60 | 67 | assertToken(JsonToken.END_ARRAY, jp.nextToken());
|
61 | 68 | jp.close();
|
62 | 69 | }
|
| 70 | + |
| 71 | + public void testFilteringWithEscapedChars() throws Exception |
| 72 | + { |
| 73 | + final String SAMPLE_WITH_QUOTES = "\b\t\f\n\r\"foo\"\u0000"; |
| 74 | + |
| 75 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 76 | + JsonGenerator jgen = JSON_F.createGenerator(out); |
| 77 | + |
| 78 | + FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(jgen, |
| 79 | + new JsonPointerBasedFilter("/escapes"), |
| 80 | + true, // includePath |
| 81 | + false // multipleMatches |
| 82 | + ); |
| 83 | + |
| 84 | + //final String JSON = "{'a':123,'array':[1,2],'escapes':'\b\t\f\n\r\"foo\"\u0000'}"; |
| 85 | + |
| 86 | + gen.writeStartObject(); |
| 87 | + |
| 88 | + gen.writeFieldName("a"); |
| 89 | + gen.writeNumber((int) 123); |
| 90 | + |
| 91 | + gen.writeFieldName("array"); |
| 92 | + gen.writeStartArray(); |
| 93 | + gen.writeNumber((short) 1); |
| 94 | + gen.writeNumber((short) 2); |
| 95 | + gen.writeEndArray(); |
| 96 | + |
| 97 | + gen.writeFieldName("escapes"); |
| 98 | + |
| 99 | + final byte[] raw = SAMPLE_WITH_QUOTES.toString().getBytes("UTF-8"); |
| 100 | + gen.writeUTF8String(raw, 0, raw.length); |
| 101 | + |
| 102 | + gen.writeEndObject(); |
| 103 | + gen.close(); |
| 104 | + |
| 105 | + JsonParser p = JSON_F.createParser(out.toByteArray()); |
| 106 | + |
| 107 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 108 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 109 | + assertEquals("escapes", p.getCurrentName()); |
| 110 | + |
| 111 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 112 | + assertEquals(SAMPLE_WITH_QUOTES, p.getText()); |
| 113 | + assertToken(JsonToken.END_OBJECT, p.nextToken()); |
| 114 | + assertNull(p.nextToken()); |
| 115 | + p.close(); |
| 116 | + } |
63 | 117 | }
|
0 commit comments