JSONmutator

A Java library for mutating JSON objects.

License

License

Categories

Categories

JSON Data
GroupId

GroupId

es.us.isa
ArtifactId

ArtifactId

json-mutator
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

JSONmutator
A Java library for mutating JSON objects.
Project URL

Project URL

https://github.com/isa-group/JSONmutator
Source Code Management

Source Code Management

https://github.com/isa-group/JSONmutator/tree/master

Download json-mutator

How to add to project

<!-- https://jarcasting.com/artifacts/es.us.isa/json-mutator/ -->
<dependency>
    <groupId>es.us.isa</groupId>
    <artifactId>json-mutator</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/es.us.isa/json-mutator/
implementation 'es.us.isa:json-mutator:0.0.1'
// https://jarcasting.com/artifacts/es.us.isa/json-mutator/
implementation ("es.us.isa:json-mutator:0.0.1")
'es.us.isa:json-mutator:jar:0.0.1'
<dependency org="es.us.isa" name="json-mutator" rev="0.0.1">
  <artifact name="json-mutator" type="jar" />
</dependency>
@Grapes(
@Grab(group='es.us.isa', module='json-mutator', version='0.0.1')
)
libraryDependencies += "es.us.isa" % "json-mutator" % "0.0.1"
[es.us.isa/json-mutator "0.0.1"]

Dependencies

compile (8)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-core jar 2.9.9
com.fasterxml.jackson.core : jackson-databind jar 2.9.9
org.apache.commons : commons-lang3 jar 3.9
org.apache.commons : commons-math3 jar 3.6.1
com.google.guava : guava jar 28.0-jre
org.apache.logging.log4j : log4j-api jar 2.13.3
org.apache.logging.log4j : log4j-core jar 2.13.3
junit : junit jar 4.13

Project Modules

There are no modules declared in this project.

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
    }
}
es.us.isa

ISA Group

Applied Software Engineering research group at the University of Seville

Versions

Version
0.0.1