Created
July 22, 2010 00:55
-
-
Save spullara/485425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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