|
| 1 | +/* |
| 2 | + * Copyright 2021-2024 Aklivity Inc |
| 3 | + * |
| 4 | + * Licensed under the Aklivity Community License (the "License"); you may not use |
| 5 | + * this file except in compliance with the License. You may obtain a copy of the |
| 6 | + * License at |
| 7 | + * |
| 8 | + * https://www.aklivity.io/aklivity-community-license/ |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OF ANY KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package org.apache.avro.io; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.OutputStream; |
| 19 | + |
| 20 | +import org.apache.avro.Schema; |
| 21 | +import org.apache.avro.io.parsing.Parser; |
| 22 | +import org.apache.avro.io.parsing.Symbol; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 25 | + |
| 26 | +/** |
| 27 | + * A derived encoder that does the skipping of fields that match the index. It also encodes unions of null and a single |
| 28 | + * type as a more normal key=value rather than key={type=value}. |
| 29 | + * |
| 30 | + * @author zfarkas |
| 31 | + */ |
| 32 | +public final class CanonicalJsonEncoder extends JsonEncoder |
| 33 | +{ |
| 34 | + |
| 35 | + |
| 36 | + public CanonicalJsonEncoder( |
| 37 | + final Schema sc, |
| 38 | + final OutputStream out) throws IOException |
| 39 | + { |
| 40 | + super(sc, out); |
| 41 | + } |
| 42 | + |
| 43 | + public CanonicalJsonEncoder( |
| 44 | + final Schema sc, |
| 45 | + final OutputStream out, |
| 46 | + final boolean pretty) throws IOException |
| 47 | + { |
| 48 | + super(sc, out, pretty); |
| 49 | + } |
| 50 | + |
| 51 | + public CanonicalJsonEncoder( |
| 52 | + final Schema sc, |
| 53 | + final JsonGenerator out) throws IOException |
| 54 | + { |
| 55 | + super(sc, out); |
| 56 | + } |
| 57 | + |
| 58 | + public Parser getParser() |
| 59 | + { |
| 60 | + return parser; |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean isNullableSingle( |
| 64 | + final Symbol.Alternative top) |
| 65 | + { |
| 66 | + return top.size() == 2 && ("null".equals(top.getLabel(0)) || "null".equals(top.getLabel(1))); |
| 67 | + } |
| 68 | + |
| 69 | + public static String getNullableSingle( |
| 70 | + final Symbol.Alternative top) |
| 71 | + { |
| 72 | + final String label = top.getLabel(0); |
| 73 | + return "null".equals(label) ? top.getLabel(1) : label; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Overwrite this function to optime json decoding of union {null, type}. |
| 78 | + * |
| 79 | + * @param unionIndex |
| 80 | + * @throws IOException |
| 81 | + */ |
| 82 | + |
| 83 | + @Override |
| 84 | + public void writeIndex( |
| 85 | + final int unionIndex) throws IOException |
| 86 | + { |
| 87 | + parser.advance(Symbol.UNION); |
| 88 | + Symbol.Alternative top = (Symbol.Alternative) parser.popSymbol(); |
| 89 | + Symbol symbol = top.getSymbol(unionIndex); |
| 90 | + if (symbol != Symbol.NULL && !isNullableSingle(top)) |
| 91 | + { |
| 92 | + out.writeStartObject(); |
| 93 | + out.writeFieldName(top.getLabel(unionIndex)); |
| 94 | + parser.pushSymbol(Symbol.UNION_END); |
| 95 | + } |
| 96 | + parser.pushSymbol(symbol); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments