Skip to content

isa-group/JSONmutator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSONmutator

A Java library for mutating JSON objects.

How to add a new mutation operator

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.):

Modify OperatorNames class

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
}

Modify properties file

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 MutationOperator class

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);
    }
}

Modify Mutator class

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
    }
}

About

A Java library for mutating JSON objects

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages