a simple marshaling and unmarshalling library for processing.
Currently marshal supports the following formats:
- json
- xml
- yaml
Pass this to the constructor of the marshal object like so:
Marhsal marshal = new Marshal(this);Lets assume for these examples that we have a class called Message with one property: content.
Message message = new Message();
message.setContent("hey");
// a file called message.json will be created in the data folder
// with the contents:
// { "content": "hey" }
//
marshal.save(message, "message.json", DataFormat.JSON);If we already have a File object we can use that instead:
Message message = new Message();
message.setContent("hey");
File file = new File("message.json");
marshal.save(message, file, DataFormat.JSON);Let's assume we have a file called message.xml in the data folder.
We can load it to an object like so:
Message message = marshal.load("message.xml", Message.class, DataFormat.XML);If you already have a File object we can use that instead:
File file = new File("message.xml");
Message message = marshal.load(file, Message.class, DataFormat.XML);Rather than write an object to a file we can marshal it to a string to use later.
Message message = new Message();
message.setContent("hey");
String output = marshal.marshal(message, DataFormat.JSON);
// prints { "content": "hey" }
println(output);If we have a a marshaled string we can unmarshall it back into an object:
String json = "{ \"content\": \"hey\" }";
Message message = marshal.unmarshal(json, Message.class, DataFormat.JSON);
// prints hey
println(message.getContent());Marshal provides the TransformationBuilder which gives us some shorthand syntax for converting objects from one format to another.
For example:
//message.xml contains "<Message><content>hey</content></Message>"
String json = marshal.transformFile("message.xml")
.ofType(Message.class)
.from(DataFormat.XML)
.to(DataFormat.JSON)
.getString();
// prints { "content": "hey" }
println(json);We can even save the converted object directly to a file:
// message.xml contains <Message><content>hey</content></Message>
marshal.transformFile("message.xml")
.ofType(Message.class)
.from(DataFormat.XML)
.to(DataFormat.JSON)
.andSaveTo("message.json");
// now there's a file in the data folder named message.json with the message in it.To compile the library run:
./build.sh
After running the build script we have Marshal.zip in the target folder.
Extract the zip file in the libraries folder of your processing sketchbook.
If you want to add or fix something feel free to make an issue and pull request.