Skip to content

Latest commit

 

History

History
327 lines (280 loc) · 10.9 KB

README.md

File metadata and controls

327 lines (280 loc) · 10.9 KB

Data Mapper - Java Library for JSON and XML Parsing

License

Data Mapper is a powerful Java library that offers a comprehensive solution for parsing, transforming, and mapping JSON or XML files to Java beans, driven by a given set of rules. It is designed to simplify the process of data manipulation and conversion in your Java projects.

With the latest version, Data Mapper introduces a new feature that allows seamless transformation of one Java bean to another type. Now, you can effortlessly convert your Java beans between different classes, making it even more versatile and adaptable to various data processing scenarios.

Table of Contents

  1. Features
  2. Installation
  3. Methods Exposed
  4. Operations Supported
  5. Input File Structures
  6. Contact

Features

  • Effortlessly parse JSON and XML files into Java beans.
  • Seamlessly transform JSON and XML strings into Java beans.
  • Preview the transformed JSON or XML as a string.
  • Flexibly transform one Java bean type into another using specified rules.
  • Transform data using custom rules to match specific requirements.
  • Exceptionally fast and lightweight for optimal performance.

Installation

To use Data Mapper in your project, include the following dependency:

<dependency>
    <groupId>org.perfios</groupId>
    <artifactId>data-mapper</artifactId>
    <version>1.1.1</version>
</dependency>

Methods exposed

1. transformFile

Transforms a JSON or XML file according to specified rules and returns an instance of the desired class type.
The overloaded version of this method accepts JSON or XML along with the rules as File type to perform the same.

Prameters expected

inputPath String
The path to the input file (JSON or XML) to be transformed.
rulesPath String
The path to the rules file containing transformation rules.
className Class<T>
The desired class type to be returned after transformation.
input File
The input JSON or XML file to be transformed.
rules File
The rules file containing transformation rules.

Returns

An instance of the desired class type, representing the transformed data.

Usage

import org.perfios.DataMapper;
import org.perfios.DataMapperImpl;

public class Usage
{
	public static void main(String[] args) {
		// Create an instance of the DataMapper interface
        DataMapper dataMapper = new DataMapperImpl();

        // Specify the paths to the input JSON file and the rules file
        String inputPath = "/path/to/input.json";
        String rulesPath = "/path/to/rules.txt";
        
        // Define the desired Java bean class (Employee in this case)
        Class<Employee> desiredClass = Employee.class;

        // Transform the JSON file using the specified rules and class type
        Employee transformedEmployee = dataMapper.transformFile(inputPath, rulesPath, desiredClass);
	}
}

2. transformString

Transforms a JSON or XML string according to specified rules and returns an instance of the desired class type.

Prameters expected

inputString String
The JSON or XML string to be transformed.
rulesString String
The string containing transformation rules.
className Class<T>
The desired class type to be returned after transformation.

Returns

An instance of the desired class type, representing the transformed data.

Usage

import org.perfios.DataMapper;
import org.perfios.DataMapperImpl;

public class Usage
{
	public static void main(String[] args) {
		// Create an instance of the DataMapper interface
        DataMapper dataMapper = new DataMapperImpl();

        // Specify the paths to the input JSON file and the rules file
        String inputPath = "/path/to/input.json";
        String rulesPath = "/path/to/rules.txt";
        
        // Read the contents of the input JSON file and rules file into strings
        String inputString = new String(Files.readAllBytes(Paths.get(inputPath.getAbsolutePath())));
        String rulesString = new String(Files.readAllBytes(Paths.get(rulesPath.getAbsolutePath())));
       
        
        // Define the desired Java bean class (Employee in this case)
        Class<Employee> desiredClass = Employee.class;

        // Transform the JSON file using the specified rules and class type
        Employee transformedEmployee = dataMapper.transformString(inputString, rulesString, desiredClass);
	}
}

3. transformBean

Transforms an input Java Bean according to specified rules and returns another Java bean of the desired type.
The overloaded version of this method accepts the transformation rules as File type to perform the same.

Prameters expected

inputBean Object
The input Java bean to be transformed.
rulesString String
The rules string containing transformation rules.
className Class<T>
The desired class type to be returned after transformation.
rules File
The rules file containing transformation rules.

Returns

An instance of the desired class type, representing the transformed data.

Usage

import org.perfios.DataMapper;
import org.perfios.DataMapperImpl;

public class Usage
{
	public static void main(String[] args) {
		// Create an instance of the DataMapper interface
        DataMapper dataMapper = new DataMapperImpl();

        // Specify the paths to the input Java bean and the rules file
        EmployeeTypeA inputBean = new EmployeeTypeA("emp_id","emp_name","emp_salary");
        File rules= new File("/path/to/rules.txt");
        
        // Define the desired Java bean class (Employee in this case)
        Class<EmployeeTypeB> desiredClass = EmployeeTypeB.class;

        // Transform the JSON file using the specified rules and class type
        EmployeeTypeB transformedEmployee = dataMapper.transformFile(inputBean, rules, desiredClass);
	}
}

4. getTransformedString

Transforms an input JSON or XML string according to specified rules and returns a string representing the transformed input in the desired extension format.
The overloaded version of this method accepts JSON or XML along with the rules as File type to perform the same.

Prameters expected

input String
The input JSON or XML string to be transformed.
rules String
The rules string containing transformation rules.
ext DataMapperImpl.Extension
The extension indicating the format of the input file (JSON or XML).
input File
The input JSON or XML file to be transformed.
rules File
The rules file containing transformation rules.

Returns

A string representing the transformed JSON or XML input in the desired extension format.

Usage

import org.perfios.DataMapper;
import org.perfios.DataMapperImpl;

public class Usage
{
	public static void main(String[] args) {
		// Create an instance of the DataMapper interface
        DataMapper dataMapper = new DataMapperImpl();

        //Create File objects of input file and rules file using their respective paths
        File input = new File("/path/to/input.json");
        File rules = new File("/path/to/rules.txt");

        // Transform the JSON file using the specified rules and desired output type
        String transformedEmployee = dataMapper.getTransformedString(input, rules, DataMapperImpl.Extension.XML);
	}
}

Operations Supported

#add "Facilitates addition of numbers or concatenation of two input fields."
#sub Facilitates subtraction of numbers between two fields from input file
#mul Facilitates multiplication of numbers between two fields from input file
#div Facilitates division of numbers between two fields from input file
#default Facilitates setting default values for fields.

Example Input File Structures and Output

JSON or XML input

Example Input JSON File Example Input XML File
{
  "details": {			
    "name": {
      "firstName": "John",
      "lastName": "Doe"
    },
    "addresses": [
      {
        "address1": {
          "phoneNumber": 1234567890,
          "state": "California"
        }
      },
      {
        "address2": {
          "phoneNumber": 9876543210,
          "state": "New York"
        }
      }
    ]
  }
}
<root>
    <details>
        <name>
            <firstName>Jane</firstName>
            <lastName>Doe</lastName>
	</name>
	<addresses>
	    <address1>
	        <phoneNumber>5555555555</phoneNumber>
		<state>Texas</state>
	    </address1>
	</addresses>
	<addresses>
	    <address2>
		<phoneNumber>1234567890</phoneNumber>
		<state>Washington</state>
	    </address2>
	</addresses>
    </details>
</root>

Example Rules File:

rules.txt

PersonalInfo/name = details/name/firstName #add details/name/lastName
PersonalInfo/details = details/addresses[]/address1/state
PersonalInfo/age = #default 25

Output Java bean structure

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class PersonalInfo {
    String name;
    String age;
    Object details;
}

Contact

If you have any questions or feedback, feel free to reach out to us via e-mail .