-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate json-io usage behind a JSON bridge.
- Loading branch information
1 parent
74b5501
commit d68310b
Showing
5 changed files
with
152 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/blacksmyth/personalfinancier/dependencies/IJSonSerialisationBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/blacksmyth/personalfinancier/dependencies/JSonBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
src/blacksmyth/personalfinancier/dependencies/JSonIoBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,5 @@ | |
|
||
public interface IFileHandlerModel<T> { | ||
public void fromSerializable(T t); | ||
|
||
public T toSerializable(); | ||
} |