use of com.linkedin.data.schema.JsonBuilder 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);
FileOutputStream fileOutputStream = new FileOutputStream(file);
JsonBuilder jsonBuilder = new JsonBuilder(JsonBuilder.Pretty.INDENTED);
SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(jsonBuilder);
jsonBuilder.writeStartObject();
jsonBuilder.writeFieldName(Snapshot.MODELS_KEY);
jsonBuilder.writeStartArray();
List<NamedDataSchema> models = generateModelList();
for (DataSchema model : models) {
encoder.encode(model);
}
jsonBuilder.writeEndArray();
jsonBuilder.writeFieldName(Snapshot.SCHEMA_KEY);
jsonBuilder.writeDataTemplate(_topLevelSchema, true);
jsonBuilder.writeEndObject();
try {
fileOutputStream.write(jsonBuilder.result().getBytes());
} finally {
fileOutputStream.close();
jsonBuilder.close();
}
return file;
}
use of com.linkedin.data.schema.JsonBuilder 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));
schemaToEncode = unionRef.getSchema();
} 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.JsonBuilder in project rest.li by linkedin.
the class SchemaToAvroJsonEncoder method schemaToAvro.
/**
* Serialize a {@link DataSchema} to an Avro-compliant schema as a JSON encoded string.
*
* @param schema is the {@link DataSchema} to build a JSON encoded output for.
* @param fieldDefaultValueProvider provides the default values for each of the fields.
* @param options provides the {@link DataToAvroSchemaTranslationOptions}.
* @return the Avro-compliant schema as JSON encoded string.
*/
static String schemaToAvro(DataSchema schema, SchemaTranslator.FieldDefaultValueProvider fieldDefaultValueProvider, DataToAvroSchemaTranslationOptions options) {
JsonBuilder builder = null;
try {
builder = new JsonBuilder(options.getPretty());
final SchemaToAvroJsonEncoder serializer = new SchemaToAvroJsonEncoder(builder, schema, fieldDefaultValueProvider, options);
serializer.encode(schema);
return builder.result();
} catch (IOException exc) {
throw new IllegalStateException(exc);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
use of com.linkedin.data.schema.JsonBuilder 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