Commit d71d3f53 authored by Daniel Bluhm's avatar Daniel Bluhm

Use Builder for JSON Deserialization

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 2f835787
......@@ -13,12 +13,16 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
......@@ -28,7 +32,9 @@ import lombok.Setter;
* This is an implementation of $interface that satisfies the dependencies of
* the @DataElement Annotation and was auto-generated by the ICE Framework.
*/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -37,12 +43,22 @@ import lombok.Setter;
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = ${class}.${class}Builder.class)
public class ${class} implements ${interface}, Serializable {
/**
* Logging tool
*/
private static final Logger logger = LoggerFactory.getLogger(${class}.class);
/**
* Deserialization helpers.
*/
@JsonPOJOBuilder(withPrefix = "")
public static class ${class}Builder implements ${class}BuilderMeta {
}
private interface ${class}BuilderMeta {
}
#foreach($field in $fields)
#fielddoc
......@@ -53,7 +69,7 @@ public class ${class} implements ${interface}, Serializable {
#if(!${field.Setter} || ${field.VarNameDifferent})
@Setter(AccessLevel.NONE)
#end
#fielddecl
#fielddecl(true)
#end
#foreach($field in ${fields.Constants})
......@@ -82,25 +98,6 @@ public class ${class} implements ${interface}, Serializable {
#end
#end
/**
* All args constructor for $class.
*
* Used in JSON Deserialization.
#foreach($field in $fields)
* @param ${field.VarName} {@code #fieldtype} for field ${field.VarName}
#end
*/
@JsonCreator
public $class(
#foreach($field in $fields)
@JsonProperty("${field.VarName}") #fieldparametertype ${field.VarName}#if($foreach.hasNext),#end
#end
) {
#foreach($field in $fields)
this.${field.VarName} = ${field.VarName};
#end
}
/**
* Copy constructor for $class.
* @param other Instance of $class to copy
......
......@@ -50,9 +50,9 @@
#macro(fieldparametertype)#nonnull("", " ")#fieldtype#end
## Get field declaration
#macro(fielddecl)
#macro(fielddecl $mutable)
#@settab(1)
#join(" ", ${field.Annotations})#join(" ", ${field.Modifiers})#fieldtype() ${field.VarName}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#if(${field.DefaultValue} && $mutable) @Builder.Default #end#join(" ", ${field.Annotations})#join(" ", ${field.Modifiers})#fieldtype() ${field.VarName}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#end
#end
......
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
public int shouldBePublic;
protected int shouldBeProtected;
......
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
public int testInt = 42;
@Builder.Default public int testInt = 42;
}
\ No newline at end of file
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
@NonNull public String test = "A String Value";
@NonNull @Builder.Default public String test = "A String Value";
}
\ No newline at end of file
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,17 +9,18 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
@NonNull
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
protected UUID privateId = UUID.randomUUID();
protected long id = 0L;
@NonNull protected String name = "name";
@NonNull protected String description = "description";
@NonNull protected String comment = "no comment";
@NonNull protected String context = "default";
protected boolean required = false;
protected boolean secret = false;
@Builder.Default protected UUID privateId = UUID.randomUUID();
@Builder.Default protected long id = 0L;
@NonNull @Builder.Default protected String name = "name";
@NonNull @Builder.Default protected String description = "description";
@NonNull @Builder.Default protected String comment = "no comment";
@NonNull @Builder.Default protected String context = "default";
@Builder.Default protected boolean required = false;
@Builder.Default protected boolean secret = false;
protected JavascriptValidator<Test> validator;
}
\ No newline at end of file
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
public byte testByte;
public short testShort;
......
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
@NonNull public UUID testUuid;
@NonNull public String testString;
......
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
public int testInt;
}
\ No newline at end of file
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
......@@ -7,6 +9,7 @@
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = TestImplementation.TestImplementationBuilder.class)
public class TestImplementation implements Test, Serializable {
@NonNull public UUID testUuid;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment