[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/371 --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82402368 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the ti
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user graemerocher commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82400528 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the tim
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r8233 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -492,7 +171,7 @@ private static void writeIterator(Iterator iterator, CharBuf buffer) { */ public static String prettyPrint(String jsonPayload) { int indentSize = 0; -// Just a guess that the pretty view will take 20 percent more than original. +// Just a guess that the pretty view will take a 20 percent more than original. --- End diff -- Thanks for pointing that out I'll remove the `a`. I missed that when fixing up the merge conflict caused by commit 7e8211fa314526ce. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82399398 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the ti
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user paulk-asert commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82374539 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the time
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user paulk-asert commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82341071 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -492,7 +171,7 @@ private static void writeIterator(Iterator iterator, CharBuf buffer) { */ public static String prettyPrint(String jsonPayload) { int indentSize = 0; -// Just a guess that the pretty view will take 20 percent more than original. +// Just a guess that the pretty view will take a 20 percent more than original. --- End diff -- why is 'a' added in above comment? Should it be 'about' or just a spurious typo? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user graemerocher commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r81288571 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the tim
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user graemerocher commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r81288503 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the tim
[GitHub] groovy pull request #371: Serialization options for JsonOutput
GitHub user jwagenleitner opened a pull request: https://github.com/apache/groovy/pull/371 Serialization options for JsonOutput You can merge this pull request into a Git repository by running: $ git pull https://github.com/jwagenleitner/groovy Json-JsonOutput Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/371.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #371 commit 01b187288ab3e92904c6cec1ec3df605c671e30a Author: John Wagenleitner Date: 2016-07-24T19:05:02Z refactor(json): move public methods up In preparation for introducing a new nested classes moved public methods that will remain on JsonOutput up above the private methods. commit 2c0336b8491913fe9bfed61571925b99dc52dc6c Author: John Wagenleitner Date: 2016-07-24T19:05:35Z Serialization options for JsonOutput Introduces the JsonOutput.Generator that can be configured with options in order to alter the generated JSON output. commit 724a584793b0d9de26b7a95e99e6e357bca500da Author: John Wagenleitner Date: 2016-07-24T19:28:40Z JsonBuilder - add ability to use JsonOutput.Options --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---