private void writeScalarPropertyValue(JsonGenerator jg, Type type, Object value)
     throws IOException {
   if (value == null) {
     jg.writeNull();
   } else if (type instanceof BooleanType) {
     jg.writeBoolean((Boolean) value);
   } else if (type instanceof LongType) {
     jg.writeNumber((Long) value);
   } else if (type instanceof DoubleType) {
     jg.writeNumber((Double) value);
   } else if (type instanceof IntegerType) {
     jg.writeNumber((Integer) value);
   } else if (type instanceof BinaryType) {
     jg.writeBinary((byte[]) value);
   } else {
     jg.writeString(type.encode(value));
   }
 }
  public void jacksonTest() throws Exception {
    JsonFactory factory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator generator = factory.createJsonGenerator(writer);
    generator.writeStartObject();
    generator.writeFieldName("bool");
    generator.writeBoolean(true);
    generator.writeFieldName("firstName");
    generator.writeString("john");
    generator.writeFieldName("age");
    generator.writeNumber(1);
    generator.writeFieldName("gg");
    generator.writeStartObject();
    generator.writeFieldName("firstName");
    generator.writeString("john");
    generator.writeEndObject();

    generator.writeEndObject();
    generator.close();
    String generated = writer.toString();
    System.out.print(generated);

    JsonParser parser = factory.createJsonParser(generated);
    assertTrue(parser.nextToken() == JsonToken.START_OBJECT);
    parser.nextToken();

    assertEquals("bool", parser.getCurrentName());
    assertTrue(parser.nextToken() == JsonToken.VALUE_TRUE);
    parser.nextToken();

    assertEquals("firstName", parser.getCurrentName());
    parser.nextToken();
    assertEquals("john", parser.getText());
    parser.nextToken();

    assertEquals("age", parser.getCurrentName());
    parser.nextToken();
    assertTrue(1 == parser.getIntValue());
    parser.nextToken();

    assertEquals("gg", parser.getCurrentName());
    assertTrue(parser.nextToken() == JsonToken.START_OBJECT);
    parser.nextToken();
    assertEquals("firstName", parser.getCurrentName());
    parser.nextToken();
    assertEquals("john", parser.getText());
    assertTrue(parser.nextToken() == JsonToken.END_OBJECT);

    assertTrue(parser.nextToken() == JsonToken.END_OBJECT);

    parser.close();
  }
 @Override
 public void serialize(
     final UnregisterResponse unregisterResponse,
     final JsonGenerator jgen,
     final SerializerProvider provider)
     throws IOException, JsonProcessingException {
   jgen.writeStartObject();
   jgen.writeFieldName(RegisterResponse.MESSSAGE_TYPE_FIELD);
   jgen.writeString(unregisterResponse.getMessageType().toString().toLowerCase());
   jgen.writeFieldName(RegisterResponse.CHANNEL_ID_FIELD);
   jgen.writeString(unregisterResponse.getChannelId());
   jgen.writeFieldName(RegisterResponse.STATUS_FIELD);
   jgen.writeNumber(unregisterResponse.getStatus().getCode());
   jgen.writeEndObject();
 }
 @Override
 public void serialize(
     final AckMessage ack, final JsonGenerator jgen, final SerializerProvider provider)
     throws IOException, JsonProcessingException {
   jgen.writeStartObject();
   jgen.writeFieldName(AckMessage.MESSSAGE_TYPE_FIELD);
   jgen.writeString(ack.getMessageType().toString().toLowerCase());
   jgen.writeArrayFieldStart(AckMessage.UPDATES_FIELD);
   for (Update update : ack.getUpdates()) {
     jgen.writeStartObject();
     jgen.writeFieldName("channelID");
     jgen.writeString(update.getChannelId());
     jgen.writeFieldName(AckMessage.VERSION_FIELD);
     jgen.writeNumber(update.getVersion());
     jgen.writeEndObject();
   }
   jgen.writeEndArray();
   jgen.writeEndObject();
 }
 @Override
 public void serialize(
     final NotificationMessage notification,
     final JsonGenerator jgen,
     final SerializerProvider provider)
     throws IOException, JsonProcessingException {
   jgen.writeStartObject();
   jgen.writeFieldName(NotificationMessage.MESSSAGE_TYPE_FIELD);
   jgen.writeString(notification.getMessageType().toString().toLowerCase());
   jgen.writeArrayFieldStart(NotificationMessage.UPDATES_FIELD);
   for (Update update : notification.getUpdates()) {
     jgen.writeStartObject();
     jgen.writeFieldName(RegisterMessage.CHANNEL_ID_FIELD);
     jgen.writeString(update.getChannelId());
     jgen.writeFieldName(NotificationMessage.VERSION_FIELD);
     jgen.writeNumber(update.getVersion());
     jgen.writeEndObject();
   }
   jgen.writeEndArray();
   jgen.writeEndObject();
 }
Example #6
0
 @Override
 public final void serialize(JsonGenerator jg, SerializerProvider provider)
     throws IOException, JsonProcessingException {
   jg.writeNumber(_value);
 }