Beispiel #1
0
 private TwitterEntry read(JsonParser jp) throws IOException {
   // First: verify that we got "Json Object":
   if (jp.nextToken() != JsonToken.START_OBJECT) {
     throw new IOException("Expected data to start with an Object");
   }
   TwitterEntry result = new TwitterEntry();
   // Iterate over object fields:
   while (jp.nextToken() != JsonToken.END_OBJECT) {
     String fieldName = jp.getCurrentName();
     // Let's move to value
     jp.nextToken();
     if (fieldName.equals("id")) {
       result.setId(jp.getLongValue());
     } else if (fieldName.equals("text")) {
       result.setText(jp.getText());
     } else if (fieldName.equals("fromUserId")) {
       result.setFromUserId(jp.getIntValue());
     } else if (fieldName.equals("toUserId")) {
       result.setToUserId(jp.getIntValue());
     } else if (fieldName.equals("languageCode")) {
       result.setLanguageCode(jp.getText());
     } else {
       // ignore, or signal error
       throw new IOException("Unrecognized field '" + fieldName + "'");
     }
   }
   jp.close(); // important to close both parser and underlying File reader
   return result;
 }
 public final void copyCurrentEvent(JsonParser jp) throws IOException, JsonProcessingException {
   switch (jp.getCurrentToken()) {
     case START_OBJECT:
       writeStartObject();
       break;
     case END_OBJECT:
       writeEndObject();
       break;
     case START_ARRAY:
       writeStartArray();
       break;
     case END_ARRAY:
       writeEndArray();
       break;
     case FIELD_NAME:
       writeFieldName(jp.getCurrentName());
       break;
     case VALUE_STRING:
       if (jp.hasTextCharacters()) {
         writeString(jp.getTextCharacters(), jp.getTextOffset(), jp.getTextLength());
       } else {
         writeString(jp.getText());
       }
       break;
     case VALUE_NUMBER_INT:
       switch (jp.getNumberType()) {
         case INT:
           writeNumber(jp.getIntValue());
           break;
         case BIG_INTEGER:
           writeNumber(jp.getBigIntegerValue());
           break;
         default:
           writeNumber(jp.getLongValue());
       }
       break;
     case VALUE_NUMBER_FLOAT:
       switch (jp.getNumberType()) {
         case BIG_DECIMAL:
           writeNumber(jp.getDecimalValue());
           break;
         case FLOAT:
           writeNumber(jp.getFloatValue());
           break;
         default:
           writeNumber(jp.getDoubleValue());
       }
       break;
     case VALUE_TRUE:
       writeBoolean(true);
       break;
     case VALUE_FALSE:
       writeBoolean(false);
       break;
     case VALUE_NULL:
       writeNull();
       break;
     case VALUE_EMBEDDED_OBJECT:
       writeObject(jp.getEmbeddedObject());
       break;
     default:
       _cantHappen();
   }
 }