Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#writeObject()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeObject() . 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: MessageList.java    From event-streams-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Build message list dependent on the format Event Streams requires. The
 * message list is in the form: [{ "value": base_64_string }, ...]
 *
 * @return {String} String representation of a JSON object.
 * @throws IOException
 */
public String build() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);

    jsonGenerator.writeStartArray();
    for (int i = 0; i < this.messages.size(); i++) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("value");
        jsonGenerator.writeObject(this.messages.get(i));
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();

    jsonGenerator.close();
    outputStream.close();

    return new String(outputStream.toByteArray());
}
 
Example 2
Source File: DataArrayResultSerializer.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void serialize(DataArrayResult value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    long count = value.getCount();
    if (count >= 0) {
        gen.writeNumberField(AT_IOT_COUNT, count);
    }
    String nextLink = value.getNextLink();
    if (nextLink != null) {
        gen.writeStringField(AT_IOT_NEXT_LINK, nextLink);
    }

    gen.writeFieldName("value");
    gen.writeObject(value.getValue());
    gen.writeEndObject();
}
 
Example 3
Source File: CoordinatedClusterSerializer.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize( final ICoordinatedCluster value, final JsonGenerator jgen,
                       final SerializerProvider provider ) throws IOException {

    jgen.writeStartObject();

    jgen.writeStringField( NAME, value.getName() );
    jgen.writeNumberField( SIZE, value.getSize() );
    jgen.writeObjectField( INSTANCE_SPEC, value.getInstanceSpec() );

    jgen.writeArrayFieldStart( INSTANCES );

    for( Instance instance: value.getInstances() ) {
        jgen.writeObject( instance );
    }

    jgen.writeEndArray();

    jgen.writeEndObject();
}
 
Example 4
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
private String buildJSONFromFields(Collection<SearchHitField> values) {
	JsonFactory nodeFactory = new JsonFactory();
	try {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
		generator.writeStartObject();
		for (SearchHitField value : values) {
			if (value.getValues().size() > 1) {
				generator.writeArrayFieldStart(value.getName());
				for (Object val : value.getValues()) {
					generator.writeObject(val);
				}
				generator.writeEndArray();
			} else {
				generator.writeObjectField(value.getName(), value.getValue());
			}
		}
		generator.writeEndObject();
		generator.flush();
		return new String(stream.toByteArray(), Charset.forName("UTF-8"));
	} catch (IOException e) {
		return null;
	}
}
 
Example 5
Source File: ApplicationAgentsListSerializer.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void writeAgentList(JsonGenerator jgen, List<AgentInfo> agentList) throws IOException {
    jgen.writeStartArray();
    for (AgentInfo agentInfo : agentList) {
        jgen.writeObject(agentInfo);
    }
    jgen.writeEndArray();
}
 
Example 6
Source File: ApiAnnotationIntrospector.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Nullable
private static <TFrom, TTo> JsonSerializer<TFrom> getJsonSerializer(
    @Nullable final Transformer<TFrom, TTo> serializer) {
  if (serializer == null) {
    return null;
  }
  return new JsonSerializer<TFrom>() {
    @Override
    public void serialize(TFrom value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException {
      jgen.writeObject(serializer.transformTo(value));
    }
  };
}
 
Example 7
Source File: BasicObjectSerializer.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeObject(Object obj, JsonGenerator jgen, boolean expand) throws JsonProcessingException, IOException {
  logger.debug("array");
  // write out an array of objects.
  jgen.writeStartArray();
  final int length = Array.getLength(obj);
  for (int i = 0; i < length; ++i) {
    Object o = Array.get(obj, i);
    if (!parent.writeObject(o, jgen, false)) {
      jgen.writeObject(o.toString());
    }
  }
  jgen.writeEndArray();
  return true;
}
 
Example 8
Source File: JSONUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static String toInterpretableJsonString(Object aObject)
    throws IOException
{
    StringWriter out = new StringWriter();
    JsonGenerator jsonGenerator = JSONUtil.getObjectMapper().getFactory().createGenerator(out);
    jsonGenerator.setCharacterEscapes(JavaScriptCharacterEscapes.get());
    jsonGenerator.writeObject(aObject);
    return out.toString();
}
 
Example 9
Source File: Container.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * Special handling for profiles serialization to ensure that metadata and checksum
 * information is serialized at the same level as profiles and not nested inside .
 *
 * @param gen
 * @param profiles
 * @throws IOException
 */
private void serializeProfiles(JsonGenerator gen, Profiles profiles) throws IOException {
    // PM-1617 metadata and checksum are at the profiles level , not nested in there
    // remove the metadata and add back after serializing profiles
    Metadata m = (Metadata) profiles.remove(Profiles.NAMESPACES.metadata);

    if (!profiles.isEmpty()) {
        gen.writeFieldName(TransformationCatalogKeywords.PROFILES.getReservedName());
        gen.writeObject(profiles);
    }
    gen.writeObject(m);
    profiles.set(m);
}
 
Example 10
Source File: EntitySetSerializer.java    From FROST-Server with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void serialize(EntitySet value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    gen.writeFieldName("value");
    gen.writeObject(value.asList());
    gen.writeEndObject();
}
 
Example 11
Source File: TimestampToISODateStringSerializer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Long timestamp, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  // Joda may throw an exception if the timestamp is MAX_LONG so protect ourselves here.
  try {
    DateTime dateTime = new DateTime(timestamp).withZone(DateTimeZone.UTC);
    jsonGenerator.writeObject(dateTime.toString(ISODateTimeFormat.dateTime()));
  } catch (IllegalFieldValueException e) {
    jsonGenerator.writeString("ERROR");
  }
}
 
Example 12
Source File: JsonUtil.java    From jea with Apache License 2.0 5 votes vote down vote up
/**
 * 将对象转换成Json串
 * 
 * @param object
 * @return
 * @throws Exception
 */
public static <T> String generatorJson(T object) throws Exception{
	StringWriter writer = new StringWriter();
	JsonGenerator generator = factory.createGenerator(writer);
	try{
		generator.writeObject(object);
		return writer.toString();
	} finally {
		generator.close();
		writer.close();
	}
}
 
Example 13
Source File: JSONOptions.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(JSONOptions value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
    JsonGenerationException {
  if (value.opaque != null) {
    jgen.writeObject(value.opaque);
  } else {
    jgen.writeTree(value.root);
  }

}
 
Example 14
Source File: CustomOauthExceptionSerializer.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(CustomOauthException value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    gen.writeObject(value.getResult());
}
 
Example 15
Source File: CDataTypeSerializer.java    From Rhombus with MIT License 4 votes vote down vote up
@Override
public void serialize(CField.CDataType value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException
{
	jsonGenerator.writeObject(value.toString());
}
 
Example 16
Source File: BladeBeanSerializerModifier.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeObject(Boolean.FALSE);
}
 
Example 17
Source File: RestObjectMapper.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(JsonObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
  jgen.writeObject(value.getMap());
}
 
Example 18
Source File: Fruit.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Fruit value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    jgen.writeObject(value.getActualInstance());
}
 
Example 19
Source File: MajorTypeSerDe.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(MajorType value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
    JsonGenerationException {
  MajorTypeHolder holder = MajorTypeHolder.get(value);
  jgen.writeObject(holder);
}
 
Example 20
Source File: JiraIssueUpdate.java    From spring-data-dev-tools with Apache License 2.0 3 votes vote down vote up
@Override
public void serialize(AssignTo value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

	gen.writeStartArray();

	gen.writeObject(Collections.singletonMap("set", Collections.singletonMap("name", value.getValue())));

	gen.writeEndArray();
}