-
-
Notifications
You must be signed in to change notification settings - Fork 0
3. Goodie Formats
Anılcan Metinyurt edited this page May 30, 2023
·
1 revision
Goodie Formats are implementations of how to turn external formats into valid Goodie elements.
They exist, so that various files/formats can be represented as a uniform format during runtime. (RuntimeGoodies comes with a built-in Google's GSON converter called GsonGoodieFormat
to read/write JSON data)
GoodieFormat abstraction contains 4 unique implementations:
- From
String
to the external formatE
- From external format
E
to the targeted GoodieG extends GoodieElement
- From external format
E
toString
- From targeted Goodie
G extends GoodieElement
toE
Using those 4 unique implementations, it is possible to do many conversions!
String <--> External Format <--> Goodie
It is very simple! Just create a class and extend GoodieFormat<E, G>
, where E
is the external runtime entity you want to convert into targeted Goodie element G
.
Here's a boilerplate of how to do it for GSON's JsonObject
:
public class GsonGoodieFormat extends GoodieFormat<JsonObject, GoodieObject> {
@Override
public GoodieObject writeToGoodie(JsonObject externalFormat) {
// Turn JsonObject into GoodieObject
}
@Override
public JsonObject readFromGoodie(GoodieObject goodie) {
// Turn GoodieObject back to a JsonObject
}
@Override
public String writeToString(JsonObject externalFormat, boolean pretty) {
// Serialize a String from given JsonObject (and beautify if pretty=true)
}
@Override
public JsonObject readFromString(String text) throws GoodieParseException {
// Parse a JsonObject from given JSON String
}
}