|
| 1 | +/* |
| 2 | + * Copyright 2013 Deutsche Nationalbibliothek |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.stream.converter; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.StringWriter; |
| 20 | + |
| 21 | +import org.culturegraph.mf.exceptions.MetafactureException; |
| 22 | +import org.culturegraph.mf.framework.DefaultStreamPipe; |
| 23 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 24 | +import org.culturegraph.mf.framework.StreamReceiver; |
| 25 | +import org.culturegraph.mf.framework.annotations.Description; |
| 26 | +import org.culturegraph.mf.framework.annotations.In; |
| 27 | +import org.culturegraph.mf.framework.annotations.Out; |
| 28 | + |
| 29 | +import com.fasterxml.jackson.core.JsonFactory; |
| 30 | +import com.fasterxml.jackson.core.JsonGenerationException; |
| 31 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 32 | +import com.fasterxml.jackson.core.JsonStreamContext; |
| 33 | + |
| 34 | +/** |
| 35 | + * Serialises an object as JSON. Records and entities are represented |
| 36 | + * as objects unless their name ends with []. If the name ends with [], |
| 37 | + * an array is created. |
| 38 | + * |
| 39 | + * @author Christoph Böhme |
| 40 | + * |
| 41 | + */ |
| 42 | +@Description("Serialises an object as JSON") |
| 43 | +@In(StreamReceiver.class) |
| 44 | +@Out(String.class) |
| 45 | +public final class JsonEncoder extends |
| 46 | + DefaultStreamPipe<ObjectReceiver<String>> { |
| 47 | + |
| 48 | + public static final String ARRAY_MARKER = "[]"; |
| 49 | + |
| 50 | + private final JsonGenerator jsonGenerator; |
| 51 | + private final StringWriter writer = new StringWriter(); |
| 52 | + |
| 53 | + public JsonEncoder() { |
| 54 | + try { |
| 55 | + jsonGenerator = new JsonFactory().createGenerator(writer); |
| 56 | + } catch (IOException e) { |
| 57 | + throw new MetafactureException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void startRecord(final String id) { |
| 63 | + final StringBuffer buffer = writer.getBuffer(); |
| 64 | + buffer.delete(0, buffer.length()); |
| 65 | + startGroup(id); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void endRecord() { |
| 70 | + endGroup(); |
| 71 | + try { |
| 72 | + jsonGenerator.flush(); |
| 73 | + } catch (IOException e) { |
| 74 | + throw new MetafactureException(e); |
| 75 | + } |
| 76 | + getReceiver().process(writer.toString()); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void startEntity(final String name) { |
| 81 | + startGroup(name); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void endEntity() { |
| 86 | + endGroup(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void literal(final String name, final String value) { |
| 91 | + try { |
| 92 | + if (jsonGenerator.getOutputContext().inObject()) { |
| 93 | + jsonGenerator.writeFieldName(name); |
| 94 | + } |
| 95 | + if (value == null) { |
| 96 | + jsonGenerator.writeNull(); |
| 97 | + } else { |
| 98 | + jsonGenerator.writeString(value); |
| 99 | + } |
| 100 | + } catch (JsonGenerationException e) { |
| 101 | + throw new MetafactureException(e); |
| 102 | + } |
| 103 | + catch (IOException e) { |
| 104 | + throw new MetafactureException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void startGroup(final String name) { |
| 109 | + try { |
| 110 | + final JsonStreamContext ctx = jsonGenerator.getOutputContext(); |
| 111 | + if (name.endsWith(ARRAY_MARKER)) { |
| 112 | + if (!ctx.inRoot()) { |
| 113 | + jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length())); |
| 114 | + } |
| 115 | + jsonGenerator.writeStartArray(); |
| 116 | + } else { |
| 117 | + if (!ctx.inRoot()) { |
| 118 | + jsonGenerator.writeFieldName(name); |
| 119 | + } |
| 120 | + jsonGenerator.writeStartObject(); |
| 121 | + } |
| 122 | + } catch (JsonGenerationException e) { |
| 123 | + throw new MetafactureException(e); |
| 124 | + } |
| 125 | + catch (IOException e) { |
| 126 | + throw new MetafactureException(e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void endGroup() { |
| 131 | + try { |
| 132 | + final JsonStreamContext ctx = jsonGenerator.getOutputContext(); |
| 133 | + if (ctx.inObject()) { |
| 134 | + jsonGenerator.writeEndObject(); |
| 135 | + } else if (ctx.inArray()) { |
| 136 | + jsonGenerator.writeEndArray(); |
| 137 | + } |
| 138 | + } catch (JsonGenerationException e) { |
| 139 | + throw new MetafactureException(e); |
| 140 | + } |
| 141 | + catch (IOException e) { |
| 142 | + throw new MetafactureException(e); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments