How Can I Make A Deep Copy of a Groovy ConfigObject?

groovy deep copy map
groovy config file
groovy deep copy string

How Can I Make A Deep Copy of a Groovy ConfigObject? I see that I can make a shallow copy of the object with .clone(), but I want to make a full deep copy.

The problem with @HappyCoder86 answer is it assumes all config key / values are strings. If some of your config values are objects / closures (common in grails) below solution wont work.

Below solution may be slow but would work if you have values of type other then string

   static def deepcopy(ConfigObject orig) {
        ConfigObject copy = new ConfigObject()
        orig.keySet().each { key ->
            def value = orig.get(key)
            if (value instanceof ConfigObject) {
                value = deepcopy(value)
            copy.put(key, value)
        return copy

ConfigObject (groovy 3.0.4 API), Returns a shallow copy of this ConfigObject, keys and configuration entries are the default getProperty implementation to create nested ConfigObject instances Writes this config object into a String serialized representation which can later  To achieve a deep copy, we can serialize an object and then deserialize it to a new object. Let's look at a few examples. 6.1. Apache Commons Lang. Apache Commons Lang has SerializationUtils#clone, which performs a deep copy when all classes in the object graph implement the Serializable interface.

ConfigObject config = new ConfigSlurper().parse originalConf.toProperties()

Groovy Goodness: Make Class Cloneable With @AutoClone , new Course(. name: 'Groovy 101' ,. date: new Date() + 10 ,. teacher: mrhaki). // We make a deep copy. def secondCourse = course.clone(). 0 How Can I Make A Deep Copy of a Groovy ConfigObject? Oct 8 '19. 0 Saxon XQuery and collection : is it possible in FLWOR query to retrieve file names of returned items?

ConfigObject clonedConfigObject = new ConfigSlurper().parse("${originalConfigObject.prettyPrint()}")

Won't work with entries having Class or Closure values types. But will work with Lists and Maps.

groovy.util.ConfigObject java code examples, for (Object o1 : map.keySet()) { String key = (String) o1; Object v = map.get(key); if (!value. ConfigObject cloned = existingConfig.clone(); propertySources. You are using the spread operator ( * ), which is making a list out of each element. Remove that: list1 = [1, 2, 3] println list1 list2 = list1.collect() assert list2 == [1, 2, 3] Check out the doc for more info on that method. share. Share a link to this answer. Copy link.

Java Code Examples groovy.util.ConfigObject, This page provides Java code examples for groovy.util. readFileToString(​configurationFile)); //Get the name and base configuration annotatorName =(​String)configObject.get("name"); ConfigObject mergedConfigObject = contextConfigObject.clone(); Parse the groovy config file, and return the reader object. This must  A Groovy class is a collection of data and the methods that operate on that data. Together, the data and methods of a class are used to represent some real world object from the problem domain. A class in Groovy declares the state (data) and the behavior of objects defined by that class.

Groovy - ConfigSlurper, ConfigSlurper is a utility class within Groovy for writing properties file like scripts multiple config files and want to create one central config object you can do:. In clone () method, we must ensure that when somebody invokes object.clone () method then it must return a deep copy of that model class (e.g. Employee class). To create a deep copy of the class, divide all the class members into two categories of mutable and immutable types. All immutable field members can be used as it is.

ConfigurationLoader (Groovy API Documentation for Geb 2.0), Manages the process of creating Configuration objects, which control the runtime behaviour of Geb. users can use this class or a subclass of to create a Configuration object and ConfigObject rawConfig, GroovyClassLoader classLoader) This means that if it is the special loader it must have the same copy of the Geb  Groovy - Lists - The List is a structure used to store a collection of data items. In Groovy, the List holds a sequence of object references. Object references in a List occupy