Java Code Examples for javax.json.stream.JsonGeneratorFactory#createGenerator()

The following examples show how to use javax.json.stream.JsonGeneratorFactory#createGenerator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSecurityMessage.java    From aceql-http with GNU Lesser General Public License v2.1 7 votes vote down vote up
/**
    * Builds a security error message in JSON format for Statements
    * 
    * @param sqlOrder
    * @param errorMessage
    * @param doPrettyPrinting
    * @return
    */
   public static String statementNotAllowedBuild(String sqlOrder,
    String errorMessage, boolean doPrettyPrinting) {

try {
    JsonGeneratorFactory jf = JsonUtil
	    .getJsonGeneratorFactory(doPrettyPrinting);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    JsonGenerator gen = jf.createGenerator(out);
    gen.writeStartObject();
    gen.write(Tag.PRODUCT_SECURITY, errorMessage);
    gen.write("SQL order", sqlOrder);
    gen.writeEnd();
    gen.close();
    return out.toString("UTF-8");
} catch (Exception e) {
    String returnString = Tag.PRODUCT_SECURITY + " " + errorMessage
	    + " " + sqlOrder;
    return returnString;
}

   }
 
Example 2
Source File: JsonOkReturn.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * Returns a name and a value after the OK
    * 
    * @return just OK
    */
   public static String build(String name, String value) {

if (name == null) {
    throw new NullPointerException("name is null");
}
if (value == null) {
    throw new NullPointerException("value is null");
}

JsonGeneratorFactory jf = JsonUtil
	.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject().write("status", "OK").write(name, value)
	.writeEnd();
gen.close();

return sw.toString();
   }
 
Example 3
Source File: JsonOkReturn.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * Build a Json with name and values from the passed map
    * 
    * @param namesAndValues
    *            the map of (name, value) to add to the JsonGenerator
    * @return
    */
   public static String build(Map<String, String> namesAndValues) {

if (namesAndValues == null) {
    throw new NullPointerException("namesAndValues is null");
}

JsonGeneratorFactory jf = JsonUtil
	.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject().write("status", "OK");

for (Map.Entry<String, String> entry : namesAndValues.entrySet()) {

    //System.out.println(entry.getKey() + "/" + entry.getValue());
    gen.write(entry.getKey(), entry.getValue());
}

gen.writeEnd();
gen.close();

return sw.toString();
   }
 
Example 4
Source File: JsonExample.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
StringWriter writer = new StringWriter();

JsonGeneratorFactory jf = JsonUtil.getJsonGeneratorFactory(true);
JsonGenerator gen = jf.createGenerator(writer);

gen.writeStartObject().write("firstName", "Duke")
	.write("lastName", "Java").write("age", 18)
	.write("streetAddress", "100 Internet Dr")
	.write("city", "JavaTown").write("state", "JA")
	.write("postalCode", "12345")

	.writeStartArray("phoneNumbers").writeStartObject()
	.write("type", "mobile").write("number", "111-111-1111")
	.writeEnd().writeStartObject().write("type", "home")
	.write("number", "222-222-2222").writeEnd().writeEnd()

	.writeEnd();
gen.close();

System.out.println(writer.toString());

   }
 
Example 5
Source File: JSONConverter.java    From jcypher with Apache License 2.0 6 votes vote down vote up
/**
 * Answer a JSON representation of a recorded query
 * @param query
 * @return
 */
public String toJSON(RecordedQuery query) {
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (this.prettyFormat != Format.NONE) {
		JsonGeneratorFactory gf = JSONWriter.getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	generator.writeStartObject();
	writeQuery(query, generator);
	generator.writeEnd();

	generator.flush();
	return sw.toString();
}
 
Example 6
Source File: JSONDBFacade.java    From jcypher with Apache License 2.0 6 votes vote down vote up
/**
 * Answer a JSON representation of the available domains in the database
 * @return
 */
public String getDomains() {
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (this.prettyFormat != Format.NONE) {
		JsonGeneratorFactory gf = JSONWriter.getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	generator.writeStartArray();
	List<String> doms = DomainInformation.availableDomains(dbAccess);
	for (String dom : doms) {
		generator.write(dom);
	}
	generator.writeEnd();

	generator.flush();
	return sw.toString();
}
 
Example 7
Source File: JSONDomainFacade.java    From jcypher with Apache License 2.0 6 votes vote down vote up
/**
 * Answer a JSON representation of the domain model
 * @return
 */
public String getDomainModel() {
	DomainModel model = ((IIntDomainAccess)this.domainAccess).getInternalDomainAccess().getDomainModel();
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (this.prettyFormat != Format.NONE) {
		JsonGeneratorFactory gf = JSONWriter.getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	generator.writeStartObject();
	generator.write("domainName", model.getDomainName());
	writeModel(model, generator);
	generator.writeEnd();

	generator.flush();
	return sw.toString();
}
 
Example 8
Source File: JSONDomainFacade.java    From jcypher with Apache License 2.0 6 votes vote down vote up
/**
 * Answer a JSON object containing the domain name
 * @return
 */
public String getDomainName() {
	DomainModel model = ((IIntDomainAccess)this.domainAccess).getInternalDomainAccess().getDomainModel();
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (this.prettyFormat != Format.NONE) {
		JsonGeneratorFactory gf = JSONWriter.getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	generator.writeStartObject();
	generator.write("domainName", model.getDomainName());
	generator.writeEnd();

	generator.flush();
	return sw.toString();
}
 
Example 9
Source File: JsonOkReturn.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Returns just OK
    * 
    * @return just OK
    */
   public static String build() {

JsonGeneratorFactory jf = JsonUtil
	.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject().write("status", "OK").writeEnd();
gen.close();

return sw.toString();
   }
 
Example 10
Source File: JsonErrorReturn.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Builds the error message
    *
    * @return the error message
    */
   public String build() {

JsonGeneratorFactory jf = JsonUtil
	.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject().write("status", "FAIL").write("error_type",
	errorType);

if (errorMessage != null) {
    gen.write("error_message", errorMessage);
} else {
    gen.write("error_message", JsonValue.NULL);
}

if (stackTrace != null) {
    gen.write("stack_trace", stackTrace);
    System.err.println(stackTrace);
}

gen.write("http_status", httpStatus);

gen.writeEnd();
gen.close();

String doc = sw.toString();

if (LOG_JSON_ERROR) {
    System.err.println(doc);
}

return doc;
   }
 
Example 11
Source File: JsonFormatter.java    From jboss-logmanager-ext with Apache License 2.0 5 votes vote down vote up
@Override
protected Generator createGenerator(final Writer writer) {
    final JsonGeneratorFactory factory;
    synchronized (config) {
        factory = this.factory;
    }
    return new FormatterJsonGenerator(factory.createGenerator(writer));
}
 
Example 12
Source File: JSONWriter.java    From jcypher with Apache License 2.0 5 votes vote down vote up
public static String toJSON(PreparedQuery preparedQuery) {
	if (preparedQuery.hasdSLParams()) { // parameters part of json must be recreated
		WriterContext context = new WriterContext();
		PQContext pqContext = preparedQuery.getContext();
		QueryParam.setExtractParams(pqContext.extractParams, context);
		context.cypherFormat = pqContext.cypherFormat;
		String cypher = preparedQuery.getCypher();
		
		StringWriter sw = new StringWriter();
		JsonGenerator generator;
		if (context.cypherFormat != Format.NONE) {
			JsonGeneratorFactory gf = getPrettyGeneratorFactory();
			generator = gf.createGenerator(sw);
		} else
			generator = Json.createGenerator(sw);
		
		ContextAccess.setUseTransactionalEndpoint(pqContext.useTransationalEndpoint, context);
		ContextAccess.setResultDataContents(context, pqContext.resultDataContents);
		context.queryParams = pqContext.queryParams;
		
		generator.writeStartObject();
		if (pqContext.useTransationalEndpoint)
			writeStatements(new Statement[] {new Statement(context, cypher)}, generator);
		else
			writeQuery("query", cypher, context, generator);
		generator.writeEnd();

		generator.flush();
		context.buffer.append(sw.getBuffer());
		return context.buffer.toString();
	} else
		return preparedQuery.getJson();
}
 
Example 13
Source File: JSONWriter.java    From jcypher with Apache License 2.0 4 votes vote down vote up
public static void toJSON(List<JcQuery> queries, WriterContext context) {
	List<Statement> statements = new ArrayList<Statement>(queries.size());
	PreparedQueries prepQs = new PreparedQueries();
	context.preparedQuery = prepQs;
	Format cf = context.cypherFormat;
	context.cypherFormat = Format.NONE;
	boolean useTxEndpoint = ContextAccess.useTransationalEndpoint(context);
	// needed for multiple statements
	ContextAccess.setUseTransactionalEndpoint(true, context);
	boolean extract = QueryParam.isExtractParams(context);
	for (JcQuery query : queries) {
		PreparedQuery prepQ = new PreparedQuery();
		prepQs.add(prepQ);
		PQContext pqContext = prepQ.getContext();
		pqContext.cypherFormat = cf;
		pqContext.extractParams = query.isExtractParams();
		pqContext.useTransationalEndpoint = ContextAccess.useTransationalEndpoint(context);
		pqContext.resultDataContents = ContextAccess.getResultDataContents(context);
		
		WriterContext ctxt = ContextAccess.cloneContext(context);
		ctxt.preparedQuery = prepQs; // needed to mark if there are dynamic parameters
		QueryParam.setExtractParams(query.isExtractParams(), ctxt);
		CypherWriter.toCypherExpression(query, ctxt);
		String cypher = ctxt.buffer.toString();
		prepQ.setCypher(cypher);
		pqContext.queryParams = QueryParamSet.getQueryParams(ctxt);
		reInitContext(ctxt);
		statements.add(new Statement(ctxt, cypher));
	}
	context.cypherFormat = cf;
	
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (context.cypherFormat != Format.NONE) {
		JsonGeneratorFactory gf = getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	generator.writeStartObject();
	Statement[] statementsArray = statements.toArray(new Statement[statements.size()]);
	writeStatements(statementsArray, generator);
	generator.writeEnd();

	generator.flush();
	context.buffer.append(sw.getBuffer());
	
	// reset to original
	QueryParam.setExtractParams(extract, context);
	ContextAccess.setUseTransactionalEndpoint(useTxEndpoint, context);
	prepQs.setJson(context.buffer.toString());
}
 
Example 14
Source File: JSONWriter.java    From jcypher with Apache License 2.0 4 votes vote down vote up
public static void toJSON(JcQuery query, WriterContext context) {
	PreparedQuery prepQ = new PreparedQuery();
	context.preparedQuery = prepQ;
	PQContext pqContext = prepQ.getContext();
	pqContext.cypherFormat = context.cypherFormat;
	context.cypherFormat = Format.NONE;
	boolean extract = QueryParam.isExtractParams(context);
	pqContext.extractParams = query.isExtractParams();
	QueryParam.setExtractParams(query.isExtractParams(), context);
	CypherWriter.toCypherExpression(query, context);
	context.cypherFormat = pqContext.cypherFormat;
	String cypher = context.buffer.toString();
	reInitContext(context);
	
	prepQ.setCypher(cypher);
	
	StringWriter sw = new StringWriter();
	JsonGenerator generator;
	if (context.cypherFormat != Format.NONE) {
		JsonGeneratorFactory gf = getPrettyGeneratorFactory();
		generator = gf.createGenerator(sw);
	} else
		generator = Json.createGenerator(sw);
	
	pqContext.useTransationalEndpoint = ContextAccess.useTransationalEndpoint(context);
	pqContext.resultDataContents = ContextAccess.getResultDataContents(context);
	pqContext.queryParams = QueryParamSet.getQueryParams(context);
	
	generator.writeStartObject();
	if (ContextAccess.useTransationalEndpoint(context))
		writeStatements(new Statement[] {new Statement(context, cypher)}, generator);
	else
		writeQuery("query", cypher, context, generator);
	generator.writeEnd();

	generator.flush();
	context.buffer.append(sw.getBuffer());
	
	// reset to original
	QueryParam.setExtractParams(extract, context);
	prepQ.setJson(context.buffer.toString());
}
 
Example 15
Source File: JSONWriter.java    From jcypher with Apache License 2.0 4 votes vote down vote up
public static String toJSON(PreparedQueries preparedQueries) {
	if (preparedQueries.hasdSLParams()) { // parameters part of json must be recreated
		WriterContext context = new WriterContext();
		List<PreparedQuery> prepQs = preparedQueries.getPreparedQueries();
		if (prepQs.isEmpty())
			return new String();
		PreparedQuery prepQ = prepQs.get(0);
		PQContext pqContext = prepQ.getContext();
		List<Statement> statements = new ArrayList<Statement>(prepQs.size());
		Format cf = pqContext.cypherFormat;
		pqContext.fillContext(context);
		context.cypherFormat = Format.NONE;
		// needed for multiple statements
		ContextAccess.setUseTransactionalEndpoint(true, context);
		for (PreparedQuery pq : prepQs) {
			WriterContext ctxt = new WriterContext();
			PQContext pqCtxt = pq.getContext();
			pqCtxt.fillContext(ctxt);
			
			String cypher = pq.getCypher();
			statements.add(new Statement(ctxt, cypher));
		}
		context.cypherFormat = cf;
		
		StringWriter sw = new StringWriter();
		JsonGenerator generator;
		if (context.cypherFormat != Format.NONE) {
			JsonGeneratorFactory gf = getPrettyGeneratorFactory();
			generator = gf.createGenerator(sw);
		} else
			generator = Json.createGenerator(sw);
		
		generator.writeStartObject();
		Statement[] statementsArray = statements.toArray(new Statement[statements.size()]);
		writeStatements(statementsArray, generator);
		generator.writeEnd();

		generator.flush();
		context.buffer.append(sw.getBuffer());
		preparedQueries.setJson(context.buffer.toString());
		return preparedQueries.getJson();
	} else
		return preparedQueries.getJson();
}
 
Example 16
Source File: AuspiceGenerator.java    From beast-mcmc with GNU Lesser General Public License v2.1 3 votes vote down vote up
private void writeTreeJSON(String filename, RootedTree tree) throws IOException {
    FileWriter writer = new FileWriter(filename);

    HashMap<String, Object> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);

    JsonGeneratorFactory factory = Json.createGeneratorFactory(config);

    JsonGenerator generator = factory.createGenerator(writer);
    currentYValue = 0;

    writeNode(generator, tree, tree.getRootNode());

    generator.close();
}