View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.io.jsontransform.sink;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.math.BigDecimal;
25  
26  import org.osgi.annotation.versioning.ProviderType;
27  
28  import com.fasterxml.jackson.core.JsonFactory;
29  import com.fasterxml.jackson.core.JsonGenerator;
30  
31  import io.wcm.caravan.io.jsontransform.element.JsonElement;
32  
33  /**
34   * Writes the JSON stream elements into an output stream.
35   */
36  @ProviderType
37  public final class JacksonStreamSink implements Sink {
38  
39    private static final JsonFactory JSON_FACTORY = new JsonFactory();
40    private final JsonGenerator generator;
41  
42    /**
43     * @param output The output stream to write into
44     * @throws IOException Error on writing
45     */
46    public JacksonStreamSink(final OutputStream output) throws IOException {
47      generator = JSON_FACTORY.createGenerator(output);
48    }
49  
50    @Override
51    public void close() throws IOException {
52      generator.close();
53    }
54  
55    @Override
56    public void write(JsonElement jsonElement) throws IOException {
57      switch (jsonElement.getType()) {
58        case START_OBJECT:
59          if (jsonElement.getKey() != null) {
60            generator.writeObjectFieldStart(convertFieldName(jsonElement.getKey()));
61          }
62          else {
63            generator.writeStartObject();
64          }
65          break;
66        case END_OBJECT:
67          generator.writeEndObject();
68          break;
69        case START_ARRAY:
70          if (jsonElement.getKey() != null) {
71            generator.writeArrayFieldStart(convertFieldName(jsonElement.getKey()));
72          }
73          else {
74            generator.writeStartArray();
75          }
76          break;
77        case END_ARRAY:
78          generator.writeEndArray();
79          break;
80        default:
81          if (jsonElement.getKey() != null) {
82            generator.writeFieldName(convertFieldName(jsonElement.getKey()));
83          }
84          writeValue(jsonElement.getValue());
85      }
86      generator.flush();
87    }
88  
89    private void writeValue(final Object value) throws IOException {
90      if (value == null) {
91        generator.writeNull();
92      }
93      else if ("false".equals(value)) {
94        generator.writeBoolean(false);
95      }
96      else if ("true".equals(value)) {
97        generator.writeBoolean(true);
98      }
99      else if (value instanceof BigDecimal) {
100       generator.writeNumber((BigDecimal)value);
101     }
102     else {
103       generator.writeString((String)value);
104     }
105   }
106 
107   private String convertFieldName(final String name) {
108     return name;
109   }
110 
111   @Override
112   public boolean hasOutput() {
113     return generator.getOutputContext().getEntryCount() > 0;
114   }
115 
116 }