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.
- Host: GitHub
- URL: https://github.com/nithin-sudarsan/data-mapper
- Owner: nithin-sudarsan
- Created: 2023-06-29T08:16:30.000Z (almost 2 years ago)
- Default Branch: main2
- Last Pushed: 2023-08-12T06:59:11.000Z (almost 2 years ago)
- Last Synced: 2025-02-03T22:40:50.665Z (4 months ago)
- Topics: java-library, java8, json, pojobuilder, xml
- Language: Java
- Homepage: https://datamapper-playground.netlify.app
- Size: 15.5 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Data Mapper - Java Library for JSON and XML Parsing
[](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.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
inputPathString
The path to the input file (JSON or XML) to be transformed.
rulesPathString
The path to the rules file containing transformation rules.
classNameClass<T>
The desired class type to be returned after transformation.
inputFile
The input JSON or XML file to be transformed.
rulesFile
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
inputStringString
The JSON or XML string to be transformed.
rulesStringString
The string containing transformation rules.
classNameClass<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
inputBeanObject
The input Java bean to be transformed.
rulesStringString
The rules string containing transformation rules.
classNameClass<T>
The desired class type to be returned after transformation.
rulesFile
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
inputString
The input JSON or XML string to be transformed.
rulesString
The rules string containing transformation rules.
extDataMapperImpl.Extension
The extension indicating the format of the input file (JSON or XML).
inputFile
The input JSON or XML file to be transformed.
rulesFile
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 inputExample 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]) .