Skip to content

Commit

Permalink
Add customDatePattern/customDateTimePattern config options
Browse files Browse the repository at this point in the history
Closes #716
  • Loading branch information
joelittlejohn committed Mar 27, 2017
1 parent 1b6414b commit 528ce3c
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,20 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean includeDynamicAccessors = false;

private String dateTimeType = null;
private String dateTimeType;

private String timeType = null;
private String timeType;

private String dateType = null;
private String dateType;

private boolean formatDateTimes = false;

private boolean formatDates = false;

private String customDatePattern;

private String customDateTimePattern;

private String refFragmentPathDelimiters = "#/.";

/**
Expand Down Expand Up @@ -710,6 +714,30 @@ public void setFormatDates(boolean formatDates) {
this.formatDates = formatDates;
}

/**
* Sets the 'customDatePattern' property of this class
*
* @param customDatePattern
* A custom pattern to use when formatting date fields during
* serialization. Requires support from your JSON binding
* library.
*/
public void setCustomDatePattern(String customDatePattern) {
this.customDatePattern = customDatePattern;
}

/**
* Sets the 'customDateTimePattern' property of this class
*
* @param customDatePattern
* A custom pattern to use when formatting date-time fields during
* serialization. Requires support from your JSON binding
* library.
*/
public void setCustomDateTimePattern(String customDateTimePattern) {
this.customDateTimePattern = customDateTimePattern;
}

/**
* Sets the 'refFragmentPathDelimiters' property of this class
*
Expand Down Expand Up @@ -968,6 +996,16 @@ public boolean isFormatDates() {
return formatDates;
}

@Override
public String getCustomDatePattern() {
return customDatePattern;
}

@Override
public String getCustomDateTimePattern() {
return customDateTimePattern;
}

@Override
public String getRefFragmentPathDelimiters() {
return refFragmentPathDelimiters;
Expand Down
19 changes: 14 additions & 5 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,28 @@ <h3>Parameters</h3>
</tr>
<tr>
<td valign="top">formatDates</td>
<td valign="top">Whether the fields of type <code>date</code> have the <code>@JsonFormat</code> annotation
with pattern set to the default value of <code>yyyy-MM-dd</code>.
<td valign="top">Whether the fields of type `date` are formatted during serialization with a default pattern of yyyy-MM-dd'T'HH:mm:ss.SSSZ.
</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">formatDateTimes</td>
<td valign="top">Whether the fields of type <code>date-time</code> have the <code>@JsonFormat</code> annotation
with pattern set to the default value of <code>yyyy-MM-dd'T'HH:mm:ss.SSS</code> and timezone set to default
value of <code>UTC</code>
<td valign="top">Whether the fields of type `date` are formatted during serialization with a default pattern of yyyy-MM-dd'T'HH:mm:ss.SSSZ.
</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">customDatePattern</td>
<td valign="top">A custom pattern to use when formatting date fields during serialization. Requires support from your JSON binding library.
</td>
<td align="center" valign="top">No (default none)</td>
</tr>
<tr>
<td valign="top">customDateTimePattern</td>
<td valign="top">A custom pattern to use when formatting date fields during serialization. Requires support from your JSON binding library.
</td>
<td align="center" valign="top">No (default none)</td>
</tr>
<tr>
<td valign="top">refFragmentPathDelimiters</td>
<td valign="top">A string containing any characters that should act as path delimiters when resolving $ref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ public class Arguments implements GenerationConfig {
private boolean useJodaLocalTimes = false;

@Parameter(names = { "-dtt", "--datetime-class" }, description = "Specify datetime class")
private String dateTimeType = null;
private String dateTimeType;

@Parameter(names = { "-tt", "--time-class" }, description = "Specify time class")
private String timeType = null;
private String timeType;

@Parameter(names = { "-dt", "--date-class" }, description = "Specify date class")
private String dateType = null;
private String dateType;

@Parameter(names = { "-c3", "--commons-lang3" }, description = "Whether to use commons-lang 3.x imports instead of commons-lang 2.x imports when adding equals, hashCode and toString methods.")
private boolean useCommonsLang3 = false;
Expand Down Expand Up @@ -166,12 +166,18 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-ida", "--include-dynamic-accessors" }, description = "Include dynamic getter, setter, and builder support on generated types.")
private boolean includeDynamicAccessors = false;

@Parameter(names = { "-fd", "--format-dates" }, description = "Whether the fields of type `date` have the `@JsonFormat` annotation with pattern set to the default value of `yyyy-MM-dd`")
@Parameter(names = { "-fd", "--format-dates" }, description = "Whether the fields of type `date` are formatted during serialization with a default pattern of `yyyy-MM-dd`")
private boolean formatDates = false;

@Parameter(names = { "-fdt", "--format-date-times" }, description = "Whether the fields of type `date-time` have the `@JsonFormat` annotation with pattern set to the default value of `yyyy-MM-dd'T'HH:mm:ss.SSS` and timezone set to default value of `UTC`")
@Parameter(names = { "-fdt", "--format-date-times" }, description = "Whether the fields of type `date-time` are formatted during serialization with a default pattern of `yyyy-MM-dd'T'HH:mm:ss.SSSZ` and timezone set to default value of `UTC`")
private boolean formatDateTimes = false;

@Parameter(names = { "-dp", "--date-pattern" }, description = "A custom pattern to use when formatting date fields during serialization")
private String customDatePattern;

@Parameter(names = { "-dtp", "--date-time-pattern" }, description = "A custom pattern to use when formatting date-time fields during serialization")
private String customDateTimePattern;

@Parameter(names = {"-rpd", "--ref-fragment-path-delimiters"}, description = "A string containing any characters that should act as path delimiters when resolving $ref fragments. By default, #, / and . are used in an attempt to support JSON Pointer and JSON Path.")
private String refFragmentPathDelimiters = "#/.";

Expand Down Expand Up @@ -434,4 +440,14 @@ public String getRefFragmentPathDelimiters() {
return refFragmentPathDelimiters;
}

@Override
public String getCustomDatePattern() {
return customDatePattern;
}

@Override
public String getCustomDateTimePattern() {
return customDateTimePattern;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,14 @@ public boolean isFormatDates() {
public String getRefFragmentPathDelimiters() {
return "#/.";
}

@Override
public String getCustomDatePattern() {
return null;
}

@Override
public String getCustomDateTimePattern() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public interface GenerationConfig {
*/
boolean isUseDoubleNumbers();


/**
* Gets the 'useBigDecimals' configuration option.
*
Expand Down Expand Up @@ -156,8 +155,7 @@ public interface GenerationConfig {
* <a href="https://code.google.com/p/google-gson/">gson</a>
* library)</li>
* <li><code>moshi1</code> (apply annotations from the
* <a href="https://github.com/square/moshi">moshi</a>
* library)</li>
* <a href="https://github.com/square/moshi">moshi</a> library)</li>
* <li><code>none</code> (apply no annotations at all)</li>
* </ul>
* @see AnnotatorFactory
Expand All @@ -168,17 +166,17 @@ public interface GenerationConfig {
* Gets the 'inclusionLevel' option for Jackson1 and Jackson2 serializers.
*
* @return Level of inclusion to set in the generated Java types.
* <p>
* Supported values
* <ul>
* <li><code>ALWAYS</code></li>
* <li><code>NON_ABSENT</code></li>
* <li><code>NON_DEFAULT</code></li>
* <li><code>NON_EMPTY</code></li>
* <li><code>NON_NULL</code></li>
* <li><code>USE_DEFAULTS</code></li>
* </ul>
* </p>
* <p>
* Supported values
* <ul>
* <li><code>ALWAYS</code></li>
* <li><code>NON_ABSENT</code></li>
* <li><code>NON_DEFAULT</code></li>
* <li><code>NON_EMPTY</code></li>
* <li><code>NON_NULL</code></li>
* <li><code>USE_DEFAULTS</code></li>
* </ul>
* </p>
*
* @see InclusionLevel
*/
Expand Down Expand Up @@ -335,7 +333,8 @@ public interface GenerationConfig {
/**
* Gets the 'fileExtensions' configuration option.
*
* @return An array of strings that should be considered as file extensions and therefore not included in class names.
* @return An array of strings that should be considered as file extensions
* and therefore not included in class names.
*/
String[] getFileExtensions();

Expand Down Expand Up @@ -374,86 +373,105 @@ public interface GenerationConfig {
/**
* Gets the 'targetVersion' configuration option.
*
* @return The target version for generated source files.
* @return The target version for generated source files.
*/
String getTargetVersion();

/**
* Gets the `includeDynamicAccessors` configuraiton option.
*
* @return Whether to include dynamic getters, setters, and builders
* or to omit these methods.
* @return Whether to include dynamic getters, setters, and builders or to
* omit these methods.
*/
boolean isIncludeDynamicAccessors();

/**
* Gets the `dateTimeType` configuration option.
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalDateTime</code> (Joda)</li>
* <li><code>java.time.LocalDateTime</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalDateTime</code> (Joda)</li>
* <li><code>java.time.LocalDateTime</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
*
* @return The java type to use instead of {@link java.util.Date}
* when adding date type fields to generate Java types.
* @return The java type to use instead of {@link java.util.Date} when
* adding date type fields to generate Java types.
*/
String getDateTimeType();

/**
* Gets the `dateType` configuration option.
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalDate</code> (Joda)</li>
* <li><code>java.time.LocalDate</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
*
* @return The java type to use instead of string
* when adding string type fields with a format of date (not
* date-time) to generated Java types.
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalDate</code> (Joda)</li>
* <li><code>java.time.LocalDate</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
*
* @return The java type to use instead of string when adding string type
* fields with a format of date (not date-time) to generated Java
* types.
*/
String getDateType();

/**
* Gets the `timeType` configuration option.
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalTime</code> (Joda)</li>
* <li><code>java.time.LocalTime</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
*
* @return The java type to use instead of string
* when adding string type fields with a format of time (not
* date-time) to generated Java types.
* <p>
* Example values:
* <ul>
* <li><code>org.joda.time.LocalTime</code> (Joda)</li>
* <li><code>java.time.LocalTime</code> (JSR310)</li>
* <li><code>null</code> (default behavior)</li>
* </ul>
*
* @return The java type to use instead of string when adding string type
* fields with a format of time (not date-time) to generated Java
* types.
*/
String getTimeType();

/**
* Gets the `formatDates` configuration option
*
* @return Whether the fields of type <code>date</code> have the <code>@JsonFormat</code> annotation
* with pattern set to the default value of <code>yyyy-MM-dd</code>
* @return Whether the fields of type <code>date</code> have the
* <code>@JsonFormat</code> annotation with pattern set to the
* default value of <code>yyyy-MM-dd</code>
*/
boolean isFormatDates();

/**
* Gets the `formatDateTime` configuration option
*
* @return Whether the fields of type <code>date-type</code> have the <code>@JsonFormat</code> annotation
* with pattern set to the default value of <code>yyyy-MM-dd'T'HH:mm:ss.SSSZ</code>
* @return Whether the fields of type <code>date-type</code> have the
* <code>@JsonFormat</code> annotation with pattern set to the
* default value of <code>yyyy-MM-dd'T'HH:mm:ss.SSSZ</code>
*/
boolean isFormatDateTimes();

/**
* Gets the 'customDatePattern' configuration option
*
* @return The custom format that dates will use when types are serialized.
* Requires support from your JSON binding library.
*/
String getCustomDatePattern();

/**
* Gets the 'customDateTimePattern' configuration option
*
* @return The custom format that dates will use when types are serialized.
* Requires support from your JSON binding library.
*/
String getCustomDateTimePattern();

/**
* Gets the `refFragmentPathDelimiters` configuration option.
*
* @return A string containing any characters that should act as path delimiters when resolving $ref fragments.
* By default, #, / and . are used in an attempt to support JSON Pointer and JSON Path.
* @return A string containing any characters that should act as path
* delimiters when resolving $ref fragments. By default, #, / and .
* are used in an attempt to support JSON Pointer and JSON Path.
*/
String getRefFragmentPathDelimiters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/
public class Jackson1Annotator extends AbstractAnnotator {

private JsonSerialize.Inclusion inclusionLevel = JsonSerialize.Inclusion.NON_NULL;
private final JsonSerialize.Inclusion inclusionLevel;

public Jackson1Annotator(GenerationConfig generationConfig) {
super(generationConfig);
Expand Down
Loading

0 comments on commit 528ce3c

Please sign in to comment.