Main Page

From Transmorph

Jump to: navigation, search

Welcome to transmorph Project Wiki

Information

Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).

Transmorph strong points are :

* support conversion for primitives and objects 
* support conversion to multidimensional arrays
* support conversion to parameterized collections and types
* support conversion for beans that contain bi-directional relationships
* you can choose exactly which converters you want to use
* no dependencies
* 40 converters included
* easy to add more converters
* objects can be modified after conversion using modifiers
* can convert to a type given either its java type (Class) or signature
* easy to use

Transmorph can also be used to inject properties from one object (Map or Bean) to a bean.

Free support is provided in the Transmorph forum.

You can also contact me at cchabanois (at) gmail (dot) com

News

6 February 2011

Transmorph 3.1.1 release

  • maintenance release
    • #0000001: Cannot convert a bean to map when a getter with no setter throws an exception even when we call setIgnorePropertiesWithNoSetter
    • do not ignore null values for bean properties anymore in MapToBean
  • ... See full changes
13 April 2010

Transmorph 3.1.0 release

  • Some changes to MapToBean :
    • can now handle an interface as a destination type as long as IMapToBeanMapping can provide a concrete type from the map and destination type
    • new implementation for IMapToBeanMapping that can use map "class" key if present
  • make sure that converters do not return null when converting to primitive type
  • added SameTypeConverter converter that can be used to clone an objet
  • improved NumberToNumber converter : conversion to BigInteger and BigDecimal have been improved
  • added comparator NumberComparator to compare numbers (to compare an Integer to a Long, an Integer to a Float ...)
  • ... See full changes
14 January 2010

Transmorph 3.0.2 release

  • added new converters :BeanToMap and SubTypeConverter
  • ... See full changes
4 December 2009

Transmorph 3.0.1 release

28 September 2009

Transmorph 3.0.0 release

  • This release is NOT source and binary compatible with earlier versions
  • Now using TypeReference which is based on Neal Gafter's Super Type Tokens to overcome type erasure. (see http://gafter.blogspot.com/2006/12/super-type-tokens.html)
  • signature parsing is now separated from type conversion
7 September 2009

Transmorph 2.2.2 release

  • added new converters : SerializableConverter and CloneableConverter
  • modified converters : BeanToBeanInjector will now inject from a subclass bean to a superclass bean without needing a mapping
  • Some changes to Transmorph class (more methods using generics)
  • ... See full changes

Using transmorph

First, you need to choose which converters you want to use.

You can use
new DefaultConverters()
which contains the main converters but you can also choose yourself the converters you need if you wish.

You can then create a Transmorph object :

 
Transmorph transmorph = new Transmorph(new DefaultConverters());
 

Create the object you want to convert :

 
Map map = new HashMap();
map.put("key1", new String[] { "value1-1", "value1-2" });
map.put("key2", new String[] { "value2-1", "value2-2" });
map.put("key3", null);
map.put("key4", new String[] { null, null });
map.put(null, new String[] { "value5-1", "value5-2" });
 

One of the limitations of generics is type erasure. Based on Neal Gafter's Type Tokens, is it possible to overcome this limitation:

 
TypeReference<Map<String,List<String>>> typeReference = new TypeReference<Map<String,List<String>>>() {};
 
 
Map<String, List<String>> converted = transmorph.convert(map,typeReference);
 

You can also use : (Here we convert an int[] to a List<Integer>

 
List<Integer> listOfInts = transmorph.convert(
	new int[] { 0, 1, 2, 3, 4, 5 }, List.class, new Class[] { Integer.class });
 

If you only know the type signature, use a parser (3 parsers available depending on the format of the signature)

 
JavaSyntaxTypeSignatureParser typeSignatureParser = new JavaSyntaxTypeSignatureParser("java.util.Map<java.lang.String, java.util.List<java.lang.String>>");
FullTypeSignature typeSignature = typeSignatureParser.parseTypeSignature();
TypeFactory typeFactory = new TypeFactory(getClass().getClassLoader());
Type type = typeFactory.getType(typeSignature);
Map<String, List<String>> converted = (Map<String, List<String>>)transmorph.convert(map,type);
 

User documentation

Developer documentation

Related projects

There are other projects that perform Object to Object conversions: (This table has mainly been copied from EZMorph comparaison table)

Project Name Description
EZMorph EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans
Commons-Beanutils ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.
Commons-Lang ArrayUtils -> Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).
Commons-Convert Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions.
Morph Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations.
Lorentz Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type.
Spring framework Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.
Dozer Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
Bean2Bean Bean2Bean is two things:
  • Object mapper between classes, using annotations
  • Type converter for any class (or Type)


  transmorph
Maintainer: Cédric ChabanoisTalk
Platform: Platform independent
Genre: java library
License: Apache License 2.0
Languages: English
Website: transmorph.sourceforge.net

Download the latest version!

Sourceforge site


Note: logo is available at http://flickr.com/photos/xadrian/2180911277/ (with a Creative Commons license)
Personal tools