Skip to content

Home

Introduction

Development

IDEs

Services

API Explorer

Releases

Release notes

Martini Runtime

Martini Desktop

Martini Online

Feature requests

Bug reports

examples package: Using the Alias property

Gloop gives developers the ability to rename Gloop model properties during serialization1 using the Alias property. With this property, you can name a Gloop model property after a keyword2 or change it to something else so that it matches the requirements of your API.

The examples package demonstrates how the Alias property works via the PrintPerson.gloop service. Once this service is executed, it will transform the input to XML and JSON (renaming properties with aliases) and log the result to the console.

Related articles

Please see the following articles for more information:

Try it!

Under the Navigator, expand the examples package and navigate to the code folder. Afterwards, look for the aliasForSerializing package. This package contains two files, shown below:

1
2
3
4
5
6
7
examples
├── ...
└── code
    └── aliasForSerializing
        ├── model
           └──Person.model
        └── PrintPerson.gloop

Running the PrintPerson.gloop service will provide an output similar to that shown below.

Output of PrintPerson.gloop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
24/04/18 13:42:26.340 INFO  [Martini]
<?xml version="1.0"?>
<input phone number="941-746-6620">
    <firstName>John</firstName>
    <last_name>Doe</last_name>
    <invalidNames _3mail="john.doe@example.com" %grade="97.0">
        <middle-name>Bambi</middle-name>
        <SSN#>220-90-5004</SSN#>
    </invalidNames>
</input>
24/04/18 13:42:26.384 INFO  [Martini]
{
    "input": {
        "firstName": "John",
        "last_name": "Doe",
        "invalidNames": {
            "middle-name": "Bambi",
            "SSN#": "220-90-5004",
            "_3mail": "john.doe@example.com",
            "%grade": 97.0
        },
        "phone number": "941-746-6620"
    }
}

But what exactly are we looking at and what's so special about this service?

Explanation

This example shows how we can serialize a JSON or XML attribute/element to a name we want. A Gloop model, when provided with an invalid property name, will attempt to make it a valid Gloop and Groovy name. The desired name will be configured as the property's Alias instead. And during serialization or deserialization to JSON or XML, Gloop will use the Alias property.

The Person.model Gloop model contains invalid property names, each assigned an alias for when it gets serialized. After the serialization, instead of the Gloop-assigned property names, the value of the Alias property will be used instead to write the output. When a property has an Alias, it will appear in brackets are the property itself in Gloop, as shown below:

Screenshot showing a Gloop property with an alias

Generate models from an existing source

You can generate Gloop models from an existing Gloop model, or a JSON or XML string, file, or schema.


  1. For example, creating the JSON representation of a Gloop model or creating a Gloop model out of a JSON string 

  2. Effects are only visible after serialization.