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

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

Introduction

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

Prototype

public abstract JsonStreamContext getOutputContext();

Source Link

Usage

From source file:org.brutusin.json.impl.serializers.JsonNodeSerializer.java

@Override
public void serialize(JsonNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    JsonWriteContext ctx = (JsonWriteContext) gen.getOutputContext();

    int status = ctx.writeValue();
    switch (status) {
    case JsonWriteContext.STATUS_OK_AFTER_COMMA:
        gen.writeRaw(',');
        break;//from w ww  .ja v a2 s. c o m
    case JsonWriteContext.STATUS_OK_AFTER_COLON:
        gen.writeRaw(':');
        break;
    case JsonWriteContext.STATUS_OK_AFTER_SPACE:
        gen.writeRaw(' ');
        break;
    }
    ;
    if (value == null) {
        gen.writeRaw("null");
    } else {
        gen.writeRaw(value.toString());
    }
}

From source file:org.brutusin.json.impl.serializers.InputStreamSerializer.java

@Override
public void serialize(InputStream value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    JsonWriteContext ctx = (JsonWriteContext) gen.getOutputContext();
    int status = ctx.writeValue();
    switch (status) {
    case JsonWriteContext.STATUS_OK_AFTER_COMMA:
        gen.writeRaw(',');
        break;//from  w w  w  .  j a v  a 2 s.  c  o m
    case JsonWriteContext.STATUS_OK_AFTER_COLON:
        gen.writeRaw(':');
        break;
    case JsonWriteContext.STATUS_OK_AFTER_SPACE:
        gen.writeRaw(' ');
        break;
    }
    ;
    if (value == null) {
        gen.writeRaw("null");
    } else {
        SerializationContext sCtx = SerializationContext.getCurrentContext();
        if (sCtx == null) {
            sCtx = new SerializationContext();
            SerializationContext.setCurrentContext(sCtx);
        }
        gen.writeRaw("\"" + sCtx.addStream(value) + "\"");
    }
}

From source file:com.proofpoint.event.collector.FilteringMapSerializer.java

@Override
public void serialize(Map<String, ?> map, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    //have to do this before starting to write the object, otherwise proper context is lost.
    PropertyMapSelectionPolicy filter = findApplicableFilter(jgen.getOutputContext());
    jgen.writeStartObject();//ww  w.  ja  va2  s .  com
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        String name = entry.getKey();

        if (!filter.matches(name)) {
            continue;
        }

        Object value = entry.getValue();
        jgen.writeObjectField(name, value);
    }
    jgen.writeEndObject();
}

From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java

public static String dumpConfigurationSectionAsFlatJson(String section) {
    Class en = CONFIGURATION_SECTIONS.get(section);
    try {//  w  ww  .  jav  a2s  .co m
        JsonFactory jfactory = new JsonFactory();
        StringWriter sw = new StringWriter();
        String enumDescription = "";
        JsonGenerator gen = jfactory.createJsonGenerator(sw);
        gen.writeStartArray();
        EnumSet values = EnumSet.allOf(en);
        for (Object v : values) {
            String key = (String) (en.getMethod("getKey")).invoke(v);

            boolean isVisible = (Boolean) (en.getMethod("isVisible")).invoke(v);
            String valueAsString;
            if (isVisible)
                valueAsString = (String) (en.getMethod("getValueAsString")).invoke(v);
            else
                valueAsString = "--HIDDEN--";
            boolean isEditable = (Boolean) (en.getMethod("isEditable")).invoke(v);
            boolean isOverridden = (Boolean) (en.getMethod("isOverridden")).invoke(v);
            String valueDescription = (String) (en.getMethod("getValueDescription")).invoke(v);
            Class type = (Class) en.getMethod("getType").invoke(v);

            gen.writeStartObject(); //               {
            gen.writeStringField("key", key);
            gen.writeStringField("value", valueAsString);
            gen.writeStringField("description", valueDescription); //                  ,"description":"description"
            gen.writeStringField("type", type.getSimpleName()); //                  ,"type":"type"
            gen.writeBooleanField("editable", isEditable);
            gen.writeBooleanField("overridden", isOverridden);
            gen.writeEndObject(); //               }
        }
        if (gen.getOutputContext().inArray())
            gen.writeEndArray(); //            ]
        gen.close();
        return sw.toString();
    } catch (Exception e) {
        BaasBoxLogger.error("Cannot generate a json for " + en.getSimpleName()
                + " Enum. Is it an Enum that implements the IProperties interface?", e);
    }
    return "{}";
}

From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java

/***
 *
 * Returns a json representation of the Enumerator
 * The Enumerator must implements the IProperties interface
 * @param en    the Enumerator to serialize. It must implements the IProperties interface
 * @return       the representation of the Enumerator 
 *//*from   www  .  java  2  s .  c o  m*/
@SuppressWarnings("unchecked")
public static String dumpConfigurationAsJson(String section) {
    Class en = CONFIGURATION_SECTIONS.get(section);
    try {
        JsonFactory jfactory = new JsonFactory();
        StringWriter sw = new StringWriter();
        String enumDescription = "";
        JsonGenerator gen = jfactory.createJsonGenerator(sw);

        Method getEnumDescription = en.getMethod("getEnumDescription");
        if (getEnumDescription != null && getEnumDescription.getReturnType() == String.class
                && Modifier.isStatic(getEnumDescription.getModifiers()))
            enumDescription = (String) getEnumDescription.invoke(null);
        gen.writeStartObject(); //{
        gen.writeStringField("section", section); //    "configuration":"EnumName"
        gen.writeStringField("description", enumDescription); //   ,"description": "EnumDescription"
        gen.writeFieldName("sub sections"); //  ,"sections":
        gen.writeStartObject(); //      {
        String lastSection = "";
        EnumSet values = EnumSet.allOf(en);
        for (Object v : values) {
            String key = (String) (en.getMethod("getKey")).invoke(v);
            boolean isVisible = (Boolean) (en.getMethod("isVisible")).invoke(v);
            String valueAsString;
            if (isVisible)
                valueAsString = (String) (en.getMethod("getValueAsString")).invoke(v);
            else
                valueAsString = "--HIDDEN--";
            boolean isEditable = (Boolean) (en.getMethod("isEditable")).invoke(v);
            String valueDescription = (String) (en.getMethod("getValueDescription")).invoke(v);
            Class type = (Class) en.getMethod("getType").invoke(v);
            String subsection = key.substring(0, key.indexOf('.'));
            if (!lastSection.equals(subsection)) {
                if (gen.getOutputContext().inArray())
                    gen.writeEndArray();
                gen.writeFieldName(subsection); //         "sectionName":
                gen.writeStartArray(); //            [
                lastSection = subsection;
            }
            boolean isOverridden = (Boolean) (en.getMethod("isOverridden")).invoke(v);
            gen.writeStartObject(); //               {
            gen.writeStringField(key, valueAsString); //                     "key": "value"   
            gen.writeStringField("description", valueDescription); //                  ,"description":"description"
            gen.writeStringField("type", type.getSimpleName()); //                  ,"type":"type"
            gen.writeBooleanField("editable", isEditable); //                  ,"editable":"true|false"
            gen.writeBooleanField("visible", isVisible); //                  ,"visible":"true|false"
            gen.writeBooleanField("overridden", isOverridden); //                  ,"overridden":"true|false"
            gen.writeEndObject(); //               }
        }
        if (gen.getOutputContext().inArray())
            gen.writeEndArray(); //            ]
        gen.writeEndObject(); //      }
        gen.writeEndObject(); //}
        gen.close();
        return sw.toString();
    } catch (Exception e) {
        BaasBoxLogger.error("Cannot generate a json for " + en.getSimpleName()
                + " Enum. Is it an Enum that implements the IProperties interface?", e);
    }
    return "{}";
}