Skip to content

Commit

Permalink
Isolate json-io usage behind a JSON bridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
LindsayBradford committed Apr 8, 2014
1 parent 74b5501 commit d68310b
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 27 deletions.
52 changes: 26 additions & 26 deletions src/blacksmyth/personalfinancier/control/JSonFileAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements IPersonalFinancierFileAdapter<T> {

private IJSonSerialisationBridge<T> jsonBridge;

public JSonFileAdapter() {
init(new JSonBridge<T>());
}

public JSonFileAdapter(IJSonSerialisationBridge<T> bridge) {
init(bridge);
}

private void init(IJSonSerialisationBridge<T> 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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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 <T> The type of objects that this class will bridge JSON serialisation for.
*/

public interface IJSonSerialisationBridge<T> {

/**
* 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);
}
38 changes: 38 additions & 0 deletions src/blacksmyth/personalfinancier/dependencies/JSonBridge.java
Original file line number Diff line number Diff line change
@@ -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 <T> The type of objects that this class will bridge JSON serialisation for.
*/

public class JSonBridge<T> implements IJSonSerialisationBridge<T> {

private IJSonSerialisationBridge<T> bridge;

public JSonBridge() {
this.bridge = new JSonIoBridge<T>();
}

@Override
public String toJSon(T object) {
return bridge.toJSon(object);
}

@Override
public T fromJson(String jsonContent) {
return bridge.fromJson(jsonContent);
}
}
51 changes: 51 additions & 0 deletions src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java
Original file line number Diff line number Diff line change
@@ -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 <T> The type of objects that this class will bridge JSON serialisation for.
*/

public class JSonIoBridge<T> implements IJSonSerialisationBridge<T> {

@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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@

public interface IFileHandlerModel<T> {
public void fromSerializable(T t);

public T toSerializable();
}

0 comments on commit d68310b

Please sign in to comment.