@@ -70,13 +70,16 @@ public static Builder builder() {
70
70
* <dl>
71
71
* <dt><prefix>.yaml.node-style</dt>
72
72
* <dd>Equivalent to {@link #nodeStyle(NodeStyle)}</dd>
73
+ * <dt><prefix>.yaml.comments-enabled</dt>
74
+ * <dd>Equivalent to {@link #commentsEnabled(boolean)}</dd>
73
75
* </dl>
74
76
*
75
77
* @since 4.0.0
76
78
*/
77
79
public static final class Builder extends AbstractConfigurationLoader .Builder <Builder , YamlConfigurationLoader > {
78
80
private final DumperOptions options = new DumperOptions ();
79
81
private @ Nullable NodeStyle style ;
82
+ private boolean enableComments ;
80
83
81
84
Builder () {
82
85
this .indent (4 );
@@ -90,6 +93,7 @@ protected void populate(final LoaderOptionSource options) {
90
93
if (declared != null ) {
91
94
this .style = declared ;
92
95
}
96
+ this .enableComments = options .getBoolean (true , "yaml" , "comments-enabled" );
93
97
}
94
98
95
99
/**
@@ -156,6 +160,34 @@ public Builder nodeStyle(final @Nullable NodeStyle style) {
156
160
return this .style ;
157
161
}
158
162
163
+ /**
164
+ * Set whether comment handling is enabled on this loader.
165
+ *
166
+ * <p>When comment handling is enabled, comments will be read from files
167
+ * and written back to files where possible.</p>
168
+ *
169
+ * <p>The default value is {@code true}</p>
170
+ *
171
+ * @param enableComments whether comment handling should be enabled
172
+ * @return this builder (for chaining)
173
+ * @since 4.2.0
174
+ */
175
+ public Builder commentsEnabled (final boolean enableComments ) {
176
+ this .enableComments = enableComments ;
177
+ return this ;
178
+ }
179
+
180
+ /**
181
+ * Get whether comment handling is enabled.
182
+ *
183
+ * @return whether comment handling is enabled
184
+ * @see #commentsEnabled(boolean) for details on comment handling
185
+ * @since 4.2.0
186
+ */
187
+ public boolean commentsEnabled () {
188
+ return this .enableComments ;
189
+ }
190
+
159
191
@ Override
160
192
public YamlConfigurationLoader build () {
161
193
return new YamlConfigurationLoader (this );
@@ -169,12 +201,12 @@ private YamlConfigurationLoader(final Builder builder) {
169
201
super (builder , new CommentHandler [] {CommentHandlers .HASH });
170
202
final LoaderOptions loaderOpts = new LoaderOptions ()
171
203
.setAcceptTabs (true )
172
- .setProcessComments (true );
204
+ .setProcessComments (builder . commentsEnabled () );
173
205
loaderOpts .setCodePointLimit (Integer .MAX_VALUE );
174
206
175
207
final DumperOptions opts = builder .options ;
176
208
opts .setDefaultFlowStyle (NodeStyle .asSnakeYaml (builder .style ));
177
- opts .setProcessComments (true );
209
+ opts .setProcessComments (builder . commentsEnabled () );
178
210
// the constructor needs ConfigurationOptions, which is only available when called (loadInternal)
179
211
this .constructor = ThreadLocal .withInitial (() -> new YamlConstructor (loaderOpts ));
180
212
this .yaml = ThreadLocal .withInitial (() -> new Yaml (this .constructor .get (), new YamlRepresenter (opts ), opts , loaderOpts ));
0 commit comments