Skip to content

3. Goodie Formats

Anılcan Metinyurt edited this page May 30, 2023 · 1 revision

📚 Goodie Formats

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)

📖 How do GoodieFormats work?

GoodieFormat abstraction contains 4 unique implementations:

  1. From String to the external format E
  2. From external format E to the targeted Goodie G extends GoodieElement
  3. From external format E to String
  4. From targeted Goodie G extends GoodieElement to E

Using those 4 unique implementations, it is possible to do many conversions!

String <--> External Format <--> Goodie

📚 How to implement a new Goodie Format?

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
	}

}