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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeFieldName() . 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: ClusterConnection.java    From vespa with Apache License 2.0 6 votes vote down vote up
public String getStatsAsJSon() throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeArrayFieldStart("session");
    for (IOThread ioThread : ioThreads) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeObjectFieldStart("endpoint");
        jsonGenerator.writeStringField("host", ioThread.getEndpoint().getHostname());
        jsonGenerator.writeNumberField("port", ioThread.getEndpoint().getPort());
        jsonGenerator.writeEndObject();
        jsonGenerator.writeFieldName("stats");
        IOThread.ConnectionStats connectionStats = ioThread.getConnectionStats();
        objectMapper.writeValue(jsonGenerator, connectionStats);
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
    jsonGenerator.close();
    return stringWriter.toString();
}
 
Example 2
Source File: BasicObjectSerializer.java    From beakerx with Apache License 2.0 6 votes vote down vote up
@Override
public boolean writeObject(Object obj, JsonGenerator jgen, boolean expand) throws JsonProcessingException, IOException {
  logger.debug("generic map");
  // convert this 'on the fly' to a map of objects
  Map<?, ?> m = (Map<?, ?>) obj;

  Set<?> kset = m.keySet();
  if (kset.size() == 0 || !(kset.iterator().next() instanceof String))
    jgen.writeObject(obj.toString());
  else {
    jgen.writeStartObject();
    for (Object k : kset) {
      jgen.writeFieldName((null == k) ? "null" : k.toString());
      if (!parent.writeObject(m.get(k), jgen, false))
        jgen.writeObject(m.get(k) != null ? (m.get(k).toString()) : "null");
    }
    jgen.writeEndObject();
  }
  return true;
}
 
Example 3
Source File: BlockSerDe.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected void doSerialize(Block block, JsonGenerator jgen, SerializerProvider provider)
        throws IOException
{
    jgen.writeStringField(ALLOCATOR_ID_FIELD_NAME, block.getAllocatorId());

    jgen.writeFieldName(SCHEMA_FIELD_NAME);
    schemaSerializer.serialize(block.getSchema(), jgen, provider);

    jgen.writeFieldName(BATCH_FIELD_NAME);
    if (block.getRowCount() > 0) {
        jgen.writeBinary(serializeRecordBatch(block.getRecordBatch()));
    }
    else {
        jgen.writeString("");
    }
}
 
Example 4
Source File: TraceCommonService.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static @Nullable String toJson(@Nullable Queries queries) throws IOException {
    if (queries == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = jsonFactory.createGenerator(CharStreams.asWriter(sb));
    try {
        jg.writeStartObject();
        jg.writeFieldName("queries");
        writeQueries(jg, queries.queries());
        jg.writeFieldName("sharedQueryTexts");
        writeSharedQueryTexts(jg, queries.sharedQueryTexts());
        jg.writeEndObject();
    } finally {
        jg.close();
    }
    return sb.toString();
}
 
Example 5
Source File: HeaderMapSerializer.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
 */
@Override
public void serialize(HeaderMap map, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeStartObject(); // {

    for (String key : map.keySet()) {
        List<String> values = map.getAll(key);

        if (values.size() <= 1) {
            gen.writeStringField(key, values.get(0)); // "key": "value"
        } else {
            gen.writeFieldName(key); // "key":
            gen.writeStartArray(values.size()); // [
            for (String val : values) {
                gen.writeString(val); // "value", ...
            }
            gen.writeEndArray();// ]
        }
    }

    gen.writeEndObject(); // }
}
 
Example 6
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 7
Source File: LongBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(Long[] value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.length;
      Long item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value[i];
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 8
Source File: ThriftJacksonSerializers.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ThriftReply value, JsonGenerator gen, SerializerProvider provider)
        throws IOException {
    if (value == null) {
        // Oneway function doesn't provide reply
        gen.writeNull();
        return;
    }

    gen.writeStartObject();
    gen.writeFieldName("header");
    serializeTMessage(value.header(), gen);

    final TBase<?, ?> result;
    final TApplicationException exception;
    if (value.isException()) {
        result = null;
        exception = value.exception();
    } else {
        result = value.result();
        exception = null;
    }

    gen.writeFieldName("result");
    serializeTBase(result, gen, useNamedEnums);
    gen.writeFieldName("exception");
    serializeTApplicationException(exception, gen, useNamedEnums);
    gen.writeEndObject();
}
 
Example 9
Source File: BeanDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param serializer1 serialization
 */
private byte[] serializer1(List<BeanBean> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    int fieldCount=0;
    jacksonSerializer.writeStartObject();
    if (value!=null)  {
      int n=value.size();
      BeanBean item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          beanBeanBindMap.serializeOnJackson(item, jacksonSerializer);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 10
Source File: DoubleBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(LinkedList<Double> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.size();
      Double item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 11
Source File: BindBeanSharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueByteSet serialization
 */
protected String serializeValueByteSet(Set<Byte> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      // write wrapper tag
      jacksonSerializer.writeFieldName("valueByteSet");
      jacksonSerializer.writeStartArray();
      for (Byte item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 12
Source File: Bean2Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueDoubleSet serialization
 */
public static byte[] serializeValueDoubleSet(HashSet<Double> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (Double item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 13
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeProperty(final ServiceMetadata metadata,
    final EdmProperty edmProperty, final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  boolean isStreamProperty = isStreamProperty(edmProperty);
  if (property != null) {
    if (!isStreamProperty) {
      json.writeFieldName(edmProperty.getName());
    }
    writePropertyValue(metadata, edmProperty, property, selectedPaths, json);
  }
}
 
Example 14
Source File: UsersStreamSerializer.java    From java-json-benchmark with MIT License 5 votes vote down vote up
@Override
public void jackson(JsonGenerator j, Users obj) throws IOException {
    j.writeStartObject();
    if (obj.users != null) {
        j.writeFieldName("users");
        j.writeStartArray();
        for (User u : obj.users) {
            jackson(j, u);
        }
        j.writeEndArray();
    }
    j.writeEndObject();
}
 
Example 15
Source File: CoercedTestRunnerSpec.java    From buck with Apache License 2.0 5 votes vote down vote up
private void writeMap(
    JsonGenerator jsonGenerator,
    Map<Arg, CoercedTestRunnerSpec> data,
    SourcePathResolverAdapter sourcePathResolverAdapter)
    throws IOException {
  jsonGenerator.writeStartObject();
  for (Map.Entry<Arg, CoercedTestRunnerSpec> entry : data.entrySet()) {
    jsonGenerator.writeFieldName(Arg.stringify(entry.getKey(), sourcePathResolverAdapter));
    entry.getValue().serialize(jsonGenerator, sourcePathResolverAdapter);
  }
  jsonGenerator.writeEndObject();
}
 
Example 16
Source File: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueCharArray serialization
 */
protected String serializeValueCharArray(Character[] value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.length;
      Character item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("valueCharArray");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value[i];
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 17
Source File: ImpExtWriter.java    From openrtb-doubleclick with Apache License 2.0 5 votes vote down vote up
private void writeBuyerGeneratedRequestDataFields(
    BuyerGeneratedRequestData req, JsonGenerator gen) throws IOException {
  switch (req.getSourceCase()) {
    case SOURCE_APP:
      gen.writeFieldName("source_app");
      writeSourceApp(req.getSourceApp(), gen);
      break;
    default:
  }
  if (req.hasData()) {
    gen.writeStringField("data", req.getData());
  }
}
 
Example 18
Source File: LongBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(LinkedList<Long> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.size();
      Long item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 19
Source File: JsonPropertyWriter.java    From odata with Apache License 2.0 4 votes vote down vote up
@Override
protected ChunkedActionRenderResult getComplexPropertyChunked(
        Object data, StructuredType type, ChunkedStreamAction action, ChunkedActionRenderResult previousResult)
        throws ODataException {
    try {
        JsonGenerator generator = previousResult.getWriter() == null ?
                JSON_FACTORY.createGenerator(previousResult.getOutputStream(),
                        JsonEncoding.UTF8).setCodec(new JsonCodecMapper()) :
                (JsonGenerator) previousResult.getWriter();
        switch (action) {
            case START_DOCUMENT:
                generator.writeStringField(CONTEXT, getContextURL(getODataUri(), getEntityDataModel()));
                generator.writeFieldName("value");
                if (isCollection(data)) {
                    generator.writeStartArray();
                }

                return new ChunkedActionRenderResult(previousResult.getOutputStream(), generator);
            case BODY_DOCUMENT:
                if (isCollection(data)) {
                    for (Object obj : (List) data) {
                        writeAllProperties(obj, type);
                    }
                } else {
                    writeAllProperties(data, type);
                }

                return new ChunkedActionRenderResult(previousResult.getOutputStream(), generator);
            case END_DOCUMENT:
                if (isCollection(data)) {
                    generator.writeEndArray();
                }
                generator.writeEndObject();

                return new ChunkedActionRenderResult(previousResult.getOutputStream(), generator);
            default:
                throw new ODataRenderException(format(
                        "Unable to render complex type value because of wrong ChunkedStreamAction: {0}",
                        action));
        }
    } catch (ODataException | IOException e) {
        throw new ODataRenderException("Unable to marshall complex");
    }
}
 
Example 20
Source File: ExportAdmins.java    From usergrid with Apache License 2.0 3 votes vote down vote up
private void saveOrganizations( JsonGenerator jg, AdminUserWriteTask task ) throws Exception {

            final BiMap<UUID, String> orgs = task.orgNamesByUuid;

            jg.writeFieldName( "organizations" );

            jg.writeStartArray();

            for (UUID uuid : orgs.keySet()) {

                jg.writeStartObject();

                jg.writeFieldName( "uuid" );
                jg.writeObject( uuid );

                jg.writeFieldName( "name" );
                jg.writeObject( orgs.get( uuid ) );

                jg.writeEndObject();

                synchronized (orgsWritten) {
                    orgsWritten.add( uuid );
                }
            }

            jg.writeEndArray();
        }