Archive: 2018

Groovy Goodness: Customizing JSON Output

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 adds the possibility to customize JSON output via a JsonGenerator instance. The easiest way to turn an object into a JSON string value is via JsonOutput.toJson. This method uses a default JsonGenerator with sensible defaults for JSON output. But we can customize this generator and create JSON output using the custom generator. To create a custom generator we use a builder accessible via JsonGenerator.Options. Via a fluent API we can for example ignore fields with null values in the output, change the date format for dates and ignore fields by their name or type of the value. And we can add a custom converter for types via either an implementation of the conversion as Closure or implementation of the JsonGenerator.Converter interface. To get the JSON string we simple invoke the toJson method of our generator.

In the following example Groovy code we have a Map with data and we want to convert it to JSON. First we use the default generator and then we create our own to customize the JSON output:

Continue reading →

Groovy Goodness: Remove Last Item From List Using RemoveLast Method (And Pop/Push Methods Reimplemented)

Posted on by  
Hubert Klein Ikkink

Versions of Groovy before 2.5.0 implemented pop and push methods for the List class for items at the end of a List object. The pop method removed the last item of a List and push added a item to the List. Groovy 2.5.0 reimplemented the methods so they now work on the first item of a List instance. To remove an item from the end of the list we can use the newly added method removeLast.

In the following example Groovy code we use the removeLast and add methods to remove and add items to the end of the list. And with the pop and push methods we remove and add items to the beginnen of the list:

Continue reading →

Groovy Goodness: Getting All Init And Tail Values Recursively

Posted on by  
Hubert Klein Ikkink

For a long time we could get the tail or init valuesfor a collection. Groovy 2.5.0 adds the methods inits and tails for Iterable objects. These methods return a List with List values where the first element is the original collection and the next is the result of init or tail on the previous element. This is repeated until the result of init or tail is an empty List.

In the next example script we have a original collection of letters. We first run the init and tail methods (without the s). Next we look at the result of invoking inits and tails:

Continue reading →

Groovy Goodness: Intersect Collections With Custom Comparator

Posted on by  
Hubert Klein Ikkink

In a previous postwe learned about the intersect method added to collections in Groovy. Since Groovy 2.5.0 we can supply a custom Comparator to the intersect method to define our own rules for the intersection.

In the following example we first apply the intersect method with the default Comparator. Then we create a new Comparator using a closure where we check if the value is in both collections and if the value starts with the letter M:

Continue reading →

Groovy Goodness: Easy Object Creation With Tap Method

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 adds the tap method to all objects and changes the method signature of the with method. In a previous post we already learned about the with method. In Groovy 2.5.0 we can add an extra boolean argument to the with method. If the value is false (is default) the with method must return the same value as what the closure invocation returns. If the value is true the object instance on which the with method is invoked is returned. The new tap method is an alias for with(true), so it will always return the object instance.

In the first example we use the tap method to create a new Sample object and set property values and invoke methods of the Sample class:

Continue reading →

Groovy Goodness: Where Is My Class?

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 makes it possible to get the location of a Class file by adding the method getLocation to the Class class. If the Class is part of the JDK the location returned is null, but otherwise we get the location of the JAR file or source file (if available) with the Class file.

In the following example we get the location for the internal JDK String class and the Groovy utility class ConfigSlurper:

Continue reading →

Groovy Goodness: Calculate MD5 And SHA Hash Values

Posted on by  
Hubert Klein Ikkink

Groovy adds a lot of useful methods to the String class. Since Groovy 2.5.0 we can even calculate MD5 and SHA hash values using the methods md5 and digest. The md5 method create a hash value using the MD5 algorithm. The digest method accepts the name of the algorithm as value. These values are dependent on the available algorithms on our Java platform. For example the algorithms MD2, MD5, SHA-1, SHA-256, SHA-384 and SHA-512 are by default available.

In the next example we use the md5 and digest methods on a String value:

Continue reading →

Groovy Goodness: Java 8 Stream Enhancements

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 adds several methods to make working with Java 8 Streams more Groovy. First of all the methods toList and toSet are added to the Stream class. These methods will convert the stream to a List and Set using the Stream.collect method with Collectors.toList and Collectors.toSet as argument. Furthermore we can convert any array object to a Stream using the stream method that is added to all array objects.

In the following example we use the support of converting an array to a Stream and then getting a List and Set from the stream:

Continue reading →

Groovy Goodness: Using String Values In Ranges

Posted on by  
Hubert Klein Ikkink

We can use ranges in Groovy using an easy syntax where the start and end values of the range are separated by .. for an inclusive range and ..< for an exclusive range as we have seen in a previous post. The values of the range are mostly numbers or enum values. But we can also use String values to define a range. Groovy will check if the String values are the same length and if the values, except for the last character, are the same. Then the natural ordering of the last character of the String value, based on the character’s int value, is used to create the range values.

In the following example we define several ranges using String values. We can even define a reverse range using String values.

Continue reading →

Groovy Goodness: Use Range By Method To Set Steps Between Numbers

Posted on by  
Hubert Klein Ikkink

Groovy has support for defining ranges in the language. When we define a range of numbers the steps between the values in the range is 1 by default. We can change the step size using the step method. This method accepts a int value with a new step size. The result is a List object with the values. Since Groovy 2.5.0 the by method is added to ranges with numbers. The by method accepts also decimal numbers and the result of the method is a NumberRange object instead of a List.

In the following example Groovy script we first define a range with int values. We use the by method to change the step size using both an int value and BigDecimal value. We also use the by method for a range of BigDecimal numbers:

Continue reading →

shadow-left