Skip to content

Martini Marshaller Functions

In Martini, the Marshaller class provides essential functions for converting objects to and from XML and JSON formats. This feature is crucial in web services and APIs, where XML and JSON are standard data formats.

Usage in Groovy

The Marshaller class is designed primarily for use with Groovy. Developers can integrate these functions into Groovy scripts or projects. In contexts outside of Groovy, such as Gloop services, it is possible to use these functions through Groovy script steps and set expressions.

Code Examples

Below are examples demonstrating the use of Marshaller functions in Groovy:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Example: Object creation
def exampleObject = new ExampleClass("Property1", "Property2")

// Convert object to JSON string
def jsonString = exampleObject.asJSON()

// Convert object to XML string
def xmlString = exampleObject.asXML()

// Reconstruct object from JSON string
def reconstructedObject = jsonString.fromJSON(ExampleClass.class)

Alternative Functions for Gloop Services

In the context of Gloop services, JsonFunctions and XmlFunctions are more appropriate. These classes are specifically designed for converting between Gloop objects and XML/JSON formats.

Accessing Marshaller Functions

Marshaller functions are not listed in the functions node in Martini. They are accessible through content assist while writing Groovy classes or using script steps. This approach streamlines the integration of Marshaller capabilities into the development process.

Implementing Marshaller Functions

To use Marshaller functions:

  1. Define the object you wish to convert (e.g., an instance of a custom class) within a Groovy script step.
  2. Use asJSON() or asXML() methods from the Marshaller class for conversion.
  3. Optionally, add a statement to output or process the resulting JSON/XML string.
  4. Execute the script to obtain the serialized representation of your object.

This method provides a simple yet effective way to employ Marshaller functions in Martini for data serialization tasks.