Skip to content

Commit

Permalink
Clean up #108 (quote writing).
Browse files Browse the repository at this point in the history
Defaults should not change. Formatting. No quoutes must be 0, not ' '.
  • Loading branch information
NathanSweet committed Mar 25, 2019
1 parent b2ccca4 commit 772d3ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
35 changes: 15 additions & 20 deletions src/com/esotericsoftware/yamlbeans/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
10 changes: 6 additions & 4 deletions src/com/esotericsoftware/yamlbeans/YamlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 772d3ed

Please sign in to comment.