Java Code Examples for org.codehaus.jackson.JsonGenerator#writeArrayFieldStart()
The following examples show how to use
org.codehaus.jackson.JsonGenerator#writeArrayFieldStart() .
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: Helper.java From NNAnalytics with Apache License 2.0 | 6 votes |
/** * Write a set of enums out to HTTP Response as a JSON list. * * @param resp the http response * @param values the enums * @throws IOException if parsing or writing fails */ public static void toJsonList(HttpServletResponse resp, Enum[]... values) throws IOException { JsonGenerator json = new JsonFactory().createJsonGenerator(resp.getWriter()).useDefaultPrettyPrinter(); try { json.writeStartObject(); for (int i = 0; i < values.length; i++) { Enum[] enumList = values[i]; json.writeArrayFieldStart("Possibilities " + (i + 1)); for (Enum value : enumList) { if (value != null) { json.writeStartObject(); json.writeStringField("Name", value.name()); json.writeEndObject(); } } json.writeEndArray(); } json.writeEndObject(); } finally { IOUtils.closeStream(json); } }
Example 2
Source File: Helper.java From NNAnalytics with Apache License 2.0 | 6 votes |
/** * Return String representation of enums as a JSON list. * * @param values the enums * @return String representation of enums as a JSON list * @throws IOException if parsing or writing fails */ public static String toJsonList(Enum[]... values) throws IOException { StringWriter sw = new StringWriter(); JsonGenerator json = new JsonFactory().createJsonGenerator(sw).useDefaultPrettyPrinter(); try { json.writeStartObject(); for (int i = 0; i < values.length; i++) { Enum[] enumList = values[i]; json.writeArrayFieldStart("Possibilities " + (i + 1)); for (Enum value : enumList) { if (value != null) { json.writeStartObject(); json.writeStringField("Name", value.name()); json.writeEndObject(); } } json.writeEndArray(); } json.writeEndObject(); } finally { IOUtils.closeStream(json); } return sw.toString(); }
Example 3
Source File: AttributesSearchQueryImpl.java From bintray-client-java with Apache License 2.0 | 6 votes |
@Override public void serialize(AttributesSearchQueryImpl value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartArray(); jgen.writeStartObject(); jgen.writeArrayFieldStart(value.attributeName); @SuppressWarnings("unchecked") List<AttributesSearchQueryClauseImpl> clauses = value.getQueryClauses(); for (AttributesSearchQueryClauseImpl clause : clauses) { if (clause.getType().equals(Attribute.Type.Boolean)) { jgen.writeBoolean((Boolean) clause.getClauseValue()); } else if (clause.getType().equals(Attribute.Type.number)) { jgen.writeNumber(String.valueOf(clause.getClauseValue())); } else { //String or Date jgen.writeString((String) clause.getClauseValue()); } } jgen.writeEndArray(); jgen.writeEndObject(); jgen.writeEndArray(); }
Example 4
Source File: RepositoryImpl.java From bintray-client-java with Apache License 2.0 | 6 votes |
/** * PATCH repo only accepts description and label updates, name is needed for URL creation, because of the special * ignore and property structure of the RepositoryDetails class this method just uses a json generator to write * the update json. */ public static String getUpdateJson(RepositoryDetails repositoryDetails) throws IOException { StringWriter writer = new StringWriter(); JsonGenerator jGen = ObjectMapperHelper.get().getJsonFactory().createJsonGenerator(writer); jGen.writeStartObject(); jGen.writeStringField("name", repositoryDetails.getName()); jGen.writeStringField("desc", repositoryDetails.getDescription()); if (repositoryDetails.getLabels().size() > 0) { jGen.writeArrayFieldStart("labels"); for (String label : repositoryDetails.getLabels()) { jGen.writeString(label); } jGen.writeEndArray(); } jGen.writeEndObject(); jGen.close(); writer.close(); return writer.toString(); }
Example 5
Source File: Log4Json.java From hadoop with Apache License 2.0 | 5 votes |
/** * Build a JSON entry from the parameters. This is public for testing. * * @param writer destination * @param loggerName logger name * @param timeStamp time_t value * @param level level string * @param threadName name of the thread * @param message rendered message * @param ti nullable thrown information * @return the writer * @throws IOException on any problem */ public Writer toJson(final Writer writer, final String loggerName, final long timeStamp, final String level, final String threadName, final String message, final ThrowableInformation ti) throws IOException { JsonGenerator json = factory.createJsonGenerator(writer); json.writeStartObject(); json.writeStringField(NAME, loggerName); json.writeNumberField(TIME, timeStamp); Date date = new Date(timeStamp); json.writeStringField(DATE, dateFormat.format(date)); json.writeStringField(LEVEL, level); json.writeStringField(THREAD, threadName); json.writeStringField(MESSAGE, message); if (ti != null) { //there is some throwable info, but if the log event has been sent over the wire, //there may not be a throwable inside it, just a summary. Throwable thrown = ti.getThrowable(); String eclass = (thrown != null) ? thrown.getClass().getName() : ""; json.writeStringField(EXCEPTION_CLASS, eclass); String[] stackTrace = ti.getThrowableStrRep(); json.writeArrayFieldStart(STACK); for (String row : stackTrace) { json.writeString(row); } json.writeEndArray(); } json.writeEndObject(); json.flush(); json.close(); return writer; }
Example 6
Source File: Log4Json.java From big-c with Apache License 2.0 | 5 votes |
/** * Build a JSON entry from the parameters. This is public for testing. * * @param writer destination * @param loggerName logger name * @param timeStamp time_t value * @param level level string * @param threadName name of the thread * @param message rendered message * @param ti nullable thrown information * @return the writer * @throws IOException on any problem */ public Writer toJson(final Writer writer, final String loggerName, final long timeStamp, final String level, final String threadName, final String message, final ThrowableInformation ti) throws IOException { JsonGenerator json = factory.createJsonGenerator(writer); json.writeStartObject(); json.writeStringField(NAME, loggerName); json.writeNumberField(TIME, timeStamp); Date date = new Date(timeStamp); json.writeStringField(DATE, dateFormat.format(date)); json.writeStringField(LEVEL, level); json.writeStringField(THREAD, threadName); json.writeStringField(MESSAGE, message); if (ti != null) { //there is some throwable info, but if the log event has been sent over the wire, //there may not be a throwable inside it, just a summary. Throwable thrown = ti.getThrowable(); String eclass = (thrown != null) ? thrown.getClass().getName() : ""; json.writeStringField(EXCEPTION_CLASS, eclass); String[] stackTrace = ti.getThrowableStrRep(); json.writeArrayFieldStart(STACK); for (String row : stackTrace) { json.writeString(row); } json.writeEndArray(); } json.writeEndObject(); json.flush(); json.close(); return writer; }
Example 7
Source File: ServiceResponseBuilder.java From WSPerfLab with Apache License 2.0 | 5 votes |
public static ByteArrayOutputStream buildTestBResponse(JsonFactory jsonFactory, BackendResponse response) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos); jsonGenerator.writeStartObject(); // delay values of each response jsonGenerator.writeArrayFieldStart("delay"); writeTuple(jsonGenerator, "a", response.getDelay()); jsonGenerator.writeEndArray(); // itemSize values of each response jsonGenerator.writeArrayFieldStart("itemSize"); writeTuple(jsonGenerator, "a", response.getItemSize()); jsonGenerator.writeEndArray(); // numItems values of each response jsonGenerator.writeArrayFieldStart("numItems"); writeTuple(jsonGenerator, "a", response.getNumItems()); jsonGenerator.writeEndArray(); // all items from responses jsonGenerator.writeArrayFieldStart("items"); addItemsFromResponse(jsonGenerator, response); jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); jsonGenerator.close(); return bos; }
Example 8
Source File: NameNodeLoader.java From NNAnalytics with Apache License 2.0 | 4 votes |
/** * Sends the loading status as JSON to the parameter HTTP response. Copied from NameNode. * * @param resp the HTTP response * @throws IOException error in fetching loading status */ public void sendLoadingStatus(HttpServletResponse resp) throws IOException { String count = "count"; String elapsedTime = "elapsedTime"; String file = "file"; String name = "name"; String desc = "desc"; String percentComplete = "percentComplete"; String phases = "phases"; String size = "size"; String status = "status"; String steps = "steps"; String total = "total"; StartupProgressView view = NameNode.getStartupProgress().createView(); JsonGenerator json = new JsonFactory().createJsonGenerator(resp.getWriter()).useDefaultPrettyPrinter(); try { json.writeStartObject(); json.writeNumberField(elapsedTime, view.getElapsedTime()); json.writeNumberField(percentComplete, view.getPercentComplete()); json.writeArrayFieldStart(phases); for (Phase phase : view.getPhases()) { json.writeStartObject(); json.writeStringField(name, phase.getName()); json.writeStringField(desc, phase.getDescription()); json.writeStringField(status, view.getStatus(phase).toString()); json.writeNumberField(percentComplete, view.getPercentComplete(phase)); json.writeNumberField(elapsedTime, view.getElapsedTime(phase)); writeStringFieldIfNotNull(json, file, view.getFile(phase)); writeNumberFieldIfDefined(json, size, view.getSize(phase)); json.writeArrayFieldStart(steps); for (Step step : view.getSteps(phase)) { json.writeStartObject(); StepType stepType = step.getType(); if (stepType != null) { json.writeStringField(name, stepType.getName()); json.writeStringField(desc, stepType.getDescription()); } json.writeNumberField(count, view.getCount(phase, step)); writeStringFieldIfNotNull(json, file, step.getFile()); writeNumberFieldIfDefined(json, size, step.getSize()); json.writeNumberField(total, view.getTotal(phase, step)); json.writeNumberField(percentComplete, view.getPercentComplete(phase, step)); json.writeNumberField(elapsedTime, view.getElapsedTime(phase, step)); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } finally { IOUtils.closeStream(json); } }
Example 9
Source File: StartupProgressServlet.java From hadoop with Apache License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("application/json; charset=UTF-8"); StartupProgress prog = NameNodeHttpServer.getStartupProgressFromContext( getServletContext()); StartupProgressView view = prog.createView(); JsonGenerator json = new JsonFactory().createJsonGenerator(resp.getWriter()); try { json.writeStartObject(); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime()); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete()); json.writeArrayFieldStart(PHASES); for (Phase phase: view.getPhases()) { json.writeStartObject(); json.writeStringField(NAME, phase.getName()); json.writeStringField(DESC, phase.getDescription()); json.writeStringField(STATUS, view.getStatus(phase).toString()); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete(phase)); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime(phase)); writeStringFieldIfNotNull(json, FILE, view.getFile(phase)); writeNumberFieldIfDefined(json, SIZE, view.getSize(phase)); json.writeArrayFieldStart(STEPS); for (Step step: view.getSteps(phase)) { json.writeStartObject(); StepType type = step.getType(); if (type != null) { json.writeStringField(NAME, type.getName()); json.writeStringField(DESC, type.getDescription()); } json.writeNumberField(COUNT, view.getCount(phase, step)); writeStringFieldIfNotNull(json, FILE, step.getFile()); writeNumberFieldIfDefined(json, SIZE, step.getSize()); json.writeNumberField(TOTAL, view.getTotal(phase, step)); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete(phase, step)); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime(phase, step)); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } finally { IOUtils.cleanup(LOG, json); } }
Example 10
Source File: StartupProgressServlet.java From big-c with Apache License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("application/json; charset=UTF-8"); StartupProgress prog = NameNodeHttpServer.getStartupProgressFromContext( getServletContext()); StartupProgressView view = prog.createView(); JsonGenerator json = new JsonFactory().createJsonGenerator(resp.getWriter()); try { json.writeStartObject(); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime()); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete()); json.writeArrayFieldStart(PHASES); for (Phase phase: view.getPhases()) { json.writeStartObject(); json.writeStringField(NAME, phase.getName()); json.writeStringField(DESC, phase.getDescription()); json.writeStringField(STATUS, view.getStatus(phase).toString()); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete(phase)); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime(phase)); writeStringFieldIfNotNull(json, FILE, view.getFile(phase)); writeNumberFieldIfDefined(json, SIZE, view.getSize(phase)); json.writeArrayFieldStart(STEPS); for (Step step: view.getSteps(phase)) { json.writeStartObject(); StepType type = step.getType(); if (type != null) { json.writeStringField(NAME, type.getName()); json.writeStringField(DESC, type.getDescription()); } json.writeNumberField(COUNT, view.getCount(phase, step)); writeStringFieldIfNotNull(json, FILE, step.getFile()); writeNumberFieldIfDefined(json, SIZE, step.getSize()); json.writeNumberField(TOTAL, view.getTotal(phase, step)); json.writeNumberField(PERCENT_COMPLETE, view.getPercentComplete(phase, step)); json.writeNumberField(ELAPSED_TIME, view.getElapsedTime(phase, step)); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } json.writeEndArray(); json.writeEndObject(); } finally { IOUtils.cleanup(LOG, json); } }
Example 11
Source File: ParquetAsJsonInputFormat.java From iow-hadoop-streaming with Apache License 2.0 | 4 votes |
private void groupToJson(JsonGenerator currentGenerator, SimpleGroup grp) throws IOException { GroupType gt = grp.getType(); currentGenerator.writeStartObject(); for(int i = 0; i < gt.getFieldCount(); i ++) { String field = gt.getFieldName(i); try { Type t = gt.getType(i); int repetition = 1; boolean repeated = false; if (t.getRepetition() == Type.Repetition.REPEATED) { repeated = true; repetition = grp.getFieldRepetitionCount(i); currentGenerator.writeArrayFieldStart(field); } else currentGenerator.writeFieldName(field); for(int j = 0; j < repetition; j ++) { if (t.isPrimitive()) { switch (t.asPrimitiveType().getPrimitiveTypeName()) { case BINARY: currentGenerator.writeString(grp.getString(i, j)); break; case INT32: currentGenerator.writeNumber(grp.getInteger(i, j)); break; case INT96: case INT64: // clumsy way - TODO - Subclass SimpleGroup or something like that currentGenerator.writeNumber(Long.parseLong(grp.getValueToString(i, j))); break; case DOUBLE: case FLOAT: currentGenerator.writeNumber(Double.parseDouble(grp.getValueToString(i, j))); break; case BOOLEAN: currentGenerator.writeBoolean(grp.getBoolean(i, j)); break; default: throw new RuntimeException("Can't handle type " + gt.getType(i)); } } else { groupToJson(currentGenerator, (SimpleGroup) grp.getGroup(i, j)); } } if (repeated) currentGenerator.writeEndArray(); } catch (Exception e) { if (e.getMessage().startsWith("not found") && gt.getType(i).getRepetition() == Type.Repetition.OPTIONAL) currentGenerator.writeNull(); else throw new RuntimeException(e); } } currentGenerator.writeEndObject(); }
Example 12
Source File: ResponseBuilder.java From ReactiveLab with Apache License 2.0 | 4 votes |
private static void generateResponse(BackendResponse responseA, BackendResponse responseB, BackendResponse responseC, BackendResponse responseD, BackendResponse responseE, JsonGenerator jsonGenerator) throws IOException, JsonGenerationException { jsonGenerator.writeStartObject(); // multiplication of C, D, E responseKey jsonGenerator.writeNumberField("responseKey", responseC.getResponseKey() + responseD.getResponseKey() + responseE.getResponseKey()); // delay values of each response jsonGenerator.writeArrayFieldStart("delay"); writeTuple(jsonGenerator, "a", responseA.getDelay()); writeTuple(jsonGenerator, "b", responseB.getDelay()); writeTuple(jsonGenerator, "c", responseC.getDelay()); writeTuple(jsonGenerator, "d", responseD.getDelay()); writeTuple(jsonGenerator, "e", responseE.getDelay()); jsonGenerator.writeEndArray(); // itemSize values of each response jsonGenerator.writeArrayFieldStart("itemSize"); writeTuple(jsonGenerator, "a", responseA.getItemSize()); writeTuple(jsonGenerator, "b", responseB.getItemSize()); writeTuple(jsonGenerator, "c", responseC.getItemSize()); writeTuple(jsonGenerator, "d", responseD.getItemSize()); writeTuple(jsonGenerator, "e", responseE.getItemSize()); jsonGenerator.writeEndArray(); // numItems values of each response jsonGenerator.writeArrayFieldStart("numItems"); writeTuple(jsonGenerator, "a", responseA.getNumItems()); writeTuple(jsonGenerator, "b", responseB.getNumItems()); writeTuple(jsonGenerator, "c", responseC.getNumItems()); writeTuple(jsonGenerator, "d", responseD.getNumItems()); writeTuple(jsonGenerator, "e", responseE.getNumItems()); jsonGenerator.writeEndArray(); // all items from responses jsonGenerator.writeArrayFieldStart("items"); addItemsFromResponse(jsonGenerator, responseA); addItemsFromResponse(jsonGenerator, responseB); addItemsFromResponse(jsonGenerator, responseC); addItemsFromResponse(jsonGenerator, responseD); addItemsFromResponse(jsonGenerator, responseE); jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); }
Example 13
Source File: ServiceResponseBuilder.java From WSPerfLab with Apache License 2.0 | 4 votes |
public static ByteArrayOutputStream buildTestAResponse(JsonFactory jsonFactory, BackendResponse responseA, BackendResponse responseB, BackendResponse responseC, BackendResponse responseD, BackendResponse responseE) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos); jsonGenerator.writeStartObject(); // multiplication of C, D, E responseKey jsonGenerator.writeNumberField("responseKey", responseC.getResponseKey() + responseD.getResponseKey() + responseE.getResponseKey()); // delay values of each response jsonGenerator.writeArrayFieldStart("delay"); writeTuple(jsonGenerator, "a", responseA.getDelay()); writeTuple(jsonGenerator, "b", responseB.getDelay()); writeTuple(jsonGenerator, "c", responseC.getDelay()); writeTuple(jsonGenerator, "d", responseD.getDelay()); writeTuple(jsonGenerator, "e", responseE.getDelay()); jsonGenerator.writeEndArray(); // itemSize values of each response jsonGenerator.writeArrayFieldStart("itemSize"); writeTuple(jsonGenerator, "a", responseA.getItemSize()); writeTuple(jsonGenerator, "b", responseB.getItemSize()); writeTuple(jsonGenerator, "c", responseC.getItemSize()); writeTuple(jsonGenerator, "d", responseD.getItemSize()); writeTuple(jsonGenerator, "e", responseE.getItemSize()); jsonGenerator.writeEndArray(); // numItems values of each response jsonGenerator.writeArrayFieldStart("numItems"); writeTuple(jsonGenerator, "a", responseA.getNumItems()); writeTuple(jsonGenerator, "b", responseB.getNumItems()); writeTuple(jsonGenerator, "c", responseC.getNumItems()); writeTuple(jsonGenerator, "d", responseD.getNumItems()); writeTuple(jsonGenerator, "e", responseE.getNumItems()); jsonGenerator.writeEndArray(); // all items from responses jsonGenerator.writeArrayFieldStart("items"); addItemsFromResponse(jsonGenerator, responseA); addItemsFromResponse(jsonGenerator, responseB); addItemsFromResponse(jsonGenerator, responseC); addItemsFromResponse(jsonGenerator, responseD); addItemsFromResponse(jsonGenerator, responseE); jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); jsonGenerator.close(); return bos; }