}
@Override
public <T> boolean visit(FlatPackEntity<T> entity, Codex<T> codex,
VisitorContext<FlatPackEntity<T>> ctx) {
JsonWriter json = context.getWriter();
try {
json.beginObject();
// data : { typeName : [ { entity }, { entity } ]
json.name("data");
json.beginObject();
for (Map.Entry<Class<? extends HasUuid>, List<HasUuid>> entry : collate(
context.getEntities()).entrySet()) {
json.name(typeContext.describe(entry.getKey()).getTypeName());
json.beginArray();
for (HasUuid value : entry.getValue()) {
if (persistenceMapper.isPersisted(value)) {
persistent.add(value);
}
visitors.visit(this, value);
}
json.endArray();
}
json.endObject(); // end data
// value : ['type', 'uuid']
json.name("value");
codex.write(entity.getValue(), context);
// errors : { 'foo.bar.baz' : 'May not be null' }
Set<ConstraintViolation<?>> violations = entity.getConstraintViolations();
Map<String, String> errors = entity.getExtraErrors();
if (!violations.isEmpty() || !errors.isEmpty()) {
json.name("errors");
json.beginObject();
for (ConstraintViolation<?> v : violations) {
json.name(v.getPropertyPath().toString());
json.value(v.getMessage());
}
for (Map.Entry<String, String> entry : errors.entrySet()) {
json.name(entry.getKey()).value(entry.getValue());
}
json.endObject(); // errors
}
// Write metadata for any entities
if (!persistent.isEmpty()) {
json.name("metadata");
json.beginArray();
for (HasUuid toWrite : persistent) {
EntityMetadata meta = new EntityMetadata();
meta.setPersistent(true);
meta.setUuid(toWrite.getUuid());
visitors.visit(this, meta);
}
json.endArray(); // metadata
}
// Write extra top-level data keys, which are only used for simple side-channel data
for (Map.Entry<String, String> entry : entity.getExtraData().entrySet()) {
json.name(entry.getKey()).value(entry.getValue());
}
// Write extra warnings, some of which may be from the serialization process
Map<UUID, String> codexWarnings = context.getWarnings();
Map<String, String> warnings = entity.getExtraWarnings();
if (!codexWarnings.isEmpty() || !warnings.isEmpty()) {
json.name("warnings");
json.beginObject();
for (Map.Entry<UUID, String> entry : codexWarnings.entrySet()) {
json.name(entry.getKey().toString()).value(entry.getValue());
}
for (Map.Entry<String, String> entry : warnings.entrySet()) {
json.name(entry.getKey()).value(entry.getValue());
}
json.endObject(); // warnings
}
json.endObject(); // core payload
} catch (IOException e) {
context.fail(e);
}