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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeRawValue() . 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: ThriftJacksonSerializers.java    From armeria with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
static void serializeTBase(@Nullable TBase value, JsonGenerator gen,
                           boolean useNamedEnums) throws IOException {
    if (value == null) {
        gen.writeNull();
        return;
    }

    gen.writeRawValue(serializeTBaseLike(protocol -> {
        try {
            value.write(protocol);
        } catch (TException ex) {
            throw new IllegalArgumentException(ex);
        }
    }, useNamedEnums));
}
 
Example 2
Source File: StatusMonitorServiceImpl.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void summary (Statement s, String name, String sql, JsonGenerator gen, boolean arrayWrapper) throws IOException, SQLException {
   logger.trace("summary: {}", name);
   if (arrayWrapper) {
       gen.writeArrayFieldStart(name);
   } else {
       gen.writeFieldName(name);
   }
   JDBCResultSet rs = (JDBCResultSet)s.executeQuery(sql);
   StringWriter strings = new StringWriter();
   PrintWriter writer = new PrintWriter(strings);
   collectResults(rs, writer, options);
   gen.writeRawValue(strings.toString());
   if (arrayWrapper) {
       gen.writeEndArray();
   }
}
 
Example 3
Source File: FastJsonHttpLogFormatter.java    From logbook with MIT License 6 votes vote down vote up
private void writeBody(
        final HttpMessage message,
        final JsonGenerator generator) throws IOException {

    final String body = message.getBodyAsString();

    if (body.isEmpty()) {
        return;
    }
    generator.writeFieldName("body");

    final String contentType = message.getContentType();

    if (JsonMediaType.JSON.test(contentType)) {
        generator.writeRawValue(body);
    } else {
        generator.writeString(body);
    }
}
 
Example 4
Source File: OperationStats.java    From vespa with Apache License 2.0 6 votes vote down vote up
public String getStatsAsJson() {
    try {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeArrayFieldStart("clusters");
        for (ClusterConnection cluster : clusters) {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeNumberField("clusterid", cluster.getClusterId());
            jsonGenerator.writeFieldName("stats");
            jsonGenerator.writeRawValue(cluster.getStatsAsJSon());
            jsonGenerator.writeEndObject();
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.writeFieldName("sessionParams");
        jsonGenerator.writeRawValue(sessionParamsAsXmlString);
        jsonGenerator.writeFieldName("throttleDebugMessage");
        jsonGenerator.writeRawValue("\"" + throttler.getDebugMessage() + "\"");
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
        return stringWriter.toString();
    } catch (IOException e) {
        return "{ \"Error\" : \""+ e.getMessage() + "\"}";
    }
}
 
Example 5
Source File: ProtoJsonSerializer.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Message message, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeRawValue(JsonFormat.printer()
            .includingDefaultValueFields()
            .omittingInsignificantWhitespace()
            .print(message));
}
 
Example 6
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@Override
public void serialize(Map<String, T> value, JsonGenerator g) throws IOException, JsonGenerationException {
    g.writeStartObject();
    for (Map.Entry<String, T> e : value.entrySet()) {
        g.writeFieldName(e.getKey());
        g.writeRawValue(underlying.serialize(e.getValue()));
    }
    g.writeEndObject();
}
 
Example 7
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void writeJsonValue(JsonGenerator jsonGenerator, Block block, int position, ConnectorSession session)
        throws IOException
{
    if (block.isNull(position)) {
        jsonGenerator.writeNull();
    }
    else {
        Slice value = JSON.getSlice(block, position);
        jsonGenerator.writeRawValue(value.toStringUtf8());
    }
}
 
Example 8
Source File: SimpleAvroDataSerializer.java    From kafka-webview with MIT License 5 votes vote down vote up
private void writeIncludingSchema(final GenericData.Record value, final JsonGenerator gen) throws IOException {
    // Start new object.
    gen.writeStartObject();

    // Write value
    gen.writeFieldName("value");
    gen.writeRawValue(value.toString());

    // Write schema
    gen.writeFieldName("schema");
    gen.writeRawValue(value.getSchema().toString());

    // End object
    gen.writeEndObject();
}
 
Example 9
Source File: RawValue.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void _serialize(JsonGenerator gen) throws IOException
{
    if (_value instanceof SerializableString) {
        gen.writeRawValue((SerializableString) _value);
    } else {
        gen.writeRawValue(String.valueOf(_value));
    }
}
 
Example 10
Source File: EntitySerializer.java    From FROST-Server with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void serializeFieldCustomized(
        Entity entity,
        JsonGenerator gen,
        BeanPropertyDefinition property,
        List<BeanPropertyDefinition> properties,
        CustomSerialization annotation) throws IOException {
    // check if encoding field is present in current bean
    // get value
    // call CustomSerializationManager
    Optional<BeanPropertyDefinition> encodingProperty = properties.stream().filter(p -> p.getName().equals(annotation.encoding())).findFirst();
    if (!encodingProperty.isPresent()) {
        throw new JsonGenerationException("can not serialize instance of class '" + entity.getClass() + "'! \n"
                + "Reason: trying to use custom serialization for field '" + property.getName() + "' but field '" + annotation.encoding() + "' specifying enconding is not present!",
                gen);
    }
    Object value = encodingProperty.get().getAccessor().getValue(entity);
    String encodingType = null;
    if (value != null) {
        encodingType = value.toString();
    }
    String customJson = CustomSerializationManager.getInstance()
            .getSerializer(encodingType)
            .serialize(property.getAccessor().getValue(entity));
    if (customJson != null && !customJson.isEmpty()) {
        gen.writeFieldName(property.getName());
        gen.writeRawValue(customJson);
    }
}
 
Example 11
Source File: JRTServerConfigRequestV3.java    From vespa with Apache License 2.0 5 votes vote down vote up
protected void addCommonReturnValues(JsonGenerator jsonGenerator) throws IOException {
    ConfigKey<?> key = requestData.getConfigKey();
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_VERSION, getProtocolVersion());
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_DEF_NAME, key.getName());
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_DEF_NAMESPACE, key.getNamespace());
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_DEF_MD5, key.getMd5());
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_CONFIGID, key.getConfigId());
    setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_CLIENT_HOSTNAME, requestData.getClientHostName());
    jsonGenerator.writeFieldName(SlimeResponseData.RESPONSE_TRACE);
    jsonGenerator.writeRawValue(getRequestTrace().toString(true));
}
 
Example 12
Source File: StringJsonElementSerializer.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeRawValue(value);
}
 
Example 13
Source File: JavaScriptFunction.java    From chart with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(JavaScriptFunction value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
	gen.writeRawValue(value.function);
}
 
Example 14
Source File: FolderSerializerWithInternalObjectMapper.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {

    gen.writeStartObject();
    gen.writeStringField("name", value.getName());

    // we access internal mapper to delegate the serialization of File list  
    ObjectMapper mapper = (ObjectMapper) gen.getCodec();

    gen.writeFieldName("files");
    String stringValue = mapper.writeValueAsString(value.getFiles());
    gen.writeRawValue(stringValue);

    gen.writeEndObject();

}
 
Example 15
Source File: JSONObjectSerializer.java    From pagerduty-client with MIT License 4 votes vote down vote up
@Override
public void serialize(JSONObject jsonObject, JsonGenerator jgen,
                      SerializerProvider provider) throws IOException{
    jgen.writeRawValue(jsonObject.toString());
}
 
Example 16
Source File: ScriptableOptionSerializer.java    From wicket-chartjs with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ScriptAware value, JsonGenerator gen, SerializerProvider provider)
    throws IOException {
  gen.writeRawValue(value.getScript());
}
 
Example 17
Source File: VTypeSerializer.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void serialize(VType vType, JsonGenerator gen, SerializerProvider serializers) throws IOException {
	gen.writeRawValue(VTypeToJson.toJson(vType).toString());
}
 
Example 18
Source File: VTypeSerializer.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void serialize(VType vType, JsonGenerator gen, SerializerProvider serializers) throws IOException {
	gen.writeRawValue(VTypeToJson.toJson(vType).toString());
}
 
Example 19
Source File: SimpleAvroDataSerializer.java    From kafka-webview with MIT License 4 votes vote down vote up
private void writeValueOnly(final GenericData.Record value, final JsonGenerator gen) throws IOException {
    gen.writeRawValue(value.toString());
}
 
Example 20
Source File: DaprStateAsyncProvider.java    From java-sdk with MIT License 4 votes vote down vote up
/**
 * Saves state changes transactionally.
 * [
 * {
 * "operation": "upsert",
 * "request": {
 * "key": "key1",
 * "value": "myData"
 * }
 * },
 * {
 * "operation": "delete",
 * "request": {
 * "key": "key2"
 * }
 * }
 * ]
 *
 * @param actorType    Name of the actor being changed.
 * @param actorId      Identifier of the actor being changed.
 * @param stateChanges Collection of changes to be performed transactionally.
 * @return Void.
 */
Mono<Void> apply(String actorType, ActorId actorId, ActorStateChange... stateChanges) {
  if ((stateChanges == null) || stateChanges.length == 0) {
    return Mono.empty();
  }

  int count = 0;
  // Constructing the JSON via a stream API to avoid creating transient objects to be instantiated.
  byte[] payload = null;
  try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
    JsonGenerator generator = JSON_FACTORY.createGenerator(writer);
    // Start array
    generator.writeStartArray();

    for (ActorStateChange stateChange : stateChanges) {
      if ((stateChange == null) || (stateChange.getChangeKind() == null)) {
        continue;
      }

      String operationName = stateChange.getChangeKind().getDaprStateChangeOperation();
      if ((operationName == null) || (operationName.length() == 0)) {
        continue;
      }

      count++;

      // Start operation object.
      generator.writeStartObject();
      generator.writeStringField("operation", operationName);

      // Start request object.
      generator.writeObjectFieldStart("request");
      generator.writeStringField("key", stateChange.getStateName());
      if ((stateChange.getChangeKind() == ActorStateChangeKind.UPDATE)
          || (stateChange.getChangeKind() == ActorStateChangeKind.ADD)) {
        byte[] data = this.stateSerializer.serialize(stateChange.getValue());
        if (data != null) {
          if (this.isStateSerializerDefault) {
            // DefaultObjectSerializer is a JSON serializer, so we just pass it on.
            generator.writeFieldName("value");
            generator.writeRawValue(new String(data, CHARSET));
          } else {
            // Custom serializer uses byte[].
            generator.writeBinaryField("value", data);
          }
        }
      }
      // End request object.
      generator.writeEndObject();

      // End operation object.
      generator.writeEndObject();
    }

    // End array
    generator.writeEndArray();

    generator.close();
    writer.flush();
    payload = writer.toByteArray();
  } catch (IOException e) {
    e.printStackTrace();
    return Mono.error(e);
  }

  if (count == 0) {
    // No-op since there is no operation to be performed.
    Mono.empty();
  }

  return this.daprClient.saveActorStateTransactionally(actorType, actorId.toString(), payload);
}