Skip to content

Instantly share code, notes, and snippets.

@spullara
Created July 22, 2010 00:55
Show Gist options
  • Select an option

  • Save spullara/485425 to your computer and use it in GitHub Desktop.

Select an option

Save spullara/485425 to your computer and use it in GitHub Desktop.
public class AvroJsonEncoder extends JsonEncoder {
private Parser parser;
private JsonGenerator out;
private static Field parserField;
static {
try {
parserField = JsonEncoder.class.getDeclaredField("parser");
parserField.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public AvroJsonEncoder(Schema sc, JsonGenerator out) throws IOException {
super(sc, out);
this.out = out;
try {
parser = (Parser) parserField.get(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void writeIndex(int unionIndex) throws IOException {
parser.advance(Symbol.UNION);
Symbol.Alternative top = (Symbol.Alternative) parser.popSymbol();
Symbol symbol = top.getSymbol(unionIndex);
if (symbol != Symbol.NULL &&
(top.labels.length != 2 || (!top.labels[0].equals("null") && !top.labels[1].equals("null")))) {
out.writeStartObject();
out.writeFieldName(top.getLabel(unionIndex));
parser.pushSymbol(Symbol.UNION_END);
}
parser.pushSymbol(symbol);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment