@Override public void serialize( LeafDataNode leafDataNode, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { try { switch (leafDataNode.getLeafType()) { case BIG_DECIMAL: generator.writeNumber(leafDataNode.getBigDecimal(null)); break; case BIG_INTEGER: generator.writeNumber(leafDataNode.getBigInteger(null)); break; case BINARY: generator.writeBinary(leafDataNode.getBytes(null)); break; case BOOLEAN: generator.writeBoolean(leafDataNode.getBoolean(false)); break; case DOUBLE: generator.writeNumber(leafDataNode.getDouble(0.0)); break; case LONG: generator.writeNumber(leafDataNode.getLong(0)); break; case STRING: generator.writeString(leafDataNode.getString(null)); break; default: assert false; break; } } catch (BigDBException e) { assert false; } }
private void writeIterableToBooleanArray(Iterable<?> elements, JsonGenerator jg) throws IOException { @SuppressWarnings("unchecked") Iterator<Boolean> it = (Iterator<Boolean>) elements.iterator(); while (it.hasNext()) { jg.writeBoolean(it.next()); } }
@Override public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { if ("true".equals(value) || "false".equals(value)) { // 判断如果是true或false的boolean值采用boolean jgen.writeBoolean(Boolean.parseBoolean(value)); } else { // 否则用字符串 jgen.writeString(value != null ? value : ""); } }
@Override public void serialize( final PrimitiveNode value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException, JsonProcessingException { if (value.value instanceof Number) { gen.writeNumber(value.asText()); } else if (value.value instanceof Boolean) { gen.writeBoolean((boolean) value.value); } else { gen.writeString(value.asText()); } }
private void writeScalarValue(JsonGenerator jgen, Object possibleValue, Class<?> valueType) throws IOException { if (Number.class.isAssignableFrom(valueType)) { jgen.writeNumber(possibleValue.toString()); } else if (Boolean.class.isAssignableFrom(valueType)) { jgen.writeBoolean((Boolean) possibleValue); } else if (Enum.class.isAssignableFrom(valueType)) { jgen.writeString(((Enum) possibleValue).name()); } else { jgen.writeString(possibleValue.toString()); } }
public void testSimpleArrayWrite() throws Exception { StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createGenerator(sw); gen.writeStartArray(); gen.writeNumber(13); gen.writeBoolean(true); gen.writeString("foobar"); gen.writeEndArray(); gen.close(); String docStr = sw.toString(); JsonParser jp = createParserUsingReader(docStr); assertEquals(JsonToken.START_ARRAY, jp.nextToken()); assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); assertEquals(13, jp.getIntValue()); assertEquals(JsonToken.VALUE_TRUE, jp.nextToken()); assertEquals(JsonToken.VALUE_STRING, jp.nextToken()); assertEquals("foobar", jp.getText()); assertEquals(JsonToken.END_ARRAY, jp.nextToken()); assertEquals(null, jp.nextToken()); jp.close(); }
@Override public void serialize( final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString(BYTE_ARRAY_PREFIX + Base64.encode((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); }
@Override public void writeFieldValue(JsonGenerator jsonGenerator, Object value) throws IOException { validateFieldValueType(value, Boolean.class); jsonGenerator.writeBoolean((Boolean) value); }
/** * Marshal. * * @param generator the generator * @param value the value * @throws IOException Signals that an I/O exception has occurred. */ public static void marshal(JsonGenerator generator, boolean value) throws IOException { generator.writeBoolean(value); }