A Java library for mutating JSON objects.
For the sake of simplicity, we will explain how to add a new mutation operator with an example. This example corresponds to the addition of a String mutation operator, but it should be replicable for any other data type (integer, boolean, etc.):
Create a new constant with the name of the mutation in the class OperatorNames, like this:
public class OperatorNames {
public static final String MUTATE = "mutate";
public static final String CHANGE_TYPE = "changeType";
public static final String REPLACE = "replace"; // NEWLY ADDED LINE
}
Add a weight to that mutation operator in the config.properties file. Note that you must use the same name for the property that you used in the constant previously defined (in this case, "replace"):
operator.value.string.weight.replace=0.1
Create a new class for the mutation operator under the package es.us.isa.jsonmutator.value.string0.operator
. Note
that this class must necessarily implement a constructor (where at least super()
is called and the weight
attribute is set) and the mutate method:
public class StringReplacementOperator extends AbstractOperator {
private int minLength; // Minimum length of the randomly generated string
private int maxLength; // Maximum length of the randomly generated string
public StringReplacementOperator() {
super();
weight = Float.parseFloat(readProperty("operator.value.string.weight." + OperatorNames.REPLACE));
minLength = Integer.parseInt(readProperty("operator.value.string.length.min"));
maxLength = Integer.parseInt(readProperty("operator.value.string.length.max"));
}
public Object mutate(Object stringObject) {
return RandomStringUtils.random(rand1.nextInt(minLength, maxLength), true, true);
}
}
Add the mutation operator to the map of operators in the constructor of the Mutator class (in this case, StringMutator):
public class StringMutator extends AbstractMutator {
public StringMutator() {
super();
prob = Float.parseFloat(readProperty("operator.value.string.prob"));
operators.put(OperatorNames.MUTATE, new StringMutationOperator());
operators.put(OperatorNames.CHANGE_TYPE, new ChangeTypeOperator(String.class));
operators.put(OperatorNames.REPLACE, new StringReplacementOperator()); // NEWLY ADDED LINE
}
}