List<NodeTuple> value = new LinkedList<NodeTuple>();
String tag;
String customTag = Tags.MAP;
tag = customTag != null ? customTag : Tags.getGlobalTagForClass( javaBean.getClass() );
// flow style will be chosen by BaseRepresenter
MappingNode node = new MappingNode( tag, value, null );
representedObjects.put( objectToRepresent, node );
boolean bestStyle = true;
for ( Property property : properties )
{
ScalarNode nodeKey = (ScalarNode) representData( property.getName() );
Object memberValue = property.get( javaBean );
boolean hasAlias = false;
if ( representedObjects.containsKey( memberValue ) )
{
// the first occurrence of the node must keep the tag
hasAlias = true;
}
Node nodeValue = representData( memberValue );
// TODO: The impl seems not to allow to skip certain values
if ( nodeValue == null )
{
continue;
}
// if possible try to avoid a global tag with a class name
if ( nodeValue instanceof MappingNode && !hasAlias )
{
// the node is a map, set or JavaBean
if ( !Map.class.isAssignableFrom( memberValue.getClass() ) )
{
// the node is set or JavaBean
if ( property.getType() == memberValue.getClass() )
{
// we do not need global tag because the property
// Class is the same as the runtime class
nodeValue.setTag( Tags.MAP );
}
}
}
else if ( memberValue != null && Enum.class.isAssignableFrom( memberValue.getClass() ) )
{
nodeValue.setTag( Tags.STR );
}
if ( nodeKey.getStyle() != null )
{
bestStyle = false;
}
if ( !( ( nodeValue instanceof ScalarNode && ( (ScalarNode) nodeValue ).getStyle() == null ) ) )
{
bestStyle = false;
}
value.add( new NodeTuple( nodeKey, nodeValue ) );
}
if ( defaultFlowStyle != null )
{
node.setFlowStyle( defaultFlowStyle );
}
else
{
node.setFlowStyle( bestStyle );
}
return node;
}