All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.util.OAJsonWriter Maven / Gradle / Ivy

There is a newer version: 3.7.10
Show newest version
/*  Copyright 1999 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.util;

import java.util.*;
import java.io.*;
import com.viaoa.hub.*;
import com.viaoa.object.*;

/**
 
see:  20200127 OAJaxb.java

    OAJsonWriter creates an JSON file that can then be read using an OAJsonReader.
If an object has already been stored in the file, then its key will be stored. @see OAJsonReader */ public class OAJsonWriter { protected PrintWriter pw; protected StringWriter sw; public int indent; private String pad=""; private int indentLast; public int indentAmount=2; private boolean bInit; private String encodeMessage; private boolean bCompress=true; private ArrayList alInclude; private ArrayList alExclude; protected Hashtable hash; // used for storing objects without an Object Key - updated by OAObject private Stack stack; private boolean includeOneRef; private boolean includeManyRef; public OAJsonWriter() { } public void setCompress(boolean b) { this.bCompress = b; } public boolean getCompress() { return this.bCompress; } /** saves OAObject as JSON */ public String write(OAObject obj) { reset(); OACascade cascade = new OACascade(); OAObjectJsonDelegate.write(obj, this, false, cascade); String s = new String(sw.getBuffer()); return s; } /** saves Hub as JSON */ public String write(Hub hub) { reset(); OACascade cascade = new OACascade(); HubJsonDelegate.write(hub, this, cascade); String s = new String(sw.getBuffer()); return s; } protected void reset(){ this.sw = new StringWriter(); this.pw = new PrintWriter(sw); stack = new Stack(); } //qqq NEW CODE 20120610 qqqqqqqqqqqqq THis needs to be tested **********qqqqqqqqq //qqqqqqqqqqqq todo: build a tree ? /** called by OAObject.write to know if object should be written for a property that references another OAObject. This can be overwritten to control which properties are saved. default is true; */ public boolean shouldIncludeProperty(Object obj, String propertyName, Object value, OAPropertyInfo pi, OALinkInfo li) { if (li != null) { if (li.getType() == li.ONE) { if (!includeOneRef) return false; } else { if (!includeManyRef) return false; } } if (propertyName == null) return false; String current = getCurrentPath(); if (current == null) current = ""; else if (current.length() > 0) current += "."; current += propertyName; if (alInclude != null) { boolean bMaybe = false; for (String s : alInclude) { if (s.equalsIgnoreCase(current)) return true; if (s.toUpperCase().startsWith(current.toUpperCase())) { if (s.charAt(s.length()) == '.') { return true; } } if (current.toUpperCase().startsWith(s.toUpperCase())) { if (current.charAt(s.length()) == '.') { bMaybe = true; } } } if (!bMaybe) return false; } if (alExclude != null) { for (String s : alExclude) { if (s.equalsIgnoreCase(current)) return false; if (s.toUpperCase().startsWith(current.toUpperCase())) { if (s.charAt(current.length()) == '.') return false; } } } return true; } /* qqqqqq example: save Employee object dept dept.manager dept.manager.employees firstName lastName calcProperty */ /** * Current propertyPath from the root OAObject. * Only includes reference/link names, no regular properties */ protected String getCurrentPath() { int x = stack.size(); String s = ""; for (int i=0; i 0) s = "." + s; s = stack.elementAt(i) + s; } return s; } // called by OAObjectJsonDelegate to track the serialized path of references public void pushReference(String name) { if (!OAString.isEmpty(name)) { stack.push(name); } } public void popReference() { if (stack.size() > 0) { stack.pop(); } } public void includeOneReferences(boolean b) { this.includeOneRef = b; } public void includeManyReferences(boolean b) { this.includeManyRef = b; } public void includePropertyPath(String propertyPath) { if (alInclude == null) { alInclude = new ArrayList(); } if (!OAString.isEmpty(propertyPath)) alInclude.add(propertyPath); } public void excludePropertyPath(String propertyPath) { if (alExclude == null) { alExclude = new ArrayList(); } if (!OAString.isEmpty(propertyPath)) alExclude.add(propertyPath); } /** hook used to know when an object is being saved. */ public void writing(Object obj) { } /** encloses line in XML CDATA tag and internally encodes illegal XML characters */ public void printCDATA(String line) { if (pw == null) throw new NullPointerException("PrintWrite is null"); //qqqqq Json does not have CDATA //qqqqqqq check to see what conversions JSON needs, qqqqqqqqqqqq if (encodeMessage != null) line = encodeMessage+Base64.encode(line); pw.print(""); } /** converts XML codes and encodes illegal XML characters */ public void printJson(String line) { if (pw == null) throw new NullPointerException("PrintWrite is null"); //qqqqqqq check to see what conversions JSON needs, qqqqqqqqqqqq pw.print(OAString.convertToXML(line, false)); } public void print(String line) { if (pw == null) throw new NullPointerException("PrintWriter is null"); pw.print(line); } public void println(String line) { if (pw == null) throw new NullPointerException("PrintWriter is null"); if (bCompress) pw.print(line); else pw.println(line); } public void indent() { if (bCompress) return; if (pw == null) throw new NullPointerException("PrintWriter is null"); if (indent != indentLast) { indentLast = indent; pad = ""; int x = indent * indentAmount; for (int i=0; i < x; i++) pad += " "; } pw.print(pad); } /** Method that can be overwritten to provide custom conversion to String. Called to convert a value to a String when there does not exist an OAConverter for a class. @return null to skip property. */ public String convertToString(String property, Object value) { return null; } }