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