title: How to specify object custom serialization in ORMLite?
body: <p>I would like to store some field of type ParentClass as json string into my database. I don't want to use Serializable interface and DataType.SERIALIZABLE cause it ties with full class name of serialized class. </p>
  
  <p>So I'm using the following code:</p>
  
  <pre><code>class ParentClass {
  
      @DatabaseField(persisterClass = MyFieldClassPersister.class)
      private MyFieldClass myField;
  }
  </code></pre>
  
  <p>where persister class a kind of:</p>
  
  <pre><code>public class MyFieldClassPersister extends StringType {
  
      private static final MyFieldClassPersister singleTon = new MyFieldClassPersister();
  
      public static MyFieldClassPersister getSingleton() {
          return singleTon;
      }
  
      protected MyFieldClassPersister() {
          super(SqlType.STRING, new Class&lt;?&gt;[0]);
      }
  
      @Override
      public Object parseDefaultString(FieldType fieldType, String defaultStr) {
          return jsonStringToObject(defaultStr);
      }
  
      @Override
      public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException {
          String string = results.getString(columnPos);
          return jsonStringToObject(string);
      }
  
      private static MyFieldClass jsonStringToObject(String string) {
          // json to object conversion logic
      }
  
  }
  </code></pre>
  
  <p>Here are two issues I've met:</p>
  
  <ol>
  <li><p>I didn't get how to specify custom convertion from object to string. Seems that ORMLite calls Object.toString() in order to get string representation of the object. It would be great to have some method in Persister in which I could specify how to convert Object to string (json in my case). Yes, I can override toString() method in MyFieldClass, but it is more convenient to perform conversion in Persister. Is there any method I could override in order to specify convertion from model object to db-object?</p></li>
  <li><p>If I mark my custom field type as String type:</p>
  
  <pre><code>class ParentClass {
  
      @DatabaseField(dataType = DataType.STRING, persisterClass = MyFieldClassPersister.class)
      private MyFieldClass myField;
  }
  </code></pre></li>
  </ol>
  
  <p>then ormlite crashes when saving object with the following message:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Field class com.myapp.venue.MyFieldClass for
      field FieldType:name=myField,class=ParentClass is not valid for type
      com.j256.ormlite.field.types.StringType@272ed83b, maybe should be
      class java.lang.String
  </code></pre>
  
  <p>It doesn't crash if I omit dataType specification. Can I avoid this crash in some way? It seems to me that it's better to specify types explicitly.</p>