An open API service indexing awesome lists of open source software.

https://github.com/nithin-sudarsan/data-mapper

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.
https://github.com/nithin-sudarsan/data-mapper

java-library java8 json pojobuilder xml

Last synced: 5 days ago
JSON representation

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.

Awesome Lists containing this project

README

        

# Data Mapper - Java Library for JSON and XML Parsing

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

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 ](#features)
2. [ Installation ](#installation)
3. [ Methods Exposed ](#methods)
4. [ Operations Supported ](#operations)
5. [ Input File Structures ](#inputoutput)
6. [ Contact ](#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:

```xml

org.perfios
data-mapper
1.1.1

```


## 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
```java
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 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
```java
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 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
```java
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 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
```java
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

```json
{
"details": {
"name": {
"firstName": "John",
"lastName": "Doe"
},
"addresses": [
{
"address1": {
"phoneNumber": 1234567890,
"state": "California"
}
},
{
"address2": {
"phoneNumber": 9876543210,
"state": "New York"
}
}
]
}
}

```

```xml



Jane
Doe



5555555555
Texas




1234567890
Washington


```

### 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
```java
@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](mailto:[email protected]) .