Search in sources :

Example 1 with SchemaToJsonEncoder

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();
        }
    }
}
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 2 with SchemaToJsonEncoder

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

Example 3 with SchemaToJsonEncoder

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;
}
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) FileWriter(java.io.FileWriter) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) File(java.io.File)

Example 4 with SchemaToJsonEncoder

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();
}
Also used : SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 5 with SchemaToJsonEncoder

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();
        }
    }
}
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

SchemaToJsonEncoder (com.linkedin.data.schema.SchemaToJsonEncoder)5 JsonBuilder (com.linkedin.data.schema.JsonBuilder)4 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)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 IOException (java.io.IOException)2 DataMap (com.linkedin.data.DataMap)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 SchemaToPdlEncoder (com.linkedin.data.schema.SchemaToPdlEncoder)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 FileWriter (java.io.FileWriter)1 StringWriter (java.io.StringWriter)1