package com.johnstarich.moviematcher.app;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.johnstarich.moviematcher.store.MovieMatcherDatabase;
import org.bson.types.ObjectId;
import spark.ResponseTransformer;

import java.io.IOException;

/**
 * Transforms objects into JSON strings
 * Created by johnstarich on 2/23/16.
 */
public class JsonTransformer implements ResponseTransformer {
	private Gson gson = new GsonBuilder()
		.registerTypeAdapter(ObjectId.class, new TypeAdapter<ObjectId>() {
			@Override
			public void write(JsonWriter jsonWriter, ObjectId objectId) throws IOException {
				jsonWriter.value(objectId.toHexString());
			}

			@Override
			public ObjectId read(JsonReader jsonReader) throws IOException {
				return new ObjectId(jsonReader.nextString());
			}
		})
		.create();

	@Override
	public String render(Object model) throws Exception {
		return MovieMatcherDatabase.morphium.toJsonString(model);
	}

	public <T> T parse(String model, Class<T> clazz) throws Exception {
		return gson.fromJson(model, clazz);
	}
}

@@ -217,24 +217,82 @@
 JsonWriter;%0A
+import com.johnstarich.moviematcher.models.AbstractModel;%0A
 import com.j
@@ -1063,24 +1063,59 @@
 ception %7B%0A%09%09
+if(model instanceof AbstractModel) 
 return Movie
@@ -1160,16 +1160,50 @@
 model);%0A
+%09%09else return gson.toJson(model);%0A
 %09%7D%0A%0A%09pub

Fix JSON serialization