use of com.linkedin.data.schema.SchemaToJsonEncoder in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
final DataSchema schemaToEncode;
if (dataSchema instanceof TyperefDataSchema) {
return ((TyperefDataSchema) dataSchema).getFullName();
} else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
return dataSchema.getUnionMemberKey();
} else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
return unionRef.getSchema().getFullName();
} else {
schemaToEncode = dataSchema;
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schemaToEncode);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
use of com.linkedin.data.schema.SchemaToJsonEncoder in project rest.li by linkedin.
the class SchemaFormatTranslator method encode.
private String encode(DataSchema schema) throws IOException {
if (_destFormat.equals(PdlSchemaParser.FILETYPE)) {
StringWriter writer = new StringWriter();
SchemaToPdlEncoder encoder = new SchemaToPdlEncoder(writer);
encoder.setTypeReferenceFormat(AbstractSchemaEncoder.TypeReferenceFormat.PRESERVE);
encoder.encode(schema);
return writer.toString();
} else if (_destFormat.equals(SchemaParser.FILETYPE)) {
JsonBuilder.Pretty pretty = JsonBuilder.Pretty.INDENTED;
JsonBuilder builder = new JsonBuilder(pretty);
try {
SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.PRESERVE);
if (_forcePdscFullyQualifiedNames) {
encoder.setAlwaysUseFullyQualifiedName(true);
}
encoder.encode(schema);
return builder.result();
} finally {
builder.closeQuietly();
}
} else {
throw new IllegalArgumentException("Unsupported format: " + _destFormat);
}
}
use of com.linkedin.data.schema.SchemaToJsonEncoder in project rest.li by linkedin.
the class SnapshotGenerator method writeFile.
public File writeFile(File outdirFile, String fileName) throws IOException {
fileName += RestConstants.SNAPSHOT_FILENAME_EXTENTION;
final File file = new File(outdirFile, fileName);
try (JsonBuilder jsonBuilder = new JsonBuilder(JsonBuilder.Pretty.INDENTED, new FileWriter(file))) {
SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(jsonBuilder);
jsonBuilder.writeStartObject();
jsonBuilder.writeFieldName(Snapshot.MODELS_KEY);
jsonBuilder.writeStartArray();
List<NamedDataSchema> models = generateModelList();
models.sort(Comparator.comparing(NamedDataSchema::getFullName));
for (DataSchema model : models) {
encoder.encode(model);
}
jsonBuilder.writeEndArray();
jsonBuilder.writeFieldName(Snapshot.SCHEMA_KEY);
jsonBuilder.writeDataTemplate(_topLevelSchema, true);
jsonBuilder.writeEndObject();
jsonBuilder.flush();
}
return file;
}
use of com.linkedin.data.schema.SchemaToJsonEncoder in project rest.li by linkedin.
the class SchemaToAvroJsonEncoder method encodePropertiesWithEmbeddedSchema.
private void encodePropertiesWithEmbeddedSchema(DataSchema schema) throws IOException {
Object dataProperty = null;
for (Map.Entry<String, Object> entry : schema.getProperties().entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (key.equals(SchemaTranslator.DATA_PROPERTY)) {
dataProperty = value;
} else {
_builder.writeFieldName(key);
_builder.writeData(value);
}
}
_builder.writeFieldName(SchemaTranslator.DATA_PROPERTY);
_builder.writeStartObject();
_builder.writeFieldName(SchemaTranslator.SCHEMA_PROPERTY);
SchemaToJsonEncoder schemaToJsonEncoder = new SchemaToJsonEncoder(_builder);
schemaToJsonEncoder.encode(schema);
_builder.writeFieldName(SchemaTranslator.OPTIONAL_DEFAULT_MODE_PROPERTY);
_builder.writeString(_options.getOptionalDefaultMode().toString());
if (dataProperty != null && dataProperty.getClass() == DataMap.class) {
for (Map.Entry<String, Object> pegasusEntry : ((DataMap) dataProperty).entrySet()) {
String key = pegasusEntry.getKey();
if (RESERVED_DATA_PROPERTIES.contains(key) == false) {
_builder.writeFieldName(pegasusEntry.getKey());
_builder.writeData(pegasusEntry.getValue());
}
}
}
_builder.writeEndObject();
}
use of com.linkedin.data.schema.SchemaToJsonEncoder in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
/*package*/
static String buildDataSchemaType(DataSchema schema) {
if (schema instanceof PrimitiveDataSchema || schema instanceof NamedDataSchema) {
return schema.getUnionMemberKey();
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schema);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + schema.toString() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
Aggregations