Skip to content

Files

bank

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Apr 2, 2025
Jul 18, 2019
Oct 15, 2023
Feb 28, 2024

JSONx Sample: Bank Message

JSON Schema for the enterprise

Build Status Coverage Status Javadocs Released Version Snapshot Version

Abstract

This document presents the Bank Message sample application.

Table of Contents

  1 Bank
  2 Contributing
  3 Special Thanks
  4 License

1 Bank

This sample is an introduction to the following JSONx technologies:

  1. JSON Schema Definition Language (JSD).
  2. JSONx Runtime API.

This example presents a simple schema that represents a message with a bank account number. The message can have one of three account number types: SWIFT, IBAN, and ACH. To help banking systems reduce software risk, organizations governing the SWIFT, IBAN and ACH code standards define simple test functions to determine the logical correctness of identifiers.

  1. 𝒗SWIFT(id) = regexid( "[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?" )
  2. 𝒗IBAN (id) = regexid( "[A-Z]{2}\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4} ?\d{0,2}" )
  3. 𝒗ACH(id, rt) = regexid( "\w{1,17}" ) × regexrt( "\d{9}" )

The following schema presents a message declaration for each type of identifier with regex constraints.

  1. Create message.jsd or message.jsdx in src/test/resources/:

JSD
{
  "doc": "Schema describing bank transactions",
  "jx:ns": "http://www.jsonx.org/schema-0.4.jsd",
  "jx:schemaLocation": "http://www.jsonx.org/schema-0.4.jsd http://www.jsonx.org/schema.jsd",
  "message": { "jx:type": "object", "abstract": true },
  "swift": {
    "jx:type": "object", "extends": "message", "properties": {
      "type": { "jx:type": "string", "pattern": "swift", "nullable": false },
      "code": { "jx:type": "string", "pattern": "[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?", "nullable": false }
    }
  },
  "iban": {
    "jx:type": "object", "extends": "message", "properties": {
      "type": { "jx:type": "string", "pattern": "iban", "nullable": false },
      "code": { "jx:type": "string", "pattern": "[A-Z]{2}\\d{2} ?\\d{4} ?\\d{4} ?\\d{4} ?\\d{4} ?\\d{0,2}", "nullable": false }
    }
  },
  "ach": {
    "jx:type": "object", "extends": "message", "properties": {
      "type": { "jx:type": "string", "pattern": "ach", "nullable": false },
      "code": { "jx:type": "string", "pattern": "\\w{1,17}", "nullable": false },
      "routing": { "jx:type": "string", "pattern": "\\d{9}", "nullable": false }
    }
  }
}
JSDx
<schema
  doc="Schema describing bank transactions"
  xmlns="http://www.jsonx.org/schema-0.4.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.jsonx.org/schema-0.4.xsd http://www.jsonx.org/schema.xsd">
  <object name="message" abstract="true"/>
  <object name="swift" extends="message">
    <property name="type" xsi:type="string" pattern="swift" nullable="false"/>
    <property name="code" xsi:type="string" pattern="[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?" nullable="false"/>
  </object>
  <object name="iban" extends="message">
    <property name="type" xsi:type="string" pattern="iban" nullable="false"/>
    <property name="code" xsi:type="string" pattern="[A-Z]{2}\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4} ?\d{0,2}" nullable="false"/>
  </object>
  <object name="ach" extends="message">
    <property name="type" xsi:type="string" pattern="ach" nullable="false"/>
    <property name="code" xsi:type="string" pattern="\w{1,17}" nullable="false"/>
    <property name="routing" xsi:type="string" pattern="\d{9}" nullable="false"/>
  </object>
</schema>

Note: You can use the Converter utility to automatically convert between JSD and JSDx.

  2. With the message.jsd or message.jsdx, you can use the jsonx-maven-plugin to automatically generate the Java class files. In your POM, add:

<plugin>
  <groupId>org.jsonx</groupId>
  <artifactId>jsonx-maven-plugin</artifactId>
  <version>0.4.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <phase>generate-test-sources</phase>
      <configuration>
        <destDir>${project.build.directory}/generated-test-sources/jsonx</destDir>
        <namespacePackages>
          <namespacePackage package="com.example.bank."/>
        </namespacePackages>
        <schemas>
          <schema>src/test/resources/message.jsd</schema> <!-- or message.jsdx -->
        </schemas>
      </configuration>
    </execution>
  </executions>
</plugin>

  3. (Alternatively) Create the Java class files by hand:

Note: Set-ters and get-ters have been replaced with public fields for conciseness.

import org.jsonx.*;

public abstract class Message implements JxObject {
}
import org.jsonx.*;

public class Swift extends Message {
  @StringProperty(pattern="swift", nullable=false)
  public String type;

  @StringProperty(pattern="[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?", nullable=false)
  public String code;
}
import org.jsonx.*;

public class Iban extends Message {
  @StringProperty(pattern="iban", nullable=false)
  public String type;

  @StringProperty(pattern="[A-Z]{2}\\d{2} ?\\d{4} ?\\d{4} ?\\d{4} ?\\d{4} ?\\d{0,2}", nullable=false)
  public String code;
}
import org.jsonx.*;

public class Ach extends Message {
  @StringProperty(pattern="ach", nullable=false)
  public String type;

  @StringProperty(pattern="\\w{1,17}", nullable=false)
  public String code;

  @StringProperty(pattern="\\d{9}", nullable=false)
  public String routing;
}

  4. You can use these classes to represent Messages of type Swift, Iban, and Ach.

List<Message> messages = new ArrayList<>();

Swift swift = new Swift();
swift.setType("swift");
swift.setCode("CTBAAU2S");
messages.add(swift);

Iban iban = new Iban();
iban.setType("iban");
iban.setCode("DE91 1000 0000 0123 4567 89");
messages.add(iban);

Ach ach = new Ach();
ach.setType("ach");
ach.setCode("379272957729384");
ach.setRouting("021000021");
messages.add(ach);

  5. You can now marshal the Java objects to JSON:

for (Message message : messages) {
  String json = JxEncoder._2.marshal(message);
  System.out.println(json);
}

... will produce:

{
  "type": "swift",
  "code": "CTBAAU2S"
}
{
  "type": "iban",
  "code": "DE91 1000 0000 0123 4567 89"
}
{
  "type": "ach",
  "code": "379272957729384",
  "routing": "021000021"
}

  6. You can also parse the JSON into Java objects:

for (Message message : messages) {
  String json = JxEncoder._2.marshal(message);
  System.out.println(json);

  Message json2 = JxDecoder.parseObject(message.getClass(), json);
  assertEquals(json, json2);
}

The code included in this module implements this example.

2 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

3 Special Thanks

Java Profiler
Special thanks to EJ Technologies for providing their award winning Java Profiler (JProfiler) for development of the JSONx Framework.

4 License

This project is licensed under the MIT License - see the LICENSE.txt file for details.