All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.transaction.SerializationHelper Maven / Gradle / Ivy

The newest version!
package com.backendless.transaction;

import com.backendless.exceptions.ExceptionMessage;
import com.backendless.persistence.BackendlessSerializer;
import com.backendless.utils.MapEntityUtil;

import java.lang.reflect.Constructor;
import java.util.Map;

class SerializationHelper
{
  static  Map serializeEntityToMap( final E entity )
  {
    if( entity == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_ENTITY );

    checkDeclaredType( entity.getClass() );
    final Map serializedEntity = BackendlessSerializer.serializeToMap( entity );
    MapEntityUtil.removeNullsAndRelations( serializedEntity);

    return serializedEntity;
  }

  private static  void checkDeclaredType( Class entityClass )
  {
    if( entityClass.isArray() || entityClass.isAssignableFrom( Iterable.class ) || entityClass.isAssignableFrom( Map.class ) )
      throw new IllegalArgumentException( ExceptionMessage.WRONG_ENTITY_TYPE );

    try
    {
      Constructor[] constructors = entityClass.getConstructors();

      if( constructors.length > 0 )
        entityClass.getConstructor();
    }
    catch( NoSuchMethodException e )
    {
      throw new IllegalArgumentException( ExceptionMessage.ENTITY_MISSING_DEFAULT_CONSTRUCTOR );
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy