From 772d3ed2f1e38c39565f5cbf18c2a7807218b50b Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 25 Mar 2019 09:35:33 +0100 Subject: [PATCH] Clean up #108 (quote writing). Defaults should not change. Formatting. No quoutes must be 0, not ' '. --- .../yamlbeans/YamlConfig.java | 35 ++++++++----------- .../yamlbeans/YamlWriter.java | 10 +++--- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/com/esotericsoftware/yamlbeans/YamlConfig.java b/src/com/esotericsoftware/yamlbeans/YamlConfig.java index ff3a3bc..42c612e 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlConfig.java +++ b/src/com/esotericsoftware/yamlbeans/YamlConfig.java @@ -139,7 +139,7 @@ static public class WriteConfig { boolean autoAnchor = true; boolean keepBeanPropertyOrder = false; WriteClassName writeClassName = WriteClassName.AUTO; - QuoteCharEnum quoteChar = QuoteCharEnum.SINGLEQUOTE; + Quote quote = Quote.NONE; EmitterConfig emitterConfig = new EmitterConfig(); WriteConfig () { @@ -226,21 +226,11 @@ public void setWriteClassname (WriteClassName write) { writeClassName = write; } - /** define the quote character you'd like to use, when writing YAML output. */ - public void setQuoteChar (QuoteCharEnum _qc) { - this.quoteChar = _qc; - } - - /** Utility function: primarily for use within YamlWriter, to get the quote-character */ - public char getQuoteChar() { - switch (this.quoteChar ) { - case NONE: return ' '; - case SINGLEQUOTE: return '\''; - case DOUBLEQUOTE: return '\"'; - default: return '\''; - } - } - } + /** The type of quotes to use when writing YAML output. */ + public void setQuoteChar (Quote quote) { + this.quote = quote; + } + } static public class ReadConfig { Version defaultVersion = new Version(1, 1); @@ -309,8 +299,13 @@ public static enum WriteClassName { ALWAYS, NEVER, AUTO } - public static enum QuoteCharEnum { - NONE, SINGLEQUOTE, DOUBLEQUOTE - } - + public static enum Quote { + NONE('\0'), SINGLE('\''), DOUBLE('"'); + + char c; + + Quote (char c) { + this.c = c; + } + } } diff --git a/src/com/esotericsoftware/yamlbeans/YamlWriter.java b/src/com/esotericsoftware/yamlbeans/YamlWriter.java index 6026b3c..3643fe7 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlWriter.java +++ b/src/com/esotericsoftware/yamlbeans/YamlWriter.java @@ -151,7 +151,8 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla if (unknownType) fieldClass = valueClass; if (object instanceof Enum) { - emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, ((Enum)object).name(), this.config.writeConfig.getQuoteChar() )); + emitter.emit( + new ScalarEvent(null, null, new boolean[] {true, true}, ((Enum)object).name(), this.config.writeConfig.quote.c)); return; } @@ -203,7 +204,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla if (valueClass == String.class) { try { Float.parseFloat(string); - style = this.config.writeConfig.getQuoteChar(); // = '\''; + style = this.config.writeConfig.quote.c; } catch (NumberFormatException ignored) { } } @@ -240,7 +241,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla char style = 0; try { Float.parseFloat(string); - style = this.config.writeConfig.getQuoteChar(); // = '\''; + style = this.config.writeConfig.quote.c; } catch (NumberFormatException ignored) { } writeValue(key, null, null, null); @@ -291,7 +292,8 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla if (propertyValue == null && prototypeValue == null) continue; if (propertyValue != null && prototypeValue != null && prototypeValue.equals(propertyValue)) continue; } - emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, property.getName(), this.config.writeConfig.getQuoteChar() )); + emitter.emit( + new ScalarEvent(null, null, new boolean[] {true, true}, property.getName(), this.config.writeConfig.quote.c)); Class propertyElementType = config.propertyToElementType.get(property); Class propertyDefaultType = config.propertyToDefaultType.get(property); writeValue(propertyValue, property.getType(), propertyElementType, propertyDefaultType);