Example usage for com.fasterxml.jackson.core JsonGenerator writeObject

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeObject

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeObject.

Prototype

public abstract void writeObject(Object pojo) throws IOException, JsonProcessingException;

Source Link

Document

Method for writing given Java object (POJO) as Json.

Usage

From source file:org.n52.io.geojson.GeoJSONGeometrySerializer.java

@Override
public void serialize(Geometry geometry, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    try {/* w w w .j a v  a 2s. c  o m*/
        final GeoJSONEncoder enc = new GeoJSONEncoder();
        gen.writeObject(enc.encodeGeometry(geometry));
    } catch (GeoJSONException e) {
        LOGGER.error("could not properly encode geometry.", e);
    }

}

From source file:com.jyzn.wifi.validate.other.JacksonTest.java

@Test
public void writeMapJSON() {

    ObjectMapper om = new ObjectMapper();
    try {//from   w  w  w  . j  a  va  2 s . c  om

        JsonGenerator jg = om.getFactory().createGenerator(System.out, JsonEncoding.UTF8);

        ImmutableMap<String, String> map = ImmutableMap.of("status", "sucess", "validateCode", "123456");

        jg.writeObject(map);
        //?om.writeValue/writeValueJsonGenerator.writeObject?
        om.writeValue(System.out, map);
        // om.writeValueAsString(map);

    } catch (IOException e) {
    }
}

From source file:org.example.testcases.BasicTypeArraysSerializer.java

private void writeArray_char(JsonGenerator jg, char[] array) throws IOException {
    jg.writeStartArray();//from w  ww . j  a v a2 s  .  co m
    for (char val : array) {
        jg.writeObject(val);
    }
    jg.writeEndArray();
}

From source file:org.example.testcases.BasicTypeArraysSerializer.java

private void writeArray_String(JsonGenerator jg, String[] array) throws IOException {
    jg.writeStartArray();//from   w  w  w  .jav a  2  s .  c  o  m
    for (String val : array) {
        jg.writeObject(val);
    }
    jg.writeEndArray();
}

From source file:com.github.marcosalis.kraken.utils.json.ListToArraySerializer.java

@Override
public void serialize(List<M> value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    if (value != null) {
        jgen.writeStartArray();/*from   w  ww.j  av  a  2  s  .co  m*/
        for (M model : value) {
            jgen.writeObject(model);
        }
        jgen.writeEndArray();

    } else { // write empty array
        jgen.writeStartArray();
        jgen.writeEndArray();
    }
}

From source file:bz.tsung.jsonapi4j.serialization.DataSerializer.java

@Override
public void serialize(Data<Resource> data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
        throws IOException {
    Collection<Resource> list = data.get();
    if (data.isToOne()) {
        if (list == null || list.isEmpty()) {
            jsonGenerator.writeObject(null);
            return;
        }// w  w w .j  a v a2 s  . com
        jsonGenerator.writeObject(list.iterator().next());
        return;
    }
    jsonGenerator.writeObject((list == null) ? Collections.emptyList() : list);
}

From source file:bz.tsung.jsonapi4j.JsonApiSerializer.java

@Override
public void serialize(T object, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
    if (object instanceof Set) {
        jsonGenerator.writeStartArray();
        for (Object value : (Set) object) {
            jsonGenerator.writeObject(value);
        }//from   w ww  .  j a  v  a 2  s .c o m
        jsonGenerator.writeEndArray();
    } else {
        jsonGenerator.writeObject(object);
    }
}

From source file:eu.trentorise.opendata.commons.jackson.TodCommonsModule.java

/**
 * Creates the module and registers all the needed serializers and
 * deserializers for Tod Commons objects
 *///from  www.  j  ava 2s . c  o  m
public TodCommonsModule() {
    super("tod-commons-jackson", readJacksonVersion(TodCommonsModule.class));

    addSerializer(Dict.class, new StdSerializer<Dict>(Dict.class) {
        @Override
        public void serialize(Dict value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeObject(value.asMultimap());
        }
    });

    addDeserializer(Dict.class, new StdDeserializer<Dict>(Dict.class) {

        @Override
        public Dict deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            TypeReference ref = new TypeReference<ImmutableListMultimap<Locale, String>>() {
            };
            return Dict.of((ImmutableListMultimap) jp.readValueAs(ref));
        }
    });

    addDeserializer(Locale.class, new LocaleDeserializer());

    setMixInAnnotation(LocalizedString.class, JacksonLocalizedString.class);

    setMixInAnnotation(Ref.class, JacksonRef.class);

}

From source file:models.ReferenceSerializer.java

protected void writeSimplifiedGroup(Group group, JsonGenerator jgen) throws IOException {
    jgen.writeStartObject();//from  w  w  w  .  ja va 2 s .com
    jgen.writeStringField("id", group.getId());
    jgen.writeStringField("label", group.getLabel());
    jgen.writeFieldName("type");
    jgen.writeObject(group.getType());
    jgen.writeEndObject();
}