https://github.com/io-sgr/object-flattener
A java library which helps you flatten java object to dot-annotated properties and back to json and/or java object.
https://github.com/io-sgr/object-flattener
Last synced: 10 months ago
JSON representation
A java library which helps you flatten java object to dot-annotated properties and back to json and/or java object.
- Host: GitHub
- URL: https://github.com/io-sgr/object-flattener
- Owner: io-sgr
- License: apache-2.0
- Created: 2020-12-08T07:37:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-08T17:11:14.000Z (about 5 years ago)
- Last Synced: 2025-01-21T00:50:24.235Z (12 months ago)
- Language: Java
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Object Flattener
A java library which helps you flatten java object to single-layer-dot-annotated properties and back to json and/or java object.
## Why properties?
* This is helpful when we need to convert a Java object to config properties file or backwards;
* Or, if you're planning to write structured logs in JSON line, it will be handy to store key-value pairs in MDC and then unflatten them into object and eventually format it to JSON line.
## How to Use
Include following dependency in your `pom.xml`
```xml
io.sgr
object-flattener-jackson
1.0.0
```
Gradle is similar, I'm pretty sure you will know what to do.
## Examples
For instance if you have a Java class defined like this:
```java
public class User {
private final String username;
private final String password;
private final List roles;
@JsonCreator
public User(
@JsonProperty("username") final String username,
@JsonProperty("password") final String password,
@JsonProperty("roles") final List roles
) {
// Details are omitted
}
// Getters are omitted
}
```
You can call `ObjectFlattener.flatten()` to get a flattened single-layer Map like this:
```java
Map flattened = ObjectFlattener.flatten(
new User("John", "Dow", Arrays.asList("User", "Reporter"))
);
```
The content on the map will be:
```properties
username=John
password=Dow
roles.[0]=User
roles.[1]=Reporter
```
You can also unflatten it to JsonNode:
```java
JsonNode node = ObjectFlattener.unflatten(flattened);
```
or Java object:
```java
User user = ObjectFlattener.unflatten(flattened, User.class);
```
## Advanced Usage
* You can pass your own `ObjectMapper` in case you want additional controll of the serialization/deserialization process.
* You can pass a prefix string when flattening to append prefix to generated properties.
* You can pass a prefix string when unflattening so you could only read a subset of the properties.
## License
Copyright 2020-2020 SgrAlpha
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.