Skip to content

Commit 5d479e8

Browse files
committed
3.0 compatibility for toml module
This reverts commit fefd694.
1 parent 58239b4 commit 5d479e8

File tree

15 files changed

+380
-355
lines changed

15 files changed

+380
-355
lines changed

toml/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>com.fasterxml.jackson.dataformat</groupId>
1111
<artifactId>jackson-dataformats-text</artifactId>
12-
<version>2.13.0-SNAPSHOT</version>
12+
<version>3.0.0-SNAPSHOT</version>
1313
</parent>
1414
<artifactId>jackson-dataformat-toml</artifactId>
1515
<packaging>bundle</packaging>

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/JacksonTomlParseException.java

-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.core.JsonParser;
55
import com.fasterxml.jackson.core.exc.StreamReadException;
66
import com.fasterxml.jackson.core.io.ContentReference;
7-
import com.fasterxml.jackson.core.util.RequestPayload;
87

98
public class JacksonTomlParseException extends StreamReadException {
109
private JacksonTomlParseException(JsonParser p, String msg, JsonLocation loc) {
@@ -15,18 +14,6 @@ private JacksonTomlParseException(String msg, JsonLocation loc, Throwable rootCa
1514
super(msg, loc, rootCause);
1615
}
1716

18-
@Override
19-
public JacksonTomlParseException withParser(JsonParser p) {
20-
this._processor = p;
21-
return this;
22-
}
23-
24-
@Override
25-
public JacksonTomlParseException withRequestPayload(RequestPayload p) {
26-
this._requestPayload = p;
27-
return this;
28-
}
29-
3017
static class ErrorContext {
3118
private final ContentReference ContentReference;
3219
private final JsonParser parser;

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/Parser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static ObjectNode parse(JacksonTomlParseException.ErrorContext errorConte
3737
return new Parser(errorContext, options, reader).parse();
3838
}
3939

40-
private TomlToken peek() throws JacksonTomlParseException {
40+
private TomlToken peek() {
4141
TomlToken here = this.next;
4242
if (here == null) throw errorContext.atPosition(lexer).generic("Premature end of file");
4343
return here;
@@ -382,7 +382,7 @@ private void parseKeyVal(TomlObjectNode target, int nextState) throws IOExceptio
382382
fieldRef.object.set(fieldRef.key, value);
383383
}
384384

385-
private TomlObjectNode getOrCreateObject(ObjectNode node, String field) throws JacksonTomlParseException {
385+
private TomlObjectNode getOrCreateObject(ObjectNode node, String field) {
386386
JsonNode existing = node.get(field);
387387
if (existing == null) {
388388
return (TomlObjectNode) node.putObject(field);
@@ -393,7 +393,7 @@ private TomlObjectNode getOrCreateObject(ObjectNode node, String field) throws J
393393
}
394394
}
395395

396-
private TomlArrayNode getOrCreateArray(ObjectNode node, String field) throws JacksonTomlParseException {
396+
private TomlArrayNode getOrCreateArray(ObjectNode node, String field) {
397397
JsonNode existing = node.get(field);
398398
if (existing == null) {
399399
return (TomlArrayNode) node.putArray(field);
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
package com.fasterxml.jackson.dataformat.toml;
22

3-
import com.fasterxml.jackson.core.FormatFeature;
4-
import com.fasterxml.jackson.core.FormatSchema;
5-
import com.fasterxml.jackson.core.JacksonException;
6-
import com.fasterxml.jackson.core.JsonFactory;
7-
import com.fasterxml.jackson.core.JsonGenerator;
8-
import com.fasterxml.jackson.core.JsonParseException;
9-
import com.fasterxml.jackson.core.JsonParser;
10-
import com.fasterxml.jackson.core.ObjectCodec;
11-
import com.fasterxml.jackson.core.StreamReadFeature;
12-
import com.fasterxml.jackson.core.Version;
13-
import com.fasterxml.jackson.core.format.InputAccessor;
14-
import com.fasterxml.jackson.core.format.MatchStrength;
3+
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.base.TextualTSFactory;
155
import com.fasterxml.jackson.core.io.IOContext;
166
import com.fasterxml.jackson.core.io.UTF8Writer;
177
import com.fasterxml.jackson.databind.node.ObjectNode;
188
import com.fasterxml.jackson.databind.node.TreeTraversingParser;
19-
import java.io.ByteArrayInputStream;
20-
import java.io.CharArrayReader;
21-
import java.io.IOException;
22-
import java.io.InputStream;
23-
import java.io.InputStreamReader;
24-
import java.io.OutputStream;
25-
import java.io.Reader;
26-
import java.io.Writer;
9+
10+
import java.io.*;
2711
import java.nio.charset.StandardCharsets;
2812

29-
public final class TomlFactory extends JsonFactory {
13+
public final class TomlFactory extends TextualTSFactory {
3014

3115
public final static String FORMAT_NAME_TOML = "toml";
3216

@@ -42,28 +26,18 @@ public final class TomlFactory extends JsonFactory {
4226
*/
4327
final static int DEFAULT_TOML_GENERATOR_FEATURE_FLAGS = 0;
4428

45-
/*
46-
/**********************************************************************
47-
/* Configuration
48-
/**********************************************************************
49-
*/
50-
51-
protected int _tomlParserFeatures = DEFAULT_TOML_PARSER_FEATURE_FLAGS;
52-
protected int _tomlGeneratorFeatures = DEFAULT_TOML_GENERATOR_FEATURE_FLAGS;
53-
5429
/*
5530
/**********************************************************************
5631
/* Factory construction, configuration
5732
/**********************************************************************
5833
*/
5934

6035
public TomlFactory() {
36+
super(DEFAULT_TOML_PARSER_FEATURE_FLAGS, DEFAULT_TOML_GENERATOR_FEATURE_FLAGS);
6137
}
6238

63-
TomlFactory(TomlFactory src, ObjectCodec oc) {
64-
super(src, oc);
65-
_tomlGeneratorFeatures = src._tomlGeneratorFeatures;
66-
_tomlParserFeatures = src._tomlParserFeatures;
39+
TomlFactory(TomlFactory src) {
40+
super(src);
6741
}
6842

6943
/**
@@ -72,9 +46,7 @@ public TomlFactory() {
7246
* @since 3.0
7347
*/
7448
TomlFactory(TomlFactoryBuilder b) {
75-
super(b, false);
76-
_tomlGeneratorFeatures = b._formatGeneratorFeatures;
77-
_tomlParserFeatures = b._formatParserFeatures;
49+
super(b);
7850
}
7951

8052
@Override
@@ -92,8 +64,15 @@ public static TomlFactoryBuilder builder() {
9264

9365
@Override
9466
public TomlFactory copy() {
95-
_checkInvalidCopy(TomlFactory.class);
96-
return new TomlFactory(this, null);
67+
return new TomlFactory(this);
68+
}
69+
70+
/**
71+
* Instances are immutable so just return `this`
72+
*/
73+
@Override
74+
public TokenStreamFactory snapshot() {
75+
return this;
9776
}
9877

9978
/*
@@ -149,60 +128,13 @@ public Class<? extends FormatFeature> getFormatWriteFeatureType() {
149128
}
150129

151130
@Override
152-
public MatchStrength hasFormat(InputAccessor acc) throws IOException {
153-
return MatchStrength.INCONCLUSIVE;
154-
}
155-
156-
157-
158-
/*
159-
/**********************************************************
160-
/* Configuration, parser settings
161-
/**********************************************************
162-
*/
163-
164-
/**
165-
* Method for enabling or disabling specified parser feature
166-
* (check {@link TomlReadFeature} for list of features)
167-
*/
168-
public final TomlFactory configure(TomlReadFeature f, boolean state)
169-
{
170-
if (state) {
171-
enable(f);
172-
} else {
173-
disable(f);
174-
}
175-
return this;
176-
}
177-
178-
/**
179-
* Method for enabling specified parser feature
180-
* (check {@link TomlReadFeature} for list of features)
181-
*/
182-
public TomlFactory enable(TomlReadFeature f) {
183-
_tomlParserFeatures |= f.getMask();
184-
return this;
185-
}
186-
187-
/**
188-
* Method for disabling specified parser features
189-
* (check {@link TomlReadFeature} for list of features)
190-
*/
191-
public TomlFactory disable(TomlReadFeature f) {
192-
_tomlParserFeatures &= ~f.getMask();
193-
return this;
194-
}
195-
196-
/**
197-
* Checked whether specified parser feature is enabled.
198-
*/
199-
public final boolean isEnabled(TomlReadFeature f) {
200-
return (_tomlParserFeatures & f.getMask()) != 0;
131+
public int getFormatReadFeatures() {
132+
return _formatReadFeatures;
201133
}
202134

203135
@Override
204-
public int getFormatParserFeatures() {
205-
return _tomlParserFeatures;
136+
public int getFormatWriteFeatures() {
137+
return _formatWriteFeatures;
206138
}
207139

208140
/*
@@ -212,25 +144,30 @@ public int getFormatParserFeatures() {
212144
*/
213145

214146
@Override
215-
public JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
147+
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, InputStream in) throws JacksonException {
216148
// "A TOML file must be a valid UTF-8 encoded Unicode document."
217-
return _createParser(new InputStreamReader(in, StandardCharsets.UTF_8), ctxt);
149+
return _createParser(readCtxt, ctxt, new InputStreamReader(in, StandardCharsets.UTF_8));
218150
}
219151

220152
@Override
221-
public JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
222-
ObjectNode node = parse(ctxt, r);
223-
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
153+
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, Reader r) throws JacksonException {
154+
ObjectNode node = parse(readCtxt, ctxt, r);
155+
return new TreeTraversingParser(node, readCtxt);
224156
}
225157

226158
@Override
227-
public JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
228-
return _createParser(new ByteArrayInputStream(data, offset, len), ctxt);
159+
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, byte[] data, int offset, int len) throws JacksonException {
160+
return _createParser(readCtxt, ctxt, new ByteArrayInputStream(data, offset, len));
229161
}
230162

231163
@Override
232-
protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt, boolean recyclable) throws IOException {
233-
return _createParser(new CharArrayReader(data, offset, len), ctxt);
164+
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, char[] data, int offset, int len, boolean recyclable) throws JacksonException {
165+
return _createParser(readCtxt, ctxt, new CharArrayReader(data, offset, len));
166+
}
167+
168+
@Override
169+
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, DataInput input) throws JacksonException {
170+
return _unsupported();
234171
}
235172

236173
/*
@@ -240,15 +177,19 @@ protected JsonParser _createParser(char[] data, int offset, int len, IOContext c
240177
*/
241178

242179
@Override
243-
public JsonGenerator createGenerator(Writer out) throws JacksonException {
244-
IOContext ctxt = _createContext(_createContentReference(out), false);
245-
return new TomlGenerator(ctxt, _tomlGeneratorFeatures, _objectCodec, out);
180+
protected JsonGenerator _createGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt, Writer out) throws JacksonException {
181+
return new TomlGenerator(writeCtxt, ioCtxt, writeCtxt.getStreamWriteFeatures(_streamWriteFeatures), out);
246182
}
247183

248184
@Override
249-
public JsonGenerator createGenerator(OutputStream out) throws JacksonException {
250-
IOContext ctxt = _createContext(_createContentReference(out), false);
251-
return new TomlGenerator(ctxt, _tomlGeneratorFeatures, _objectCodec, new UTF8Writer(ctxt, out));
185+
protected JsonGenerator _createUTF8Generator(ObjectWriteContext writeCtxt, IOContext ioCtxt, OutputStream out) throws JacksonException {
186+
return _createGenerator(writeCtxt, ioCtxt, new UTF8Writer(ioCtxt, out));
187+
}
188+
189+
@Override
190+
protected Writer _createWriter(IOContext ioCtxt, OutputStream out, JsonEncoding enc) throws JacksonException {
191+
// "A TOML file must be a valid UTF-8 encoded Unicode document."
192+
return new UTF8Writer(ioCtxt, out);
252193
}
253194

254195
/*
@@ -257,14 +198,19 @@ public JsonGenerator createGenerator(OutputStream out) throws JacksonException {
257198
/**********************************************************************
258199
*/
259200

260-
private ObjectNode parse(IOContext ctxt, Reader r0) throws IOException {
201+
private ObjectNode parse(ObjectReadContext readCtxt, IOContext ctxt, Reader r0) {
261202
JacksonTomlParseException.ErrorContext errorContext = new JacksonTomlParseException.ErrorContext(ctxt.contentReference(), null);
262-
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
263-
try (Reader r = r0) {
264-
return Parser.parse(errorContext, _tomlParserFeatures, r);
203+
int readFeatures = readCtxt.getFormatReadFeatures(DEFAULT_TOML_PARSER_FEATURE_FLAGS);
204+
try {
205+
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
206+
try (Reader r = r0) {
207+
return Parser.parse(errorContext, readFeatures, r);
208+
}
209+
} else {
210+
return Parser.parse(errorContext, readFeatures, r0);
265211
}
266-
} else {
267-
return Parser.parse(errorContext, _tomlParserFeatures, r0);
212+
} catch (IOException e) {
213+
throw _wrapIOFailure(e);
268214
}
269215
}
270216
}

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/TomlFactoryBuilder.java

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package com.fasterxml.jackson.dataformat.toml;
22

3-
import com.fasterxml.jackson.core.TSFBuilder;
3+
import com.fasterxml.jackson.core.base.DecorableTSFactory;
44

55
/**
6-
* {@link com.fasterxml.jackson.core.TSFBuilder}
6+
* {@link com.fasterxml.jackson.core.TokenStreamFactory.TSFBuilder}
77
* implementation for constructing {@link TomlFactory}
88
* instances.
99
*
1010
* @since 3.0
1111
*/
12-
public class TomlFactoryBuilder extends TSFBuilder<TomlFactory, TomlFactoryBuilder> {/*
13-
/**********************************************************
14-
/* Configuration
15-
/**********************************************************
16-
*/
17-
18-
protected int _formatParserFeatures = TomlFactory.DEFAULT_TOML_PARSER_FEATURE_FLAGS;
19-
protected int _formatGeneratorFeatures = TomlFactory.DEFAULT_TOML_GENERATOR_FEATURE_FLAGS;
20-
21-
/*
22-
/**********************************************************
23-
/* Life cycle
24-
/**********************************************************
25-
*/
26-
12+
public class TomlFactoryBuilder extends DecorableTSFactory.DecorableTSFBuilder<TomlFactory, TomlFactoryBuilder> {
2713
TomlFactoryBuilder() {
28-
super();
14+
super(TomlFactory.DEFAULT_TOML_PARSER_FEATURE_FLAGS, TomlFactory.DEFAULT_TOML_GENERATOR_FEATURE_FLAGS);
2915
}
3016

3117
TomlFactoryBuilder(TomlFactory base) {
@@ -44,27 +30,27 @@ public TomlFactory build() {
4430
*/
4531

4632
public TomlFactoryBuilder enable(TomlReadFeature f) {
47-
_formatParserFeatures |= f.getMask();
33+
_formatReadFeatures |= f.getMask();
4834
return this;
4935
}
5036

5137
public TomlFactoryBuilder enable(TomlReadFeature first, TomlReadFeature... other) {
52-
_formatParserFeatures |= first.getMask();
38+
_formatReadFeatures |= first.getMask();
5339
for (TomlReadFeature f : other) {
54-
_formatParserFeatures |= f.getMask();
40+
_formatReadFeatures |= f.getMask();
5541
}
5642
return this;
5743
}
5844

5945
public TomlFactoryBuilder disable(TomlReadFeature f) {
60-
_formatParserFeatures &= ~f.getMask();
46+
_formatReadFeatures &= ~f.getMask();
6147
return this;
6248
}
6349

6450
public TomlFactoryBuilder disable(TomlReadFeature first, TomlReadFeature... other) {
65-
_formatParserFeatures &= ~first.getMask();
51+
_formatReadFeatures &= ~first.getMask();
6652
for (TomlReadFeature f : other) {
67-
_formatParserFeatures &= ~f.getMask();
53+
_formatReadFeatures &= ~f.getMask();
6854
}
6955
return this;
7056
}

0 commit comments

Comments
 (0)