From d68310bda5661f0f37229ec7b676610ad45369a6 Mon Sep 17 00:00:00 2001 From: Lindsay Bradford Date: Tue, 8 Apr 2014 14:08:52 +1000 Subject: [PATCH] Isolate json-io usage behind a JSON bridge. --- .../control/JSonFileAdapter.java | 52 +++++++++---------- .../IJSonSerialisationBridge.java | 37 +++++++++++++ .../dependencies/JSonBridge.java | 38 ++++++++++++++ .../dependencies/JSonIoBridge.java | 51 ++++++++++++++++++ .../model/IFileHandlerModel.java | 1 - 5 files changed, 152 insertions(+), 27 deletions(-) create mode 100644 src/blacksmyth/personalfinancier/dependencies/IJSonSerialisationBridge.java create mode 100644 src/blacksmyth/personalfinancier/dependencies/JSonBridge.java create mode 100644 src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java diff --git a/src/blacksmyth/personalfinancier/control/JSonFileAdapter.java b/src/blacksmyth/personalfinancier/control/JSonFileAdapter.java index 66be099..8f72d72 100644 --- a/src/blacksmyth/personalfinancier/control/JSonFileAdapter.java +++ b/src/blacksmyth/personalfinancier/control/JSonFileAdapter.java @@ -10,43 +10,43 @@ package blacksmyth.personalfinancier.control; -import java.io.IOException; - import blacksmyth.general.FileUtilities; - -import com.cedarsoftware.util.io.JsonReader; -import com.cedarsoftware.util.io.JsonWriter; +import blacksmyth.personalfinancier.dependencies.IJSonSerialisationBridge; +import blacksmyth.personalfinancier.dependencies.JSonBridge; /** - * An adapter through to a 3rd-party JSON Serialization library. - * All access to JSson functionality should go through this adapter. - * @author linds + * An adapter class that transfers state between instantiated objects and file-serialised + * object state via a 3rd-party JSON Serialization library. */ + public class JSonFileAdapter implements IPersonalFinancierFileAdapter { + private IJSonSerialisationBridge jsonBridge; + + public JSonFileAdapter() { + init(new JSonBridge()); + } + + public JSonFileAdapter(IJSonSerialisationBridge bridge) { + init(bridge); + } + + private void init(IJSonSerialisationBridge bridge) { + this.jsonBridge = bridge; + } + @Override public void toFileFromObject(String filePath, T t) { - try { - FileUtilities.saveTextFile( - filePath, - JsonWriter.formatJson( - JsonWriter.objectToJson(t) - ) - ); - - } catch (IOException e) { - } + FileUtilities.saveTextFile( + filePath, + jsonBridge.toJSon(t) + ); } @Override - @SuppressWarnings("unchecked") public T toObjectFromFile(String filePath) { - try { - return (T) JsonReader.jsonToJava( - FileUtilities.loadTextFile(filePath) - ); - } catch (IOException e) { - return null; - } + return jsonBridge.fromJson( + FileUtilities.loadTextFile(filePath) + ); } } diff --git a/src/blacksmyth/personalfinancier/dependencies/IJSonSerialisationBridge.java b/src/blacksmyth/personalfinancier/dependencies/IJSonSerialisationBridge.java new file mode 100644 index 0000000..080b0be --- /dev/null +++ b/src/blacksmyth/personalfinancier/dependencies/IJSonSerialisationBridge.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2012, Lindsay Bradford and other Contributors. + * All rights reserved. + * + * This program and the accompanying materials are made available + * under the terms of the BSD 3-Clause licence which accompanies + * this distribution, and is available at + * http://opensource.org/licenses/BSD-3-Clause + */ + +package blacksmyth.personalfinancier.dependencies; + +/** + * An interface defining the 'Abstraction' of the Bridge pattern, allowing a bridge + * of all needed functionality between the application and some 3rd-party JSON library + * that the application depends on. + * + * @param The type of objects that this class will bridge JSON serialisation for. + */ + +public interface IJSonSerialisationBridge { + + /** + * Converts an object of type T to a JSON serialisation string of its content. + * @param object + * @return JSON serialisation of object state. + */ + + public String toJSon(T object); + + /** + * Covnerts a JSON serialisation of an object of type T into an instance of that object. + * @param jsonContent + * @return an object instantiated with the state encoded in jsonContent. + */ + public T fromJson(String jsonContent); +} diff --git a/src/blacksmyth/personalfinancier/dependencies/JSonBridge.java b/src/blacksmyth/personalfinancier/dependencies/JSonBridge.java new file mode 100644 index 0000000..d695e95 --- /dev/null +++ b/src/blacksmyth/personalfinancier/dependencies/JSonBridge.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2012, Lindsay Bradford and other Contributors. + * All rights reserved. + * + * This program and the accompanying materials are made available + * under the terms of the BSD 3-Clause licence which accompanies + * this distribution, and is available at + * http://opensource.org/licenses/BSD-3-Clause + */ + +package blacksmyth.personalfinancier.dependencies; + +/** + * A class implementing the 'refined abstraction class' of the Bridge pattern, allowing a + * bridge all needed functionality between the application and the open-source JSON library json-io + * (https://code.google.com/p/json-io/) + * + * @param The type of objects that this class will bridge JSON serialisation for. + */ + +public class JSonBridge implements IJSonSerialisationBridge { + + private IJSonSerialisationBridge bridge; + + public JSonBridge() { + this.bridge = new JSonIoBridge(); + } + + @Override + public String toJSon(T object) { + return bridge.toJSon(object); + } + + @Override + public T fromJson(String jsonContent) { + return bridge.fromJson(jsonContent); + } +} diff --git a/src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java b/src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java new file mode 100644 index 0000000..a874e3f --- /dev/null +++ b/src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2012, Lindsay Bradford and other Contributors. + * All rights reserved. + * + * This program and the accompanying materials are made available + * under the terms of the BSD 3-Clause licence which accompanies + * this distribution, and is available at + * http://opensource.org/licenses/BSD-3-Clause + */ + +package blacksmyth.personalfinancier.dependencies; + +import java.io.IOException; + +import com.cedarsoftware.util.io.JsonWriter; +import com.cedarsoftware.util.io.JsonReader; + +/** + * A class implementing the 'Concrete Implementor class' of the Bridge pattern, allowing a bridge + * of all needed functionality between the application and the open-source JSON library json-io + * (https://code.google.com/p/json-io/) + * + * @param The type of objects that this class will bridge JSON serialisation for. + */ + +public class JSonIoBridge implements IJSonSerialisationBridge { + + @Override + public String toJSon(T object) { + try { + return JsonWriter.formatJson( + JsonWriter.objectToJson(object) + ); + } catch (IOException ioe) { + return null; + } + } + + @SuppressWarnings("unchecked") + @Override + public T fromJson(String jsonContent) { + try { + return (T) JsonReader.jsonToJava( + jsonContent + ); + } catch (IOException e) { + return null; + } + } + +} diff --git a/src/blacksmyth/personalfinancier/model/IFileHandlerModel.java b/src/blacksmyth/personalfinancier/model/IFileHandlerModel.java index 0658516..d9778e3 100644 --- a/src/blacksmyth/personalfinancier/model/IFileHandlerModel.java +++ b/src/blacksmyth/personalfinancier/model/IFileHandlerModel.java @@ -12,6 +12,5 @@ public interface IFileHandlerModel { public void fromSerializable(T t); - public T toSerializable(); }