Search in sources :

Example 1 with JsonBuilder

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;
}
Also used : JsonBuilder(com.linkedin.data.schema.JsonBuilder) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FileOutputStream(java.io.FileOutputStream) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) File(java.io.File)

Example 2 with JsonBuilder

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();
        }
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) DataTemplate(com.linkedin.data.template.DataTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo)

Example 3 with JsonBuilder

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();
        }
    }
}
Also used : JsonBuilder(com.linkedin.data.schema.JsonBuilder) IOException(java.io.IOException)

Example 4 with JsonBuilder

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();
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException)

Aggregations

JsonBuilder (com.linkedin.data.schema.JsonBuilder)4 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)3 SchemaToJsonEncoder (com.linkedin.data.schema.SchemaToJsonEncoder)3 IOException (java.io.IOException)3 DataSchema (com.linkedin.data.schema.DataSchema)2 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)2 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 HasTyperefInfo (com.linkedin.data.template.HasTyperefInfo)1 TyperefInfo (com.linkedin.data.template.TyperefInfo)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1