Skip to content

HectorBst/jsonschema2pojo-androidx-databinding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Coverage Violations Maven Central License

jsonschema2pojo-androidx-databinding

This project is a jsonschema2pojo extension dedicated to observable entities generation for Android Data Binding.

Features

Observable objects

This extension generate observable data objects as explained here.

E.g., this schema:

{
	"title": "Sample entity",
	"type": "object",
	"properties": {
		"field": {
			"title": "A field",
			"type": "string"
		}
	}
}

Will produce:

public class Entity extends BaseObservable {

	private String field;

	@Bindable
	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
		notifyPropertyChanged(BR.field);
	}
}

Collections and maps

Collection type fields become ObservableList and Map type fields become ObservableMap.

E.g., this schema:

{
	"title": "Sample entity",
	"type": "object",
	"properties": {
		"field": {
			"title": "A list",
			"type": "array",
			"items": {
				"type": "string"
			}
		}
	}
}

Will produce:

public class Entity extends BaseObservable {

	private ObservableList<String> field;

	@Bindable
	public ObservableList<String> getField() {
		return field;
	}

	public void setField(ObservableList<String> field) {
		this.field = field;
		notifyPropertyChanged(BR.field);
	}
}

Disable observability for specific fields or classes

It is possible to exclude fields or classes from observability if they are not concerned by Data Binding, setting the JSON property x-adb-observable to false at property or schema level (if missing or null the value is true).

E.g., this schema:

{
	"title": "Sample entity",
	"type": "object",
	"properties": {
		"field": {
			"title": "A string",
			"type": "string"
		},
		"excludedField": {
			"title": "A string",
			"type": "string",
			"x-adb-observable": false
		}
	}
}

Will produce:

public class Entity extends BaseObservable {

	private String field;

	private String excludedField;

	@Bindable
	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
		notifyPropertyChanged(BR.field);
	}

	public String getExcludedField() {
		return field;
	}

	public void setExcludedField(String field) {
		this.field = field;
	}
}

If x-adb-observable is set to false for an object, this value will be propagated to all properties.

Gradle configuration

Here is an example of how the extension can be added to the jsonschema2pojo Gradle plugin in a Android project.

...

buildscript {
	...
	dependencies {
		...
		//jsonschema2pojo dependency
		classpath "org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:${jsonschema2pojoVersion}"
		//Extension dependency
		classpath "dev.hctbst:jsonschema2pojo-androidx-databinding:${jsonschema2pojoDataBindingVersion}"
	}
}

apply plugin: 'com.android.library'
apply plugin: 'jsonschema2pojo'

android {
	...

	buildFeatures {
		//Enable data binding
		dataBinding = true
	}
}

...

jsonSchema2Pojo {
	...
	//Extension RuleFactory
	customRuleFactory = 'dev.hctbst.jsonschema2pojo.androidx.databinding.AndroidDataBindingRuleFactory'
}

A more complete example testable in an Android application is available here.

License

This project is released under version 2.0 of the Apache License.