com.monitorjbl.json.JsonViewSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-view Show documentation
Show all versions of json-view Show documentation
Provides programmatic exclusion/inclusion for Jackson JSON serialization
package com.monitorjbl.json;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
public class JsonViewSerializer extends JsonSerializer {
private final int cacheSize;
public JsonViewSerializer() {
this(1000);
}
public JsonViewSerializer(int cacheSize) {
this.cacheSize = cacheSize;
}
@Override
public void serialize(JsonView result, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
new JsonWriter(jgen, result, cacheSize).write(null, result.getValue());
}
static class JsonWriter {
//caches the results of the @JsonIgnore test to cut down on expensive reflection calls
static final Map hasJsonIgnoreCache = new ConcurrentHashMap<>();
Stack path = new Stack<>();
String currentPath = "";
Match currentMatch = null;
final JsonGenerator jgen;
final JsonView result;
final int cacheSize;
JsonWriter(JsonGenerator jgen, JsonView result, int cacheSize) {
this.jgen = jgen;
this.result = result;
this.cacheSize = cacheSize;
}
boolean writePrimitive(Object obj) throws IOException {
if (obj instanceof String) {
jgen.writeString((String) obj);
} else if (obj instanceof Date) {
jgen.writeNumber(((Date) obj).getTime());
} else if (obj instanceof Integer) {
jgen.writeNumber((Integer) obj);
} else if (obj instanceof Long) {
jgen.writeNumber((Long) obj);
} else if (obj instanceof Short) {
jgen.writeNumber((Short) obj);
} else if (obj instanceof Double) {
jgen.writeNumber((Double) obj);
} else if (obj instanceof Float) {
jgen.writeNumber((Float) obj);
} else if (obj instanceof Character) {
jgen.writeNumber((Character) obj);
} else if (obj instanceof Byte) {
jgen.writeNumber((Byte) obj);
} else if (obj instanceof Boolean) {
jgen.writeBoolean((Boolean) obj);
} else {
return false;
}
return true;
}
private boolean writeEnum(Object obj) throws IOException {
if (obj.getClass().isEnum()) {
jgen.writeString(obj.toString());
} else {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
boolean writeList(Object obj) throws IOException {
if (obj instanceof List || obj instanceof Set || obj.getClass().isArray()) {
Iterable iter;
if (obj.getClass().isArray()) {
if (obj instanceof byte[]) {
jgen.writeBinary((byte[]) obj);
return true;
} else {
iter = convertArray(obj);
}
} else {
iter = (Iterable