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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#flush() . 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: MCRJWTUtil.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public static Response getJWTRenewSuccessResponse(String jwt) throws IOException {
    try (StringWriter sw = new StringWriter()) {
        JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(sw);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeBooleanField("executed", true);
        jsonGenerator.writeStringField("access_token", jwt);
        jsonGenerator.writeStringField("token_type", "Bearer");
        jsonGenerator.writeEndObject();
        jsonGenerator.flush();
        jsonGenerator.close();
        return Response.status(Response.Status.OK)
            .header("Authorization", "Bearer " + jwt)
            .entity(sw.toString())
            .build();
    }
}
 
Example 2
Source File: CharBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(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("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 3
Source File: DbxRawClientV2.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
private static <T> String headerSafeJson(StoneSerializer<T> serializer, T value) {
    StringWriter out = new StringWriter();
    try {
        JsonGenerator g = JSON.createGenerator(out);
        // Escape 0x7F, because it's not allowed in an HTTP header.
        // Escape all non-ASCII because the new HTTP spec recommends against non-ASCII in headers.
        g.setHighestNonEscapedChar(0x7E);
        serializer.serialize(value, g);
        g.flush();
    } catch (IOException ex) {
        throw LangUtil.mkAssert("Impossible", ex);
    }
    return out.toString();
}
 
Example 4
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueTimeList serialization
 */
public static byte[] serializeValueTimeList(List<Time> 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();
      Time 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.writeString(SQLTimeUtils.write(item));
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 5
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueBeanSet serialization
 */
public static byte[] serializeValueBeanSet(LinkedHashSet<Bean> 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 (Bean item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          beanBindMap.serializeOnJackson(item, jacksonSerializer);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 6
Source File: OpenRtbJsonWriter.java    From openrtb with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes a {@link BidResponse} to JSON, with a provided {@link JsonGenerator}
 * which allows several choices of output and encoding.
 */
public final void writeBidResponse(BidResponse resp, JsonGenerator gen) throws IOException {
  gen.writeStartObject();
  writeBidResponseFields(resp, gen);
  writeExtensions(resp, gen);
  gen.writeEndObject();
  gen.flush();
}
 
Example 7
Source File: ImpExtWriter.java    From openrtb-doubleclick with Apache License 2.0 5 votes vote down vote up
private void writeExcludedCreative(ExcludedCreative exCreat, JsonGenerator gen)
    throws IOException {
  gen.writeStartObject();
  writeExcludedCreativeFields(exCreat, gen);
  gen.writeEndObject();
  gen.flush();
}
 
Example 8
Source File: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueBigDecimalSet serialization
 */
protected String serializeValueBigDecimalSet(HashSet<BigDecimal> 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("valueBigDecimalSet");
      jacksonSerializer.writeStartArray();
      for (BigDecimal item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeString(BigDecimalUtils.write(item));
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 9
Source File: BeanBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(BeanInner[] 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;
      BeanInner 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 {
          beanInnerBindMap.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: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueCharList serialization
 */
protected String serializeValueCharList(LinkedList<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.size();
      Character item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("valueCharList");
      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.toString();
  } 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 valueDoubleSet serialization
 */
protected String 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("valueDoubleSet");
      jacksonSerializer.writeStartArray();
      for (Double 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: DoubleDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param serializer2 serialization
 */
private byte[] serializer2(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;
    int fieldCount=0;
    jacksonSerializer.writeStartObject();
    if (value!=null)  {
      int n=value.length;
      Double 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 13
Source File: BindBean64SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueLongTypeArray serialization
 */
protected String serializeValueLongTypeArray(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("valueLongTypeArray");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value[i];
        jacksonSerializer.writeNumber(item);
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 14
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Outputs a Smile representation of the Protocol Message supplied into the parameter output.
 * (This representation is the new version of the classic "ProtocolPrinter" output from the
 * original Protocol Buffer system)
 */
public void print(Message message, JsonGenerator generator) throws IOException {
	generator.writeStartObject();
	printMessage(message, generator);
    generator.writeEndObject();
    generator.flush();
}
 
Example 15
Source File: BindBean2SharedPreferences.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 16
Source File: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueTimeList serialization
 */
protected String serializeValueTimeList(List<Time> 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();
      Time item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("valueTimeList");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeString(SQLTimeUtils.write(item));
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 17
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueIntegerSet serialization
 */
public static byte[] serializeValueIntegerSet(LinkedHashSet<Integer> 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 (Integer 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 18
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueCharList serialization
 */
public static byte[] serializeValueCharList(LinkedList<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.size();
      Character 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: Configuration.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 *  Writes out all properties and their attributes (final and resource) to
 *  the given {@link Writer}, the format of the output would be,
 *
 *  <pre>
 *  { "properties" :
 *      [ { key : "key1",
 *          value : "value1",
 *          isFinal : "key1.isFinal",
 *          resource : "key1.resource" },
 *        { key : "key2",
 *          value : "value2",
 *          isFinal : "ke2.isFinal",
 *          resource : "key2.resource" }
 *       ]
 *   }
 *  </pre>
 *
 *  It does not output the properties of the configuration object which
 *  is loaded from an input stream.
 *  <p>
 *
 * @param config the configuration
 * @param out the Writer to write to
 * @throws IOException
 */
public static void dumpConfiguration(Configuration config,
                                     Writer out) throws IOException {
	JsonFactory dumpFactory = new JsonFactory();
	JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
	dumpGenerator.writeStartObject();
	dumpGenerator.writeFieldName("properties");
	dumpGenerator.writeStartArray();
	dumpGenerator.flush();
	ConfigRedactor redactor = new ConfigRedactor(config);
	synchronized (config) {
		for (Map.Entry<Object,Object> item: config.getProps().entrySet()) {
			appendJSONProperty(dumpGenerator, config, item.getKey().toString(),
					redactor);
		}
	}
	dumpGenerator.writeEndArray();
	dumpGenerator.writeEndObject();
	dumpGenerator.flush();
}
 
Example 20
Source File: Configuration.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 *  Writes properties and their attributes (final and resource)
 *  to the given {@link Writer}.
 *
 *  <li>
 *  When propertyName is not empty, and the property exists
 *  in the configuration, the format of the output would be,
 *  <pre>
 *  {
 *    "property": {
 *      "key" : "key1",
 *      "value" : "value1",
 *      "isFinal" : "key1.isFinal",
 *      "resource" : "key1.resource"
 *    }
 *  }
 *  </pre>
 *  </li>
 *
 *  <li>
 *  When propertyName is null or empty, it behaves same as
 *  {@link #dumpConfiguration(Configuration, Writer)}, the
 *  output would be,
 *  <pre>
 *  { "properties" :
 *      [ { key : "key1",
 *          value : "value1",
 *          isFinal : "key1.isFinal",
 *          resource : "key1.resource" },
 *        { key : "key2",
 *          value : "value2",
 *          isFinal : "ke2.isFinal",
 *          resource : "key2.resource" }
 *       ]
 *   }
 *  </pre>
 *  </li>
 *
 *  <li>
 *  When propertyName is not empty, and the property is not
 *  found in the configuration, this method will throw an
 *  {@link IllegalArgumentException}.
 *  </li>
 *  <p>
 * @param config the configuration
 * @param propertyName property name
 * @param out the Writer to write to
 * @throws IOException
 * @throws IllegalArgumentException when property name is not
 *   empty and the property is not found in configuration
 **/
public static void dumpConfiguration(Configuration config,
                                     String propertyName, Writer out) throws IOException {
	if(Strings.isNullOrEmpty(propertyName)) {
		dumpConfiguration(config, out);
	} else if (Strings.isNullOrEmpty(config.get(propertyName))) {
		throw new IllegalArgumentException("Property " +
				propertyName + " not found");
	} else {
		JsonFactory dumpFactory = new JsonFactory();
		JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
		dumpGenerator.writeStartObject();
		dumpGenerator.writeFieldName("property");
		appendJSONProperty(dumpGenerator, config, propertyName,
				new ConfigRedactor(config));
		dumpGenerator.writeEndObject();
		dumpGenerator.flush();
	}
}