Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Info

This project shows filtering JSON in a customized fashion e.g. before loading the test results into Elastic Search with custom property

public class ArtistSerializer implements JsonSerializer<Artist> {
	private List<String> reportFields = new ArrayList<>();
	public void setReportFields(List<String> data) {
		reportFields = data;
	}

	public void setReportFields(String... fields) {
		for (int cnt = 0; cnt != fields.length; cnt++) {
			reportFields.add(fields[cnt]);
		}
	}
	public void resetReportFields() {
		reportFields.clear();
	}

	@Override
	public JsonElement serialize(final Artist data, final Type type,
			final JsonSerializationContext context) {
		JsonObject result = new JsonObject();

		if (reportFields.size() == 0 || reportFields.contains("name")) {
			String name = data.getName();
			if (name != null && !name.isEmpty()) {
				result.add("name", new JsonPrimitive(name));
			}
		}
		return result;
	}
}

that controls which fields to serialize. It may be usefule e.g. when indexing the test results on ELK

The class is used in the adapter role:

ArtistSerializer serializer = new ArtistSerializer();
Gson gson = new GsonBuilder().registerTypeAdapter(Artist.class, new ArtistSerializer()).create();
serializer.setReportFields("name");
Artist artist = new Artist(1, "Paul", "vocals")
JsonElement rowJson = serializer.serialize(artist, null, null);
System.err.println("JSON serialization or artist:\n" + rowJson.toString());

it will print:

{
  "id": 1,
  "staticInfo": "4e61c229-1555-4135-b938-4ddf4d40aa82",
  "name": "paul"
}

while if

serializer.resetReportFields();

was called then

{
  "id": 1,
  "staticInfo": "7b2ade68-3800-4d1a-9b72-7d22d9482a35",
  "name": "paul",
  "plays": "vocals"
}

will be printed (the id and guid fields are shown by default and do not need to ne added to the reportFields

TODO

The names of getter methods may be funky. One may Determine method name through reflection, and once caching is implement, improve the class above (repeated discovery could be expensive)

See Also

Author

Serguei Kouzmine