Skip to content

feat: Improve JSON serialization with ModifiableVariable #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
</properties>

<dependencies>
<!-- scope: compile -->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
Expand All @@ -80,10 +86,15 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-generator</artifactId>
<version>4.38.0</version>
</dependency>
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
<version>4.38.0</version>
</dependency>
<!-- scope: compile -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
Expand All @@ -100,12 +111,11 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- scope: test -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<scope>test</scope>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<!-- scope: test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -196,6 +206,29 @@
<skipTests>${skip.surefire.tests}</skipTests>
</configuration>
</plugin>
<!-- Generate JSON schema from Java classes -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<id>generate-json-schema</id>
<goals>
<goal>java</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<mainClass>de.rub.nds.modifiablevariable.json.JsonSchemaCliGenerator</mainClass>
<arguments>
<argument>de.rub.nds.modifiablevariable.ModifiableVariable</argument>
<argument>src/main/resources/ModifiableVariable.schema.json</argument>
<argument>de.rub.nds.modifiablevariable.json.ModifiableVariableModule</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<!-- Build jar file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package de.rub.nds.modifiablevariable;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.*;
import jakarta.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.LinkedList;
Expand All @@ -32,15 +32,20 @@
*/
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "@type")
public abstract class ModifiableVariable<E> implements Serializable {

/** The list of modifications that will be applied to the original value when accessed */
@XmlElementWrapper
@XmlAnyElement(lax = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private LinkedList<VariableModification<E>> modifications;

/** The expected value for assertion validation */
@JsonInclude(JsonInclude.Include.NON_NULL)
protected E assertEquals;

/** Default constructor that creates an empty modifiable variable. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
*/
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
@JsonTypeInfo(
use = JsonTypeInfo.Id.SIMPLE_NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "@type")
public abstract class VariableModification<E> implements Serializable {

/** Logger for debugging modification applications */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -23,6 +24,7 @@
public class BigIntegerAddModification extends VariableModification<BigInteger> {

/** The value to add to the original BigInteger */
@JsonProperty(required = true)
private BigInteger summand;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -25,6 +26,7 @@
public class BigIntegerExplicitValueModification extends VariableModification<BigInteger> {

/** The explicit value that will replace the original value */
@JsonProperty(required = true)
private BigInteger explicitValue;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -24,6 +25,7 @@
public class BigIntegerMultiplyModification extends VariableModification<BigInteger> {

/** The factor to multiply by */
@JsonProperty(required = true)
private BigInteger factor;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -25,6 +26,7 @@
public class BigIntegerShiftLeftModification extends VariableModification<BigInteger> {

/** The number of bits to shift left */
@JsonProperty(required = true)
private int shift;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -25,6 +26,7 @@
public class BigIntegerShiftRightModification extends VariableModification<BigInteger> {

/** The number of bit positions to shift right */
@JsonProperty(required = true)
private int shift;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -24,6 +25,7 @@
public class BigIntegerSubtractModification extends VariableModification<BigInteger> {

/** The value to subtract from the original BigInteger */
@JsonProperty(required = true)
private BigInteger subtrahend;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.math.BigInteger;
Expand All @@ -27,6 +28,7 @@
public class BigIntegerXorModification extends VariableModification<BigInteger> {

/** The BigInteger value to XOR with the original value */
@JsonProperty(required = true)
private BigInteger xor;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.biginteger;

import com.fasterxml.jackson.annotation.JsonInclude;
import de.rub.nds.modifiablevariable.ModifiableVariable;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import jakarta.xml.bind.annotation.XmlRootElement;
Expand All @@ -31,6 +32,7 @@
public class ModifiableBigInteger extends ModifiableVariable<BigInteger> {

/** The original BigInteger value before any modifications */
@JsonInclude(JsonInclude.Include.NON_NULL)
private BigInteger originalValue;

/** Default constructor that creates an empty ModifiableBigInteger with no original value. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bool;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;

Expand All @@ -24,6 +25,7 @@
public class BooleanExplicitValueModification extends VariableModification<Boolean> {

/** The explicit boolean value that will replace the original value */
@JsonProperty(required = true)
private boolean explicitValue;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bool;

import com.fasterxml.jackson.annotation.JsonInclude;
import de.rub.nds.modifiablevariable.ModifiableVariable;
import jakarta.xml.bind.annotation.XmlRootElement;

Expand All @@ -24,6 +25,7 @@
public class ModifiableBoolean extends ModifiableVariable<Boolean> {

/** The original Boolean value before any modifications */
@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean originalValue;

/** Default constructor that creates an empty ModifiableBoolean with no original value. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter;
Expand All @@ -32,6 +33,7 @@ public class ByteArrayAppendValueModification extends VariableModification<byte[

/** The bytes to append to the end of the original byte array */
@XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class)
@JsonProperty(required = true)
private byte[] bytesToAppend;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import jakarta.xml.bind.annotation.XmlRootElement;
Expand All @@ -27,9 +28,11 @@
public class ByteArrayDeleteModification extends VariableModification<byte[]> {

/** The number of bytes to delete */
@JsonProperty(required = true)
private int count;

/** The position from which to start deletion (0-based index) */
@JsonProperty(required = true)
private int startPosition;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter;
Expand All @@ -29,6 +30,7 @@ public class ByteArrayExplicitValueModification extends VariableModification<byt

/** The explicit byte array that will replace the original value */
@XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class)
@JsonProperty(required = true)
private byte[] explicitValue;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter;
Expand All @@ -33,9 +34,11 @@ public class ByteArrayInsertValueModification extends VariableModification<byte[

/** The bytes to insert into the original byte array */
@XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class)
@JsonProperty(required = true)
private byte[] bytesToInsert;

/** The position at which to insert the bytes (0-based index) */
@JsonProperty(required = true)
private int startPosition;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import de.rub.nds.modifiablevariable.util.ArrayConverter;
import de.rub.nds.modifiablevariable.util.UnformattedByteArrayAdapter;
Expand All @@ -33,6 +34,7 @@ public class ByteArrayPrependValueModification extends VariableModification<byte

/** The bytes to prepend to the beginning of the original byte array */
@XmlJavaTypeAdapter(UnformattedByteArrayAdapter.class)
@JsonProperty(required = true)
private byte[] bytesToPrepend;

/** Default constructor for serialization. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package de.rub.nds.modifiablevariable.bytearray;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.Arrays;
Expand All @@ -29,6 +30,7 @@
public class ByteArrayShuffleModification extends VariableModification<byte[]> {

/** The shuffle pattern defining which indices to swap */
@JsonProperty(required = true)
private int[] shuffle;

/** Default constructor for serialization. */
Expand Down
Loading