package edu.uci.ics.asterix.dataflow.data.nontagged.printers.json;

import java.io.PrintStream;

import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
import edu.uci.ics.asterix.om.types.ATypeTag;
import edu.uci.ics.asterix.om.types.AUnionType;
import edu.uci.ics.asterix.om.types.BuiltinType;
import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
import edu.uci.ics.hyracks.algebricks.data.IPrinter;
import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;

public class ANullableFieldPrinterFactory implements IPrinterFactory {

    private static final long serialVersionUID = 1L;

    private AUnionType unionType;

    public ANullableFieldPrinterFactory(AUnionType unionType) {
        this.unionType = unionType;
    }

    @Override
    public IPrinter createPrinter() {
        return new IPrinter() {
            private IPrinter nullPrinter;
            private IPrinter fieldPrinter;

            @Override
            public void init() throws AlgebricksException {
                nullPrinter = (AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(BuiltinType.ANULL)).createPrinter();
                fieldPrinter = (AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(unionType.getUnionList().get(1)))
                        .createPrinter();
            }

            @Override
            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
                fieldPrinter.init();
                if (b[s] == ATypeTag.NULL.serialize())
                    nullPrinter.print(b, s, l, ps);
                else
                    fieldPrinter.print(b, s, l, ps);
            }

        };
    }

}

@@ -138,16 +138,20 @@
 gged.Aql
+JSON
 PrinterF
@@ -1046,32 +1046,36 @@
 llPrinter = (Aql
+JSON
 PrinterFactoryPr
@@ -1127,16 +1127,41 @@
 .ANULL))
+%0A                        
 .createP
@@ -1205,16 +1205,20 @@
 r = (Aql
+JSON
 PrinterF
@@ -1287,17 +1287,8 @@
 st()
-.get(1)))
 %0A   
@@ -1300,32 +1300,41 @@
                 
+.get(1)))
 .createPrinter()

Use the JSON printers in the JSON nullbable factory.