Skip to content

Instantly share code, notes, and snippets.

@fo2rist
Last active August 28, 2019 15:25
Show Gist options
  • Save fo2rist/055f552ab86e29b3fabbd3560080e19e to your computer and use it in GitHub Desktop.
Save fo2rist/055f552ab86e29b3fabbd3560080e19e to your computer and use it in GitHub Desktop.
Gson Examples. Two data classes corresponding Java code
public final class WithoutDefaultConstructor {
private final int intValue;
@NotNull
private final String strValue;
public WithoutDefaultConstructor(int intValue, @NotNull String strValue) {
super();
this.intValue = intValue;
this.strValue = strValue;
}
public WithoutDefaultConstructor(int intValue, String strValue, int defaultParametersFlags, DefaultConstructorMarker defaultConstructorMarker) {
if ((defaultParametersFlags & 2) != 0) {
strValue = "default";
}
this(intValue, strValue);
}
//... other methods
}
public final class WithDefaultConstructor {
private final int intValue;
@NotNull
private final String strValue;
public WithDefaultConstructor(int intValue, @NotNull String strValue) {
super();
this.intValue = intValue;
this.strValue = strValue;
}
public WithDefaultConstructor(int intValue, String strValue, int defaultParametersFlags, DefaultConstructorMarker defaultConstructorMarker) {
if ((defaultParametersFlags & 1) != 0) { // checking binary flag for the first parameter presence `01`
intValue = 1;
}
if ((defaultParametersFlags & 2) != 0) { // checking binary flag for the second parameter presence `10`
strValue = "default";
}
this(intValue, strValue);
}
public WithDefaultConstructor() {
this(0, (String)null, 3, (DefaultConstructorMarker)null);
}
//... other methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment