|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.*; |
| 6 | +import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate; |
| 7 | +import com.fasterxml.jackson.core.filter.TokenFilter; |
| 8 | +import com.fasterxml.jackson.core.util.JsonGeneratorDelegate; |
| 9 | + |
| 10 | +// for [core#609] |
| 11 | +public class TokenFilter609Test |
| 12 | + extends com.fasterxml.jackson.core.BaseTest |
| 13 | +{ |
| 14 | + static class NullExcludingTokenFilter extends TokenFilter { |
| 15 | + private static final NullExcludingTokenFilter INSTANCE = |
| 16 | + new NullExcludingTokenFilter(); |
| 17 | + |
| 18 | + @Override |
| 19 | + public boolean includeNull() { |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + static class StringTruncatingGeneratorDelegate |
| 26 | + extends JsonGeneratorDelegate |
| 27 | + { |
| 28 | + private final int maxStringLength; |
| 29 | + |
| 30 | + private StringTruncatingGeneratorDelegate( |
| 31 | + JsonGenerator jsonGenerator, |
| 32 | + int maxStringLength) { |
| 33 | + super(jsonGenerator); |
| 34 | + this.maxStringLength = maxStringLength; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void writeString(String text) throws IOException { |
| 39 | + if (text == null) { |
| 40 | + writeNull(); |
| 41 | + } else if (maxStringLength <= 0 || maxStringLength >= text.length()) { |
| 42 | + super.writeString(text); |
| 43 | + } else { |
| 44 | +// super.writeString(text.substring(0, maxStringLength)); |
| 45 | + StringReader textReader = new StringReader(text); |
| 46 | + super.writeString(textReader, maxStringLength); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void writeFieldName(String name) throws IOException { |
| 52 | + if (maxStringLength <= 0 || maxStringLength >= name.length()) { |
| 53 | + super.writeFieldName(name); |
| 54 | + } else { |
| 55 | + String truncatedName = name.substring(0, maxStringLength); |
| 56 | + super.writeFieldName(truncatedName); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + // for [core#609]: will pass in 2.10 for some cases |
| 63 | + @SuppressWarnings("resource") |
| 64 | + public void testIssue609() throws Exception |
| 65 | + { |
| 66 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 67 | + JsonGenerator g = createGenerator(outputStream); |
| 68 | + g = new FilteringGeneratorDelegate( |
| 69 | + g, NullExcludingTokenFilter.INSTANCE, true, true); |
| 70 | + int maxStringLength = 10; |
| 71 | + g = new StringTruncatingGeneratorDelegate( |
| 72 | + g, maxStringLength); |
| 73 | + g.writeStartObject(); |
| 74 | + g.writeFieldName("message"); |
| 75 | + g.writeString("1234567890!"); |
| 76 | + g.writeEndObject(); |
| 77 | + g.close(); |
| 78 | + |
| 79 | + String json = outputStream.toString("US-ASCII"); |
| 80 | + assertEquals("{\"message\":\"1234567890\"}", json); |
| 81 | + } |
| 82 | +} |
0 commit comments