@Override public void serialize(TableLayoutDesc layout, JsonGenerator generator, SerializerProvider provider) throws IOException { String jsonString = ToJson.toAvroJsonString(layout); JsonNode node = mJsonMapper.readTree(jsonString); generator.writeTree(node); }
/** {@inheritDoc} */ @Override public String toString() { try { return ToJson.toJsonString(mDesc); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
final Iterable<Object> javaArray = (Iterable<Object>) value; for (Object element : javaArray) { jsonArray.add(toJsonNode(element, schema.getElementType())); final Map<String, Object> javaMap = (Map<String, Object>) value; for (Map.Entry<String, Object> entry : javaMap.entrySet()) { jsonObject.put(entry.getKey(), toJsonNode(entry.getValue(), schema.getValueType())); final JsonNode fieldNode = toJsonNode(fieldValue, field.schema()); case UNION: return toUnionJsonNode(value, schema);
/** * Encodes an Avro value into a JSON string. * * Fields with default values are omitted. * * @param value Avro value to encode. * @param schema Avro schema of the value. * @return Pretty string representation of the JSON-encoded value. * @throws IOException on error. */ public static String toJsonString(Object value, Schema schema) throws IOException { final JsonNode node = ToJson.toJsonNode(value, schema); final StringWriter stringWriter = new StringWriter(); final JsonGenerator generator = JSON_FACTORY.createJsonGenerator(stringWriter); // We have disabled this because we used unions to represent row key formats // in the table layout. This is a HACK and needs a better solution. // TODO: Find better solution. https://jira.fiji.org/browse/SCHEMA-174 //generator.disable(Feature.QUOTE_FIELD_NAMES); generator.setPrettyPrinter(new DefaultPrettyPrinter()); final ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(generator, node); return stringWriter.toString(); }
return (null == value) ? JSON_NODE_FACTORY.nullNode() : toJsonNode(value, optionalType); for (Schema type : schema.getTypes()) { try { final JsonNode actualNode = toJsonNode(value, type); union.put(type.getFullName(), actualNode); return union;
/** {@inheritDoc} */ @Override public String toString() { try { return ToJson.toJsonString(mDesc); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
/** {@inheritDoc} */ @Override protected JsonNode toJsonNode() { final ObjectNode root = JsonNodeFactory.instance.objectNode(); root.put(FAMILY_NODE, mFamily); root.put(QUALIFIER_NODE, mQualifier); final ObjectNode value = root.with(VALUE_NODE); // Schema's documentation for toString says it is rendered as JSON. value.put(SCHEMA_NODE, mValue.getWriterSchema().toString()); try { value.put(DATA_NODE, ToJson.toAvroJsonString(mValue.getData(), mValue.getWriterSchema())); } catch (IOException ioe) { throw new FijiIOException(ioe); } return root; }
/** * Encodes an Avro record into JSON. * * @param record Avro record to encode. * @return Pretty JSON representation of the record. * @throws IOException on error. */ public static String toJsonString(IndexedRecord record) throws IOException { final Schema schema = record.getSchema(); return toJsonString(record, schema); }
try { root.put(ROW_KEY_FORMAT_NODE, ToJson.toAvroJsonString(mRowKeyFormat, mRowKeyFormat.getSchema())); final ArrayNode components = root.putArray(COMPONENTS_NODE); for (int i = 0; i < mComponents.length; i++) {
/** * Implements the --do=dump operation. * * @throws Exception on error. */ private void dumpLayout() throws Exception { final FijiTableLayout layout = mFiji.getMetaTable().getTableLayout(mTableURI.getTable()); final StringBuilder json = new StringBuilder(); json.append("/*\n"); json.append(" The raw JSON view of table layouts is intended for use by\n"); json.append(" system administrators or for debugging purposes.\n"); json.append("\n"); json.append(" Most users should use the 'fiji-schema-shell' DDL tool to modify\n"); json.append(" your table layouts instead.\n"); json.append("*/\n"); json.append(ToJson.toJsonString(layout.getDesc())); if (mWriteTo.isEmpty()) { System.out.println(json.toString()); } else { final String fileName = String.format("%s.json", mWriteTo); final FileOutputStream fos = new FileOutputStream(fileName); try { fos.write(Bytes.toBytes(json.toString())); } finally { ResourceUtils.closeOrLog(fos); } } }
/** * Dumps the history of layouts of a given table. * * @throws Exception on error. */ private void history() throws Exception { // Gather all of the layouts stored in the metaTable. final NavigableMap<Long, FijiTableLayout> timedLayouts = mFiji.getMetaTable().getTimedTableLayoutVersions(mTableURI.getTable(), mMaxVersions); if (timedLayouts.isEmpty()) { throw new RuntimeException("No such table: " + mTableURI.getTable()); } for (Map.Entry<Long, FijiTableLayout> entry: timedLayouts.entrySet()) { final long timestamp = entry.getKey(); final FijiTableLayout layout = entry.getValue(); final String json = ToJson.toJsonString(layout.getDesc()); if (mWriteTo.isEmpty()) { // SCHEMA-14: Add a newline to separate the dumped JSON versions of the layout. System.out.printf("timestamp: %d:%n%s%n", timestamp, json); } else { final String fileName = String.format("%s-%d.json", mWriteTo, timestamp); final FileOutputStream fos = new FileOutputStream(fileName); try { fos.write(Bytes.toBytes(json)); } finally { ResourceUtils.closeOrLog(fos); } } } }