1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.rosetta;
\r
26 import java.io.IOException;
\r
27 import java.io.Writer;
\r
28 import java.util.Stack;
\r
30 import com.att.inno.env.util.IndentPrintWriter;
\r
32 public class OutJson extends Out {
\r
35 public<IN,S> void extract(IN in, Writer writer, Parse<IN, S> prs, boolean ... options) throws IOException, ParseException {
\r
36 Parsed<S> p = prs.newParsed();
\r
37 IndentPrintWriter ipw;
\r
38 if(options.length>0 && options[0]) { // is Pretty
\r
39 ipw = writer instanceof IndentPrintWriter?(IndentPrintWriter)writer:new IndentPrintWriter(writer);
\r
45 // If it's a fragment, print first Object Name. If root Object, skip first name
\r
46 Stack<LevelStack> jsonLevel = new Stack<LevelStack>();
\r
47 jsonLevel.push(new LevelStack(options.length>1 && options[1]));
\r
48 boolean print = true, hadData=false;
\r
49 char afterName=0, beforeName=0, maybe = 0, prev=0;
\r
52 while((p = prs.parse(in,p.reuse())).valid()) {
\r
58 if(count==2) { // it's empty, write open/close on it's own
\r
66 if(jsonLevel.peek().printObjectName) {
\r
68 } else { // don't print names on first
\r
71 maybe=jsonLevel.peek().listItem();
\r
72 jsonLevel.push(new LevelStack(true));
\r
75 if(p.hasData()) { // if we have data, we print that, so may need to prepend a comma.
\r
76 maybe = jsonLevel.peek().listItem();
\r
77 } else { // No data means just print,
\r
78 p.name = ""; // XML tags come through with names, but no data
\r
82 afterName = p.event;
\r
85 afterName = p.event;
\r
86 if((prev==',' && !hadData) || prev==']')maybe=',';
\r
87 else maybe = jsonLevel.peek().listItem();
\r
89 jsonLevel.push(new LevelStack(false));
\r
93 afterName = p.event;
\r
95 if(prev==',' && !hadData)maybe=',';
\r
96 else maybe = jsonLevel.peek().listItem();
\r
98 p.name = ""; // XML tags come through with names, but no data
\r
110 maybe=jsonLevel.peek().listItem();
\r
119 if(ipw==null)writer.append(maybe);
\r
120 else ipw.println(maybe);
\r
124 if(beforeName!=0) {
\r
125 if(ipw==null)writer.append(beforeName);
\r
126 else ipw.println(beforeName);
\r
131 writer.append('"');
\r
132 if(p.event==3)writer.append("__");
\r
133 writer.append(p.name);
\r
134 writer.append("\":");
\r
138 writer.append('"');
\r
139 escapedWrite(writer, p.sb);
\r
140 writer.append('"');
\r
141 } else if(p.sb.length()>0) {
\r
142 writer.append(p.sb);
\r
147 if(ipw==null)writer.append(afterName);
\r
149 switch(afterName) {
\r
151 ipw.println(afterName);
\r
157 ipw.print(afterName);
\r
160 if(prev=='}' || prev==',')ipw.println();
\r
162 ipw.print(afterName);
\r
166 ipw.println(afterName);
\r
169 ipw.print(afterName);
\r
184 hadData = p.hasData();
\r
190 private void escapedWrite(Writer writer, StringBuilder sb) throws IOException {
\r
192 for(int i=0;i<sb.length();++i) {
\r
193 switch(c=sb.charAt(i)) {
\r
196 if(i<sb.length()) {
\r
202 writer.append('\\');
\r
203 // Passthrough on purpose
\r
213 public String logName() {
\r
214 return "Rosetta JSON";
\r
217 private static class LevelStack {
\r
218 public boolean printObjectName=false;
\r
219 private boolean first_n_List=true;
\r
221 public LevelStack(boolean printObjectName) {
\r
222 this.printObjectName = printObjectName;
\r
225 public char listItem() {
\r
227 first_n_List=false;
\r