Partialize is a Java based library helping you to implement the JSON partial responses on your project.
The GitHub issue tracker is only for bug reports and feature requests. Anything
else, such as questions for help in using the library, should be posted in
StackOverflow
under tags partialize
and java
.
To compile Partialize, you must ensure that Java 9 and Maven are correctly installed.
#> mvn compile
#> mvn package
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.thibaultmeyer</groupId>
<artifactId>partialize</artifactId>
<version>YY.MM</version>
</dependency>
fields=firstName,lastName,posts(title,createDate)
fields=firstName,lastName,posts(*)
@Partialize(
allowedFields = {"uid", "firstName", "lastName", "emails", "createDate"},
defaultFields = {"uid", "firstName", "lastName"}
)
public class AccountModel {
private UUID uid;
private String firstName;
private String lastName;
private String password;
private List<AccountEmailModel> emails;
private DateTime createDate;
/* ADD GETTER / SETTER METHODS HERE */
}
@Partialize(
allowedFields = {"uid", "email", "isDefault"},
defaultFields = {"email", "isDefault"}
)
public class AccountEmailModel {
private UUID uid;
private String email;
private Boolean isDefault;
/* ADD GETTER / SETTER METHODS HERE */
}
public class JodaDateTimeConverter implements Converter<DateTime> {
private static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
@Override
public void convert(final String fieldName, final DateTime data, final ObjectNode node) {
node.put(fieldName, data.toString(JodaDateTimeConverter.DATETIME_FORMAT));
}
@Override
public void convert(final String fieldName, final DateTime data, final ArrayNode node) {
node.add(data.toString(JodaDateTimeConverter.DATETIME_FORMAT));
}
@Override
public Class<DateTime> getManagedObjectClass() {
return DateTime.class;
}
}
PartializeConverterManager.getInstance().registerConverter(new JodaDateTimeConverter());
final AccountModel account = AccountModel.find().where().eq("id", 1).findUnique();
final String fields = "firstName,lastName,emails(email,isDefault),createDate";
final Partialize partialize = new Partialize();
final ContainerNode result = partialize.buildPartialObject(fields, AccountModel.class, account);
System.out.println(result);
{
"firstName": "John",
"lastName": "Smith",
"emails": [
{
"email": "john.smith@domain.local",
"isDefault": true
},
{
"email": "john@domain.local",
"isDefault": false
}
],
"createDate": "2016-01-15T23:45:12"
}
final AccountModel account = AccountModel.find().where().eq("id", 1).findUnique();
final String fields = "alias1,alias2";
final Partialize partialize = new Partialize();
partialize.setAliases(new HashMap<String, String>() {{
put("alias1", "firstName");
put("alias2", "lastName");
}});
final ContainerNode result = partialize.buildPartialObject(fields, AccountModel.class, account);
System.out.println(result);
final AccountModel account = AccountModel.find().where().eq("id", 1).findUnique();
final String fields = "uid,firstName,lastName,password";
final Partialize partialize = new Partialize();
partialize.setAccessPolicy(accessPolicy -> {
return Arrays.asList(
"AccountModel.uid",
"AccountModel.firstName",
"AccountModel.lastName"
).contains(accessPolicy.clazz.getSimpleName() + "." + accessPolicy.field);
});
final ContainerNode result = partialize.buildPartialObject(fields, AccountModel.class, account);
System.out.println(result);
This project is released under terms of the MIT license.