Skip to content

Commit

Permalink
Remove invalid characters from field names in YAML data.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Mar 12, 2017
1 parent c902cdd commit ecb417e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/com/esotericsoftware/yamlbeans/Beans.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,20 @@ static public Set<Property> getProperties (Class type, boolean beanProperties, b
return properties;
}

static private String toJavaIdentifier (String name) {
StringBuilder buffer = new StringBuilder();
for (int i = 0, n = name.length(); i < n; i++) {
char c = name.charAt(i);
if (Character.isJavaIdentifierPart(c)) buffer.append(c);
}
return buffer.toString();
}

static public Property getProperty (Class type, String name, boolean beanProperties, boolean privateFields,
YamlConfig config) {
if (type == null) throw new IllegalArgumentException("type cannot be null.");
if (name == null || name.length() == 0) throw new IllegalArgumentException("name cannot be null or empty.");
name = name.replace(" ", "");
name = toJavaIdentifier(name);

if (beanProperties) {
DeferredConstruction deferredConstruction = getDeferredConstruction(type, config);
Expand Down Expand Up @@ -210,7 +219,7 @@ static private ArrayList<Field> getAllFields (Class type) {
nextClass = nextClass.getSuperclass();
}
ArrayList<Field> allFields = new ArrayList();
for(int i = classes.size() - 1; i >= 0; i--) {
for (int i = classes.size() - 1; i >= 0; i--) {
Collections.addAll(allFields, classes.get(i).getDeclaredFields());
}
return allFields;
Expand Down

0 comments on commit ecb417e

Please sign in to comment.