use of com.linkedin.avroutil1.compatibility.backports.Avro702Data in project avro-util by linkedin.
the class AvscWriter method toJson.
protected void toJson(Schema schema, AvroNames names, String contextNamespaceWhenParsed, String contextNamespaceWhenParsedUnder702, G gen) throws IOException {
Avro702Data avro702Data;
switch(schema.getType()) {
case ENUM:
// taken from EnumSchema.toJson() in avro 1.11
if (writeNameRef(schema, names, gen)) {
return;
}
gen.writeStartObject();
gen.writeStringField("type", "enum");
avro702Data = writeName(schema, names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
if (schema.getDoc() != null) {
gen.writeStringField("doc", schema.getDoc());
}
gen.writeArrayFieldStart("symbols");
for (String symbol : schema.getEnumSymbols()) {
gen.writeString(symbol);
}
gen.writeEndArray();
writeEnumDefault(schema, gen);
writeProps(schema, gen);
aliasesToJson(schema, avro702Data.getExtraAliases(), gen);
gen.writeEndObject();
break;
case FIXED:
// taken from FixedSchema.toJson() in avro 1.11
if (writeNameRef(schema, names, gen)) {
return;
}
gen.writeStartObject();
gen.writeStringField("type", "fixed");
avro702Data = writeName(schema, names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
if (schema.getDoc() != null) {
gen.writeStringField("doc", schema.getDoc());
}
gen.writeNumberField("size", schema.getFixedSize());
writeProps(schema, gen);
aliasesToJson(schema, avro702Data.getExtraAliases(), gen);
gen.writeEndObject();
break;
case RECORD:
// taken from RecordSchema.toJson() in avro 1.11
if (writeNameRef(schema, names, gen)) {
return;
}
gen.writeStartObject();
gen.writeStringField("type", schema.isError() ? "error" : "record");
avro702Data = writeName(schema, names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
AvroName name = AvroName.of(schema);
// save current namespaces - both 1.4 and correct one
// save avro-702 mode namespace
String savedBadSpace = names.badSpace();
// save correct namespace
String savedCorrectSpace = names.correctSpace();
// avro 1.4 only ever sets namespace if the current is null
if (savedBadSpace == null) {
names.badSpace(name.getSpace());
}
// always update correct namespace
names.correctSpace(name.getSpace());
if (schema.getDoc() != null) {
gen.writeStringField("doc", schema.getDoc());
}
if (schema.getFields() != null) {
gen.writeFieldName("fields");
fieldsToJson(schema, names, avro702Data.getNamespaceWhenParsing(), avro702Data.getNamespaceWhenParsing702(), gen);
}
writeProps(schema, gen);
aliasesToJson(schema, avro702Data.getExtraAliases(), gen);
gen.writeEndObject();
// avro 1.4 never restores namespace, so we never restore space
// always restore correct namespace
names.correctSpace(savedCorrectSpace);
break;
case ARRAY:
// taken from ArraySchema.toJson() in avro 1.11
gen.writeStartObject();
gen.writeStringField("type", "array");
gen.writeFieldName("items");
toJson(schema.getElementType(), names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
writeProps(schema, gen);
gen.writeEndObject();
break;
case MAP:
// taken from MapSchema.toJson() in avro 1.11
gen.writeStartObject();
gen.writeStringField("type", "map");
gen.writeFieldName("values");
toJson(schema.getValueType(), names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
writeProps(schema, gen);
gen.writeEndObject();
break;
case UNION:
// taken from UnionSchema.toJson() in avro 1.11
gen.writeStartArray();
for (Schema type : schema.getTypes()) {
toJson(type, names, contextNamespaceWhenParsed, contextNamespaceWhenParsedUnder702, gen);
}
gen.writeEndArray();
break;
default:
// all other schema types (taken from Schema.toJson() in avro 1.11)
if (!hasProps(schema)) {
// no props defined
// just write name
gen.writeString(schema.getName());
} else {
gen.writeStartObject();
gen.writeStringField("type", schema.getName());
writeProps(schema, gen);
gen.writeEndObject();
}
}
}
Aggregations