Skip to content

Commit 860a883

Browse files
committed
and bit more work for #229...
1 parent dadb47b commit 860a883

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactory.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.core.format.InputAccessor;
1111
import com.fasterxml.jackson.core.format.MatchStrength;
1212
import com.fasterxml.jackson.core.io.IOContext;
13+
import com.fasterxml.jackson.dataformat.yaml.util.StringQuotingChecker;
1314

1415
@SuppressWarnings("resource")
1516
public class YAMLFactory extends JsonFactory
@@ -50,13 +51,21 @@ public class YAMLFactory extends JsonFactory
5051

5152
protected int _yamlGeneratorFeatures = DEFAULT_YAML_GENERATOR_FEATURE_FLAGS;
5253

54+
/**
55+
* Helper object used to determine whether property names, String values
56+
* must be quoted or not.
57+
*
58+
* @since 2.12
59+
*/
60+
protected StringQuotingChecker _quotingChecker;
61+
62+
protected DumperOptions.Version _version;
63+
5364
/*
5465
/**********************************************************************
5566
/* Factory construction, configuration
5667
/**********************************************************************
5768
*/
58-
59-
protected DumperOptions.Version _version;
6069

6170
/**
6271
* Default constructor used to create factory instances.
@@ -75,22 +84,23 @@ public YAMLFactory(ObjectCodec oc)
7584
super(oc);
7685
_yamlParserFeatures = DEFAULT_YAML_PARSER_FEATURE_FLAGS;
7786
_yamlGeneratorFeatures = DEFAULT_YAML_GENERATOR_FEATURE_FLAGS;
78-
/* 26-Jul-2013, tatu: Seems like we should force output as 1.1 but
79-
* that adds version declaration which looks ugly...
80-
*/
87+
// 26-Jul-2013, tatu: Seems like we should force output as 1.1 but
88+
// that adds version declaration which looks ugly...
8189
//_version = DumperOptions.Version.V1_1;
8290
_version = null;
91+
_quotingChecker = StringQuotingChecker.Default.instance();
8392
}
8493

8594
/**
86-
* @since 2.2.1
95+
* @since 2.2
8796
*/
8897
public YAMLFactory(YAMLFactory src, ObjectCodec oc)
8998
{
9099
super(src, oc);
91100
_version = src._version;
92101
_yamlParserFeatures = src._yamlParserFeatures;
93102
_yamlGeneratorFeatures = src._yamlGeneratorFeatures;
103+
_quotingChecker = src._quotingChecker;
94104
}
95105

96106
/**
@@ -102,6 +112,7 @@ protected YAMLFactory(YAMLFactoryBuilder b)
102112
{
103113
super(b, false);
104114
_yamlGeneratorFeatures = b.formatGeneratorFeaturesMask();
115+
_quotingChecker = b.stringQuotingChecker();
105116
}
106117

107118
@Override

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactoryBuilder.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.fasterxml.jackson.dataformat.yaml;
22

33
import com.fasterxml.jackson.core.TSFBuilder;
4+
import com.fasterxml.jackson.dataformat.yaml.util.StringQuotingChecker;
45

56
/**
67
* {@link com.fasterxml.jackson.core.TSFBuilder}
78
* implementation for constructing {@link YAMLFactory}
89
* instances.
9-
*
10-
* @since 3.0
1110
*/
1211
public class YAMLFactoryBuilder extends TSFBuilder<YAMLFactory, YAMLFactoryBuilder>
1312
{
@@ -24,6 +23,14 @@ public class YAMLFactoryBuilder extends TSFBuilder<YAMLFactory, YAMLFactoryBuild
2423
*/
2524
protected int _formatGeneratorFeatures;
2625

26+
/**
27+
* Helper object used to determine whether property names, String values
28+
* must be quoted or not.
29+
*
30+
* @since 2.12
31+
*/
32+
protected StringQuotingChecker _quotingChecker;
33+
2734
/*
2835
/**********************************************************
2936
/* Life cycle
@@ -37,6 +44,7 @@ protected YAMLFactoryBuilder() {
3744
public YAMLFactoryBuilder(YAMLFactory base) {
3845
super(base);
3946
_formatGeneratorFeatures = base._yamlGeneratorFeatures;
47+
_quotingChecker = base._quotingChecker;
4048
}
4149

4250
// // // Parser features NOT YET defined
@@ -45,39 +53,51 @@ public YAMLFactoryBuilder(YAMLFactory base) {
4553

4654
public YAMLFactoryBuilder enable(YAMLGenerator.Feature f) {
4755
_formatGeneratorFeatures |= f.getMask();
48-
return _this();
56+
return this;
4957
}
5058

5159
public YAMLFactoryBuilder enable(YAMLGenerator.Feature first, YAMLGenerator.Feature... other) {
5260
_formatGeneratorFeatures |= first.getMask();
5361
for (YAMLGenerator.Feature f : other) {
5462
_formatGeneratorFeatures |= f.getMask();
5563
}
56-
return _this();
64+
return this;
5765
}
5866

5967
public YAMLFactoryBuilder disable(YAMLGenerator.Feature f) {
6068
_formatGeneratorFeatures &= ~f.getMask();
61-
return _this();
69+
return this;
6270
}
6371

6472
public YAMLFactoryBuilder disable(YAMLGenerator.Feature first, YAMLGenerator.Feature... other) {
6573
_formatGeneratorFeatures &= ~first.getMask();
6674
for (YAMLGenerator.Feature f : other) {
6775
_formatGeneratorFeatures &= ~f.getMask();
6876
}
69-
return _this();
77+
return this;
7078
}
7179

7280
public YAMLFactoryBuilder configure(YAMLGenerator.Feature f, boolean state) {
7381
return state ? enable(f) : disable(f);
7482
}
7583

84+
public YAMLFactoryBuilder stringQuotingChecker(StringQuotingChecker sqc) {
85+
_quotingChecker = sqc;
86+
return this;
87+
}
88+
7689
// // // Accessors
7790

7891
// public int formatParserFeaturesMask() { return _formatParserFeatures; }
7992
public int formatGeneratorFeaturesMask() { return _formatGeneratorFeatures; }
8093

94+
public StringQuotingChecker stringQuotingChecker() {
95+
if (_quotingChecker != null) {
96+
return _quotingChecker;
97+
}
98+
return StringQuotingChecker.Default.instance();
99+
}
100+
81101
@Override
82102
public YAMLFactory build() {
83103
return new YAMLFactory(this);

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java

+6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* @since 2.12
1515
*/
1616
public abstract class StringQuotingChecker
17+
implements java.io.Serializable
1718
{
19+
private static final long serialVersionUID = 1L;
20+
1821
/* As per <a href="https://yaml.org/type/bool.html">YAML Spec</a> there are a few
1922
* aliases for booleans, and we better quote such values as keys; although Jackson
2023
* itself has no problems dealing with them, some other tools do have.
@@ -165,7 +168,10 @@ protected boolean valueHasQuotableChar(String inputStr)
165168
*/
166169
public final static class Default
167170
extends StringQuotingChecker
171+
implements java.io.Serializable
168172
{
173+
private static final long serialVersionUID = 1L;
174+
169175
private final static Default INSTANCE = new Default();
170176

171177
public Default() { }

0 commit comments

Comments
 (0)