Cloning objects without Serialization

c# deep copy without serialization
java copy object by value
groovy clone object
java deep copy object
java clone object
java deep copy list
java clone object without cloneable
java copy fields from one object to another

I've found numerous solutions here at SO and elsewere that deal with deep clone of object via serialization/deserialization (into memory and back).

It requires that classes to be cloned are marked with [Serializable]. I happen to have my classes (well most of them) marked with [DataContract] because I use DataContractSerializer to serialize into XML.

I only introduced [Serializable] attribute because of the need for deep clone of some of these class instances. However, now something happened to serialization/deserialization via the DCS because it does not work anymore - errors about expecting a different XML element on deserialization. If I remove the [Serializable] the errors are gone.

What are my options? I just want to deep clone my objects as simple as possible.

This works

    public static T DeepClone<T>(this T a)
        using (MemoryStream stream = new MemoryStream())
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            dcs.WriteObject(stream, a);
            stream.Position = 0;
            return (T)dcs.ReadObject(stream);

Java Tip 76: An alternative to the deep copy technique, This is because the default behavior of an object's clone() method automatically here, a shallow copy occurs when an object is copied without its contained objects. A serializable object can be serialized by passing it to the writeObject()​  Clone through Serialization The simplest approach is to clone by taking advantage of an object being Serializable. Apache Commons provides a method to do this, but for completeness, code to do it yourself is below also.

Json serialization and deserialization should work, it doesn't require the classes to have serialize annotation.

public static T DeepCopy<T>(this T source)
    return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));

Classloader Deep-Cloning without Serialization, These objects are (in the simple cases) only referred to by a cloned object. Since we cannot make any assumptions about the class structure or  Easy Deep Cloning of Serializable and Non-Serializable Objects in Java Posted on December 11, 2011 by carey Frequently developers rely on 3d party libraries to avoid reinventing the wheel, particularly in the Java world, with projects like Apache and Spring so prevalent.

Here's how to use it:

var oldList = new List<int>();
var newList = oldList.Clone();

using these Methods:

public static T Clone<T>(this T o) where T : new()
    return (T)CloneObject(o);
static object CloneObject(object obj)
    if (ReferenceEquals(obj, null)) return null;

    var type = obj.GetType();
    if (type.IsValueType || type == typeof(string))
        return obj;
    else if (type.IsArray)
        var array = obj as Array;
        var arrayType = Type.GetType(type.FullName.Replace("[]", string.Empty));
        var arrayInstance = Array.CreateInstance(arrayType, array.Length);

        for (int i = 0; i < array.Length; i++)
            arrayInstance.SetValue(CloneObject(array.GetValue(i)), i);
        return Convert.ChangeType(arrayInstance, type);
    else if (type.IsClass)
        var instance = Activator.CreateInstance(type);
        var fields = type.GetFields(BindingFlags.Public |
                    BindingFlags.NonPublic | BindingFlags.Instance);

        foreach (var field in fields)
            var fieldValue = field.GetValue(obj);
            if (ReferenceEquals(fieldValue, null)) continue;
            field.SetValue(instance, CloneObject(fieldValue));
        return instance;
        return null;

How to Make a Deep Copy of an Object in Java, The other way to serialize is to use JSON serialization. Gson is a library that's used for converting objects into JSON and vice versa. Unlike  Customer1 and Customer2 are linked and any modification in an object will be reflected in the other object. To Clone is necessary for un-linking the object and its virtual copy; and they are independent objects. ICloneable It is an official .NET Framework Interface to clone the objects. It is very simple and has only one method, Clone.

Easy Deep Cloning of Serializable and Non-Serializable Objects in , Option 1 – Java Deep Cloning Library​​ It takes advantage of Java's excellent reflection capabilities to provide optimized deep-cloned versions of objects. Cloner cloner=new Cloner(); Object cloned = cloner. deepClone(someObject); As you can see, it's very simple and effective, and requires minimal code. The idea is that it serializes your object and then deserializes it into a fresh object. The benefit is that you don't have to concern yourself about cloning everything when an object gets too complex. And with the use of extension methods (also from the originally referenced source): In case you prefer to use the new extension methods of C# 3

Java Cloning: Copy Constructors vs. Cloning, Java Deep Cloning Library: offers deep cloning without the need to implement Method to deep clone an object using in memory serialization. We all know that easiest way of deep cloning (with some performance overhead) or deep copy is Serialization. Java serialization involves serializing the object into bytes and from bytes to object again. I will suggest you to use in memory deep cloning whenever it is the only need and you don’t need to persist the object for future use. In this Java deep cloning example, I will suggest one mechanism of in-memory deep cloning for your reference.

Deep Copy of Object in C#, Most JSON libraries in Java have the ability to effectively serialize any POJO without any configuration or mapping required. This means that if  I have two objects of the same type and need to copy property values from one object to another. There are two options: Use reflection, navigate through the properties of the first object and copy the values. Serialize the first object and deserialize a copy.