Java Code Examples for org.codehaus.jackson.JsonGenerator#writeString()

The following examples show how to use org.codehaus.jackson.JsonGenerator#writeString() . 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: RepositoryImpl.java    From bintray-client-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: AttributesSearchQueryImpl.java    From bintray-client-java with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ObjectMapperProvider.java    From hraven with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(Configuration conf, JsonGenerator jsonGenerator,
    SerializerProvider serializerProvider) throws IOException {
  SerializationContext context = RestResource.serializationContext
      .get();
  Predicate<String> configFilter = context.getConfigurationFilter();
  Iterator<Map.Entry<String, String>> keyValueIterator = conf.iterator();

  jsonGenerator.writeStartObject();

  // here's where we can filter out keys if we want
  while (keyValueIterator.hasNext()) {
    Map.Entry<String, String> kvp = keyValueIterator.next();
    if (configFilter == null || configFilter.apply(kvp.getKey())) {
      jsonGenerator.writeFieldName(kvp.getKey());
      jsonGenerator.writeString(kvp.getValue());
    }
  }
  jsonGenerator.writeEndObject();
}
 
Example 4
Source File: DefaultAnonymizingRumenSerializer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public void serialize(AnonymizableDataType object, JsonGenerator jGen, 
                      SerializerProvider sProvider) 
throws IOException, JsonProcessingException {
  Object val = object.getAnonymizedValue(statePool, conf);
  // output the data if its a string
  if (val instanceof String) {
    jGen.writeString(val.toString());
  } else {
    // let the mapper (JSON generator) handle this anonymized object.
    jGen.writeObject(val);
  }
}
 
Example 5
Source File: ObjectMapperProvider.java    From hraven with Apache License 2.0 5 votes vote down vote up
/**
 * checks if the member is to be filtered out or no if filter itself is
 * null, writes out that member as a String
 *
 * @param member
 * @param includeFilter
 * @param taskObject
 * @param jsonGenerator
 * @throws JsonGenerationException
 * @throws IOException
 */
public static void filteredWrite(String member, Predicate<String> includeFilter,
    String taskObject, JsonGenerator jsonGenerator)
    throws JsonGenerationException, IOException {
  if (includeFilter != null) {
    if (includeFilter.apply(member)) {
      jsonGenerator.writeFieldName(member);
      jsonGenerator.writeString(taskObject);
    }
  } else {
    jsonGenerator.writeFieldName(member);
    jsonGenerator.writeString(taskObject);
  }
}
 
Example 6
Source File: JsonDateSerializer.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
        throws IOException, JsonProcessingException {
    String formattedDate = null;
    if (date != null) {
        formattedDate = dateFormat.format(date);
    }
    jsonGenerator.writeString(formattedDate);
}
 
Example 7
Source File: JMXJsonServlet.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void writeObject(JsonGenerator jg, Object value) throws IOException {
  if(value == null) {
    jg.writeNull();
  } else {
    Class<?> c = value.getClass();
    if (c.isArray()) {
      jg.writeStartArray();
      int len = Array.getLength(value);
      for (int j = 0; j < len; j++) {
        Object item = Array.get(value, j);
        writeObject(jg, item);
      }
      jg.writeEndArray();
    } else if(value instanceof Number) {
      Number n = (Number)value;
      jg.writeNumber(n.toString());
    } else if(value instanceof Boolean) {
      Boolean b = (Boolean)value;
      jg.writeBoolean(b);
    } else if(value instanceof CompositeData) {
      CompositeData cds = (CompositeData)value;
      CompositeType comp = cds.getCompositeType();
      Set<String> keys = comp.keySet();
      jg.writeStartObject();
      for(String key: keys) {
        writeAttribute(jg, key, cds.get(key));
      }
      jg.writeEndObject();
    } else if(value instanceof TabularData) {
      TabularData tds = (TabularData)value;
      jg.writeStartArray();
      for(Object entry : tds.values()) {
        writeObject(jg, entry);
      }
      jg.writeEndArray();
    } else {
      jg.writeString(value.toString());
    }
  }
}
 
Example 8
Source File: Log4Json.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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 9
Source File: MediaTypeSerializer.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final MediaType mediaType, final JsonGenerator generator, final SerializerProvider provider)
        throws IOException {
    generator.writeStartObject();
    generator.writeFieldName("name");
    generator.writeString(mediaType.name());
    generator.writeFieldName("displayName");
    generator.writeString(mediaType.getDisplayName());
    generator.writeEndObject();
}
 
Example 10
Source File: DefaultAnonymizingRumenSerializer.java    From big-c with Apache License 2.0 5 votes vote down vote up
public void serialize(AnonymizableDataType object, JsonGenerator jGen, 
                      SerializerProvider sProvider) 
throws IOException, JsonProcessingException {
  Object val = object.getAnonymizedValue(statePool, conf);
  // output the data if its a string
  if (val instanceof String) {
    jGen.writeString(val.toString());
  } else {
    // let the mapper (JSON generator) handle this anonymized object.
    jGen.writeObject(val);
  }
}
 
Example 11
Source File: DefaultRumenSerializer.java    From big-c with Apache License 2.0 5 votes vote down vote up
public void serialize(DataType object, JsonGenerator jGen, SerializerProvider sProvider) 
throws IOException, JsonProcessingException {
  Object data = object.getValue();
  if (data instanceof String) {
    jGen.writeString(data.toString());
  } else {
    jGen.writeObject(data);
  }
}
 
Example 12
Source File: AppPackage.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    PropertyInfo propertyInfo, JsonGenerator jgen, SerializerProvider provider)
  throws IOException, JsonProcessingException
{
  if (provideDescription) {
    jgen.writeStartObject();
    jgen.writeStringField("value", propertyInfo.value);
    jgen.writeStringField("description", propertyInfo.description);
    jgen.writeEndObject();
  } else {
    jgen.writeString(propertyInfo.value);
  }
}
 
Example 13
Source File: Log4Json.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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 14
Source File: AppPackage.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    PropertyInfo propertyInfo, JsonGenerator jgen, SerializerProvider provider)
  throws IOException, JsonProcessingException
{
  if (provideDescription) {
    jgen.writeStartObject();
    jgen.writeStringField("value", propertyInfo.value);
    jgen.writeStringField("description", propertyInfo.description);
    jgen.writeEndObject();
  } else {
    jgen.writeString(propertyInfo.value);
  }
}
 
Example 15
Source File: JsonDateSerializer.java    From ranger with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Date date, JsonGenerator gen,
		SerializerProvider provider) throws IOException,
		JsonProcessingException {

	String formattedDate = new SimpleDateFormat(DATE_FORMAT).format(date);
	gen.writeString(formattedDate);
}
 
Example 16
Source File: ServiceResponseBuilder.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
private static void addItemsFromResponse(JsonGenerator jsonGenerator, BackendResponse a) throws IOException {
    for (String s : a.getItems()) {
        jsonGenerator.writeString(s);
    }
}
 
Example 17
Source File: Json.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
	JsonProcessingException {
	jgen.writeString(ISODateTimeFormat.dateTime().print(value));
}
 
Example 18
Source File: ParquetAsJsonInputFormat.java    From iow-hadoop-streaming with Apache License 2.0 4 votes vote down vote up
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 19
Source File: Json.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
	JsonProcessingException {
	jgen.writeString(value.toString("h:mmaa"));
}
 
Example 20
Source File: ObjectMapperProvider.java    From hraven with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(HdfsStats hdfsStats, JsonGenerator jsonGenerator,
    SerializerProvider serializerProvider) throws IOException {

  jsonGenerator.writeStartObject();
  jsonGenerator.writeFieldName("hdfsStatsKey");
  HdfsStatsKey hk = hdfsStats.getHdfsStatsKey();
  QualifiedPathKey qpk = hk.getQualifiedPathKey();
  jsonGenerator.writeStartObject();
  jsonGenerator.writeFieldName("cluster");
  jsonGenerator.writeString(qpk.getCluster());
  if (StringUtils.isNotBlank(qpk.getNamespace())) {
    jsonGenerator.writeFieldName("namespace");
    jsonGenerator.writeString(qpk.getNamespace());
  }
  jsonGenerator.writeFieldName("path");
  jsonGenerator.writeString(qpk.getPath());
  jsonGenerator.writeFieldName("runId");
  jsonGenerator.writeNumber(hk.getRunId());
  jsonGenerator.writeEndObject();
  jsonGenerator.writeFieldName("fileCount");
  jsonGenerator.writeNumber(hdfsStats.getFileCount());
  jsonGenerator.writeFieldName("dirCount");
  jsonGenerator.writeNumber(hdfsStats.getDirCount());
  jsonGenerator.writeFieldName("spaceConsumed");
  jsonGenerator.writeNumber(hdfsStats.getSpaceConsumed());
  jsonGenerator.writeFieldName("owner");
  jsonGenerator.writeString(hdfsStats.getOwner());
  jsonGenerator.writeFieldName("quota");
  jsonGenerator.writeNumber(hdfsStats.getQuota());
  jsonGenerator.writeFieldName("spaceQuota");
  jsonGenerator.writeNumber(hdfsStats.getSpaceQuota());
  jsonGenerator.writeFieldName("trashFileCount");
  jsonGenerator.writeNumber(hdfsStats.getTrashFileCount());
  jsonGenerator.writeFieldName("trashSpaceConsumed");
  jsonGenerator.writeNumber(hdfsStats.getTrashSpaceConsumed());
  jsonGenerator.writeFieldName("tmpFileCount");
  jsonGenerator.writeNumber(hdfsStats.getTmpFileCount());
  jsonGenerator.writeFieldName("tmpSpaceConsumed");
  jsonGenerator.writeNumber(hdfsStats.getTmpSpaceConsumed());
  jsonGenerator.writeFieldName("accessCountTotal");
  jsonGenerator.writeNumber(hdfsStats.getAccessCountTotal());
  jsonGenerator.writeFieldName("accessCost");
  jsonGenerator.writeNumber(hdfsStats.getAccessCost());
  jsonGenerator.writeFieldName("storageCost");
  jsonGenerator.writeNumber(hdfsStats.getStorageCost());
  jsonGenerator.writeFieldName("hdfsCost");
  jsonGenerator.writeNumber(hdfsStats.getHdfsCost());
  jsonGenerator.writeEndObject();
}