Search in sources :

Example 26 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class Conversions method dataSchemaToDataMap.

/**
 * Return a {@link DataMap} representation of the provided {@link NamedDataSchema}.
 *
 * @param schema provides the {@link NamedDataSchema}.
 * @return a {@link DataMap} representation of the provided {@link NamedDataSchema}.
 */
public static DataMap dataSchemaToDataMap(NamedDataSchema schema) {
    String inputSchemaAsString = schema.toString();
    try {
        JacksonDataCodec codec = new JacksonDataCodec();
        DataMap schemaAsDataMap = codec.stringToMap(inputSchemaAsString);
        return schemaAsDataMap;
    } catch (IOException e) {
        // codec.readMap from JSON generated from a schema should always be successful.
        throw new IllegalStateException(UNEXPECTED_IOEXCEPTION + inputSchemaAsString, e);
    }
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 27 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class Conversions method dataMapToDataSchema.

/**
 * Return a {@link DataSchema} for the provided {@link DataMap}.
 *
 * @param map provides the {@link DataMap} representation of a JSON-encoded {@link DataSchema}.
 * @param parser provides the {@link SchemaParser} that will be used for parsing the map representation of the schema.
 * @return a {@link DataSchema} for the provided {@link DataMap} or null if the map does not represent a valid schema,
 *         parse errors can be obtained from the provided {@link SchemaParser}.
 */
public static DataSchema dataMapToDataSchema(DataMap map, PegasusSchemaParser parser) {
    // Convert DataMap into DataSchema
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JacksonDataCodec codec = new JacksonDataCodec();
    try {
        codec.writeMap(map, outputStream);
    } catch (IOException e) {
        // This should never occur
        throw new IllegalStateException(UNEXPECTED_IOEXCEPTION + map, e);
    }
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    parser.parse(inputStream);
    List<DataSchema> topLevelDataSchemas = parser.topLevelDataSchemas();
    assert (topLevelDataSchemas.size() <= 1);
    if (parser.hasError()) {
        return null;
    } else if (topLevelDataSchemas.size() != 1) {
        // This should never occur
        throw new IllegalStateException(WRONG_NUMBER_OF_SCHEMA_LEFT + topLevelDataSchemas);
    }
    return topLevelDataSchemas.get(0);
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)27 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)7 DataMap (com.linkedin.data.DataMap)6 BeforeTest (org.testng.annotations.BeforeTest)5 BackupRequestsConfiguration (com.linkedin.d2.BackupRequestsConfiguration)3 BoundedCostBackupRequests (com.linkedin.d2.BoundedCostBackupRequests)3 DataCodec (com.linkedin.data.codec.DataCodec)3 ByteString (com.linkedin.data.ByteString)2 BsonDataCodec (com.linkedin.data.codec.BsonDataCodec)2 PsonDataCodec (com.linkedin.data.codec.PsonDataCodec)2 EmptyRecord (com.linkedin.restli.common.EmptyRecord)2 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)1