Skip to content

Commit

Permalink
tnoodle-android no longer depends on gson.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfly committed Dec 25, 2013
1 parent 547fc20 commit 9a7c659
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 105 deletions.
4 changes: 3 additions & 1 deletion scrambles/src/net/gnehzr/tnoodle/scrambles/Main.java
@@ -1,5 +1,7 @@
package net.gnehzr.tnoodle.scrambles;

import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -63,7 +65,7 @@ private static void printScramblerInfoJSON(SortedMap<String, LazyInstantiator<Pu
puzzleInfos.put(puzzle, puzzleInfo);
}

String puzzleInfosJSON = Utils.GSON.toJson(puzzleInfos);
String puzzleInfosJSON = GSON.toJson(puzzleInfos);
System.out.println(puzzleInfosJSON);
}

Expand Down
35 changes: 0 additions & 35 deletions scrambles/src/net/gnehzr/tnoodle/scrambles/PuzzlePlugins.java
Expand Up @@ -11,16 +11,6 @@
import net.gnehzr.tnoodle.utils.Plugins;
import net.gnehzr.tnoodle.utils.Strings;
import java.util.SortedMap;
import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import net.gnehzr.tnoodle.utils.Utils;

public class PuzzlePlugins {
private static final Logger l = Logger.getLogger(PuzzlePlugins.class.getName());
Expand Down Expand Up @@ -53,29 +43,4 @@ public static String getScramblerLongName(String shortName) {
return plugins.getPluginComment(shortName);
}

static {
Utils.registerTypeHierarchyAdapter(Puzzle.class, new Puzzlerizer());
}
private static class Puzzlerizer implements JsonSerializer<Puzzle>, JsonDeserializer<Puzzle> {
@Override
public Puzzle deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
String scramblerName = json.getAsString();
SortedMap<String, LazyInstantiator<Puzzle>> scramblers = getScramblers();
LazyInstantiator<Puzzle> lazyScrambler = scramblers.get(scramblerName);
if(lazyScrambler == null) {
throw new JsonParseException(scramblerName + " not found in: " + scramblers.keySet());
}
return lazyScrambler.cachedInstance();
} catch(Exception e) {
throw new JsonParseException(e);
}
}

@Override
public JsonElement serialize(Puzzle scrambler, Type typeOfT, JsonSerializationContext context) {
return new JsonPrimitive(scrambler.getShortName());
}
}

}
3 changes: 1 addition & 2 deletions tnoodle-android/tnoodle-android/build.gradle
Expand Up @@ -16,6 +16,7 @@ android {
exclude 'net/gnehzr/tnoodle/utils/Launcher.java'
exclude 'net/gnehzr/tnoodle/utils/TNoodleLogging.java'
exclude 'net/gnehzr/tnoodle/utils/OneLineLogFormatter.java'
exclude 'net/gnehzr/tnoodle/utils/GsonUtils.java'

java.srcDirs '../../sq12phase/src', '../../min2phase/src', '../../threephase/src'
exclude 'cs/min2phase/MainProgram.java'
Expand All @@ -37,8 +38,6 @@ android {

dependencies {
compile 'com.android.support:appcompat-v7:+'

compile files('../../lib/gson-2.2.4.jar')
}

// From http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven
Expand Down
58 changes: 58 additions & 0 deletions utils/src/net/gnehzr/tnoodle/utils/GsonUtils.java
@@ -0,0 +1,58 @@
package net.gnehzr.tnoodle.utils;

import net.gnehzr.tnoodle.svglite.Color;
import net.gnehzr.tnoodle.svglite.InvalidHexColorException;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GsonUtils {

private GsonUtils() {}

// GSON encodes the string "'" as "\u0027" by default.
// This behavior is controlled by the htmlSafe attribute.
// We call disableHtmlEscaping to disable this behavior.
private static GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
public static Gson GSON = gsonBuilder.create();
public static synchronized void registerTypeAdapter(Class<?> clz, Object typeAdapter) {
gsonBuilder = gsonBuilder.registerTypeAdapter(clz, typeAdapter);
GSON = gsonBuilder.create();
}
public static synchronized void registerTypeHierarchyAdapter(Class<?> clz, Object typeAdapter) {
gsonBuilder = gsonBuilder.registerTypeHierarchyAdapter(clz, typeAdapter);
GSON = gsonBuilder.create();
}

static {
registerTypeAdapter(Color.class, new Colorizer());
}

private static class Colorizer implements JsonSerializer<Color>, JsonDeserializer<Color> {

@Override
public JsonElement serialize(Color c, Type t, JsonSerializationContext context) {
return new JsonPrimitive(c.toHex());
}

@Override
public Color deserialize(JsonElement json, Type t, JsonDeserializationContext context) throws JsonParseException {
try {
return new Color(json.getAsString());
} catch(InvalidHexColorException e) {
throw new JsonParseException(e);
}
}

}

}
50 changes: 0 additions & 50 deletions utils/src/net/gnehzr/tnoodle/utils/Utils.java
Expand Up @@ -2,9 +2,6 @@

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;

import net.gnehzr.tnoodle.svglite.InvalidHexColorException;
import net.gnehzr.tnoodle.svglite.Color;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -13,7 +10,6 @@
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;
Expand All @@ -25,16 +21,6 @@
import java.util.logging.Logger;
import java.util.Random;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public final class Utils {
private static final Logger l = Logger.getLogger(Utils.class.getName());

Expand Down Expand Up @@ -72,42 +58,6 @@ public static void copyFile(File sourceFile, File destFile) throws IOException {
}


// GSON encodes the string "'" as "\u0027" by default.
// This behavior is controlled by the htmlSafe attribute.
// We call disableHtmlEscaping to disable this behavior.
private static GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
public static Gson GSON = gsonBuilder.create();
public static synchronized void registerTypeAdapter(Class<?> clz, Object typeAdapter) {
gsonBuilder = gsonBuilder.registerTypeAdapter(clz, typeAdapter);
GSON = gsonBuilder.create();
}
public static synchronized void registerTypeHierarchyAdapter(Class<?> clz, Object typeAdapter) {
gsonBuilder = gsonBuilder.registerTypeHierarchyAdapter(clz, typeAdapter);
GSON = gsonBuilder.create();
}

static {
registerTypeAdapter(Color.class, new Colorizer());
}

private static class Colorizer implements JsonSerializer<Color>, JsonDeserializer<Color> {

@Override
public JsonElement serialize(Color c, Type t, JsonSerializationContext context) {
return new JsonPrimitive(c.toHex());
}

@Override
public Color deserialize(JsonElement json, Type t, JsonDeserializationContext context) throws JsonParseException {
try {
return new Color(json.getAsString());
} catch(InvalidHexColorException e) {
throw new JsonParseException(e);
}
}

}

public static File getResourceDirectory() {
return getResourceDirectory(true);
}
Expand Down
3 changes: 2 additions & 1 deletion web-utils/src/net/gnehzr/tnoodle/server/SafeHttpServlet.java
@@ -1,6 +1,7 @@
package net.gnehzr.tnoodle.server;

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -107,7 +108,7 @@ protected static void sendError(HttpServletRequest request, HttpServletResponse
if("json".equals(extension)) {
HashMap<String, String> json = new HashMap<String, String>();
json.put("error", error);
sendJSON(request, response, Utils.GSON.toJson(json));
sendJSON(request, response, GSON.toJson(json));
} else {
sendText(request, response, error);
}
Expand Down
@@ -1,6 +1,6 @@
package net.gnehzr.tnoodle.server.webscrambles;

import static net.gnehzr.tnoodle.utils.Utils.GSON;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.parseExtension;

Expand Down
@@ -1,6 +1,6 @@
package net.gnehzr.tnoodle.server.webscrambles;

import static net.gnehzr.tnoodle.utils.Utils.GSON;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down
@@ -1,5 +1,7 @@
package net.gnehzr.tnoodle.server.webscrambles;

import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
Expand All @@ -17,7 +19,6 @@
import javax.servlet.http.HttpServletResponse;

import net.gnehzr.tnoodle.server.SafeHttpServlet;
import net.gnehzr.tnoodle.utils.Utils;

@SuppressWarnings("serial")
public class ScrambleImporterHandler extends SafeHttpServlet {
Expand Down Expand Up @@ -50,7 +51,7 @@ protected void wrappedService(HttpServletRequest request, HttpServletResponse re
}
}
//we need to escape our backslashes
String json = Utils.GSON.toJson(scrambles).replaceAll("\\\\", Matcher.quoteReplacement("\\\\"));
String json = GSON.toJson(scrambles).replaceAll("\\\\", Matcher.quoteReplacement("\\\\"));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
BufferedWriter html = new BufferedWriter(new OutputStreamWriter(bytes));
html.append("<html><body><script>parent.postMessage('");
Expand All @@ -71,7 +72,7 @@ protected void wrappedService(HttpServletRequest request, HttpServletResponse re
while((line = in.readLine()) != null) {
scrambles.add(line);
}
sendJSON(request, response, Utils.GSON.toJson(scrambles));
sendJSON(request, response, GSON.toJson(scrambles));
}
}

Expand Down
@@ -1,6 +1,6 @@
package net.gnehzr.tnoodle.server.webscrambles;

import static net.gnehzr.tnoodle.utils.Utils.GSON;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toInt;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.join;
Expand Down
Expand Up @@ -2,17 +2,22 @@

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.parseExtension;
import static net.gnehzr.tnoodle.utils.Utils.GSON;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;
import static net.gnehzr.tnoodle.utils.Utils.throwableToString;

import net.gnehzr.tnoodle.svglite.Svg;
import net.gnehzr.tnoodle.utils.Utils;
import net.gnehzr.tnoodle.utils.GsonUtils;
import net.gnehzr.tnoodle.utils.BadLazyClassDescriptionException;
import net.gnehzr.tnoodle.utils.LazyInstantiatorException;
import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import java.lang.reflect.Type;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;

import net.gnehzr.tnoodle.svglite.Color;
import net.gnehzr.tnoodle.svglite.Dimension;
Expand Down Expand Up @@ -86,6 +91,32 @@ public BufferedImage getBufferedImage() {
private BufferedImage img = null;
}

static {
GsonUtils.registerTypeHierarchyAdapter(Puzzle.class, new Puzzlerizer());
}

private static class Puzzlerizer implements JsonSerializer<Puzzle>, JsonDeserializer<Puzzle> {
@Override
public Puzzle deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
String scramblerName = json.getAsString();
SortedMap<String, LazyInstantiator<Puzzle>> scramblers = PuzzlePlugins.getScramblers();
LazyInstantiator<Puzzle> lazyScrambler = scramblers.get(scramblerName);
if(lazyScrambler == null) {
throw new JsonParseException(scramblerName + " not found in: " + scramblers.keySet());
}
return lazyScrambler.cachedInstance();
} catch(Exception e) {
throw new JsonParseException(e);
}
}

@Override
public JsonElement serialize(Puzzle scrambler, Type typeOfT, JsonSerializationContext context) {
return new JsonPrimitive(scrambler.getShortName());
}
}

@Override
protected void wrappedService(HttpServletRequest request, HttpServletResponse response, String[] path, LinkedHashMap<String, String> query) throws ServletException, IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, DocumentException, ZipException, BadLazyClassDescriptionException, LazyInstantiatorException {
if (path.length == 0) {
Expand Down Expand Up @@ -220,7 +251,7 @@ protected void wrappedService(HttpServletRequest request, HttpServletResponse re
}

static {
Utils.registerTypeAdapter(PuzzleImageInfo.class, new PuzzleImageInfoizer());
GsonUtils.registerTypeAdapter(PuzzleImageInfo.class, new PuzzleImageInfoizer());
}
private static class PuzzleImageInfoizer implements JsonSerializer<PuzzleImageInfo> {
@Override
Expand Down
8 changes: 3 additions & 5 deletions winstone/src/net/gnehzr/tnoodle/server/JsEnvServlet.java
@@ -1,16 +1,14 @@
package net.gnehzr.tnoodle.server;

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;

import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.gnehzr.tnoodle.utils.Utils;


@SuppressWarnings("serial")
public class JsEnvServlet extends SafeHttpServlet {

Expand All @@ -25,10 +23,10 @@ protected void wrappedService(HttpServletRequest request,
LinkedHashMap<String, String> query) throws Exception {
String extension = SafeHttpServlet.getExtension(request);
if(extension.equals("js")) {
String js = "window.TNOODLE_ENV = " + Utils.GSON.toJson(jsEnv) + ";";
String js = "window.TNOODLE_ENV = " + GSON.toJson(jsEnv) + ";";
sendJS(request, response, js);
} else if(extension.equals("json")) {
sendJSON(request, response, Utils.GSON.toJson(jsEnv));
sendJSON(request, response, GSON.toJson(jsEnv));
} else {
azzert(false);
}
Expand Down
2 changes: 1 addition & 1 deletion winstone/src/net/gnehzr/tnoodle/server/VersionServlet.java
@@ -1,6 +1,6 @@
package net.gnehzr.tnoodle.server;

import static net.gnehzr.tnoodle.utils.Utils.GSON;
import static net.gnehzr.tnoodle.utils.GsonUtils.GSON;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;

import net.gnehzr.tnoodle.utils.Utils;
Expand Down

0 comments on commit 9a7c659

Please sign in to comment.