protected Object _unwrapAndDeserialize(
     JsonParser jp, DeserializationContext ctxt, JavaType rootType, JsonDeserializer<Object> deser)
     throws IOException, JsonParseException, JsonMappingException {
   SerializedString rootName = _provider.findExpectedRootName(ctxt.getConfig(), rootType);
   if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
     throw JsonMappingException.from(
         jp,
         "Current token not START_OBJECT (needed to unwrap root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   if (jp.nextToken() != JsonToken.FIELD_NAME) {
     throw JsonMappingException.from(
         jp,
         "Current token not FIELD_NAME (to contain expected root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   String actualName = jp.getCurrentName();
   if (!rootName.getValue().equals(actualName)) {
     throw JsonMappingException.from(
         jp,
         "Root name '"
             + actualName
             + "' does not match expected ('"
             + rootName
             + "') for type "
             + rootType);
   }
   // ok, then move to value itself....
   jp.nextToken();
   Object result;
   if (_valueToUpdate == null) {
     result = deser.deserialize(jp, ctxt);
   } else {
     deser.deserialize(jp, ctxt, _valueToUpdate);
     result = _valueToUpdate;
   }
   // and last, verify that we now get matching END_OBJECT
   if (jp.nextToken() != JsonToken.END_OBJECT) {
     throw JsonMappingException.from(
         jp,
         "Current token not END_OBJECT (to match wrapper object with root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   return result;
 }
Example #2
0
  public final void copyCurrentStructure(JsonParser jp)
      throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();

    // Let's handle field-name separately first
    if (t == JsonToken.FIELD_NAME) {
      writeFieldName(jp.getCurrentName());
      t = jp.nextToken();
      // fall-through to copy the associated value
    }

    switch (t) {
      case START_ARRAY:
        writeStartArray();
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          copyCurrentStructure(jp);
        }
        writeEndArray();
        break;
      case START_OBJECT:
        writeStartObject();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
          copyCurrentStructure(jp);
        }
        writeEndObject();
        break;
      default: // others are simple:
        copyCurrentEvent(jp);
    }
  }
 private void reportIllegal(JsonParser parser, JsonToken expToken) throws IOException {
   JsonToken curr = parser.getCurrentToken();
   String msg = "Expected token " + expToken + "; got " + curr;
   if (curr == JsonToken.FIELD_NAME) {
     msg += " (current field name '" + parser.getCurrentName() + "')";
   }
   msg += ", location: " + parser.getTokenLocation();
   throw new IllegalStateException(msg);
 }
 protected static JsonToken _initForReading(JsonParser jp)
     throws IOException, JsonParseException, JsonMappingException {
   /* First: must point to a token; if not pointing to one, advance.
    * This occurs before first read from JsonParser, as well as
    * after clearing of current token.
    */
   JsonToken t = jp.getCurrentToken();
   if (t == null) { // and then we must get something...
     t = jp.nextToken();
     if (t == null) { // [JACKSON-99] Should throw EOFException?
       throw new EOFException("No content to map to Object due to end of input");
     }
   }
   return t;
 }
  protected MediaContent readMediaContent(JsonParser parser) throws IOException {
    MediaContent mc = new MediaContent();
    if (parser.nextToken() != JsonToken.START_OBJECT) {
      reportIllegal(parser, JsonToken.START_OBJECT);
    }
    // first fast version when field-order is as expected
    if (parser.nextFieldName(FIELD_MEDIA)) {
      mc.media = readMedia(parser);
      if (parser.nextFieldName(FIELD_IMAGES)) {
        mc.images = readImages(parser);
        parser.nextToken();
        verifyCurrent(parser, JsonToken.END_OBJECT);
        return mc;
      }
    }

    // and fallback if order was changed
    for (; parser.getCurrentToken() == JsonToken.FIELD_NAME; parser.nextToken()) {
      String field = parser.getCurrentName();
      Integer I = fullFieldToIndex.get(field);
      if (I != null) {
        switch (I) {
          case FIELD_IX_MEDIA:
            mc.media = readMedia(parser);
            continue;
          case FIELD_IX_IMAGES:
            mc.images = readImages(parser);
            continue;
        }
      }
      throw new IllegalStateException("Unexpected field '" + field + "'");
    }
    verifyCurrent(parser, JsonToken.END_OBJECT);

    if (mc.media == null) throw new IllegalStateException("Missing field: " + FIELD_MEDIA);
    if (mc.images == null) mc.images = new ArrayList<Image>();

    return mc;
  }
 private final void verifyCurrent(JsonParser parser, JsonToken expToken) throws IOException {
   if (parser.getCurrentToken() != expToken) {
     reportIllegal(parser, expToken);
   }
 }
  private Image readImage(JsonParser parser) throws IOException {
    boolean haveWidth = false;
    boolean haveHeight = false;
    Image image = new Image();
    if (parser.nextFieldName(FIELD_URI)) {
      image.uri = parser.nextTextValue();
      if (parser.nextFieldName(FIELD_TITLE)) {
        image.title = parser.nextTextValue();
        if (parser.nextFieldName(FIELD_WIDTH)) {
          image.width = parser.nextIntValue(-1);
          haveWidth = true;
          if (parser.nextFieldName(FIELD_HEIGHT)) {
            image.height = parser.nextIntValue(-1);
            haveHeight = true;
            if (parser.nextFieldName(FIELD_SIZE)) {
              image.size = Image.Size.valueOf(parser.nextTextValue());
              parser.nextToken();
              verifyCurrent(parser, JsonToken.END_OBJECT);
              return image;
            }
          }
        }
      }
    }

    for (; parser.getCurrentToken() == JsonToken.FIELD_NAME; parser.nextToken()) {
      String field = parser.getCurrentName();
      // read value token (or START_ARRAY)
      parser.nextToken();
      Integer I = fullFieldToIndex.get(field);
      if (I != null) {
        switch (I) {
          case FIELD_IX_URI:
            image.uri = parser.getText();
            continue;
          case FIELD_IX_TITLE:
            image.title = parser.getText();
            continue;
          case FIELD_IX_WIDTH:
            image.width = parser.getIntValue();
            haveWidth = true;
            continue;
          case FIELD_IX_HEIGHT:
            image.height = parser.getIntValue();
            haveHeight = true;
            continue;
          case FIELD_IX_SIZE:
            image.size = Image.Size.valueOf(parser.getText());
            continue;
        }
      }
      throw new IllegalStateException("Unexpected field '" + field + "'");
    }

    if (image.uri == null) throw new IllegalStateException("Missing field: " + FIELD_URI);
    if (!haveWidth) throw new IllegalStateException("Missing field: " + FIELD_WIDTH);
    if (!haveHeight) throw new IllegalStateException("Missing field: " + FIELD_HEIGHT);
    if (image.size == null) throw new IllegalStateException("Missing field: " + FIELD_SIZE);

    verifyCurrent(parser, JsonToken.END_OBJECT);

    return image;
  }
  private Media readMedia(JsonParser parser) throws IOException {
    if (parser.nextToken() != JsonToken.START_OBJECT) {
      reportIllegal(parser, JsonToken.START_OBJECT);
    }
    Media media = new Media();
    boolean haveWidth = false;
    boolean haveHeight = false;
    boolean haveDuration = false;
    boolean haveSize = false;

    // As with above, first fast path
    if (parser.nextFieldName(FIELD_PLAYER)) {
      media.player = Media.Player.find(parser.nextTextValue());
      if (parser.nextFieldName(FIELD_URI)) {
        media.uri = parser.nextTextValue();
        if (parser.nextFieldName(FIELD_TITLE)) {
          media.title = parser.nextTextValue();
          if (parser.nextFieldName(FIELD_WIDTH)) {
            haveWidth = true;
            media.width = parser.nextIntValue(-1);
            if (parser.nextFieldName(FIELD_HEIGHT)) {
              haveHeight = true;
              media.height = parser.nextIntValue(-1);
              if (parser.nextFieldName(FIELD_FORMAT)) {
                media.format = parser.nextTextValue();
                if (parser.nextFieldName(FIELD_DURATION)) {
                  haveDuration = true;
                  media.duration = parser.nextLongValue(-1L);
                  if (parser.nextFieldName(FIELD_SIZE)) {
                    haveSize = true;
                    media.size = parser.nextLongValue(-1L);
                    if (parser.nextFieldName(FIELD_BITRATE)) {
                      media.bitrate = parser.nextIntValue(-1);
                      media.hasBitrate = true;
                      if (parser.nextFieldName(FIELD_COPYRIGHT)) {
                        media.copyright = parser.nextTextValue();
                        if (parser.nextFieldName(FIELD_PERSONS)) {
                          media.persons = readPersons(parser);
                          parser.nextToken();
                          verifyCurrent(parser, JsonToken.END_OBJECT);
                          return media;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    // and if something reorder or missing, general loop:

    for (; parser.getCurrentToken() == JsonToken.FIELD_NAME; parser.nextToken()) {
      String field = parser.getCurrentName();
      Integer I = fullFieldToIndex.get(field);
      if (I != null) {
        switch (I) {
          case FIELD_IX_PLAYER:
            media.player = Media.Player.find(parser.nextTextValue());
            continue;
          case FIELD_IX_URI:
            media.uri = parser.nextTextValue();
            continue;
          case FIELD_IX_TITLE:
            media.title = parser.nextTextValue();
            continue;
          case FIELD_IX_WIDTH:
            media.width = parser.nextIntValue(-1);
            haveWidth = true;
            continue;
          case FIELD_IX_HEIGHT:
            media.height = parser.nextIntValue(-1);
            haveHeight = true;
            continue;
          case FIELD_IX_FORMAT:
            media.format = parser.nextTextValue();
            continue;
          case FIELD_IX_DURATION:
            media.duration = parser.nextLongValue(-1L);
            haveDuration = true;
            continue;
          case FIELD_IX_SIZE:
            media.size = parser.nextLongValue(-1L);
            haveSize = true;
            continue;
          case FIELD_IX_BITRATE:
            media.bitrate = parser.nextIntValue(-1);
            media.hasBitrate = true;
            continue;
          case FIELD_IX_PERSONS:
            media.persons = readPersons(parser);
            continue;
          case FIELD_IX_COPYRIGHT:
            media.copyright = parser.nextTextValue();
            continue;
        }
      }
      throw new IllegalStateException("Unexpected field '" + field + "'");
    }
    verifyCurrent(parser, JsonToken.END_OBJECT);

    if (media.uri == null) throw new IllegalStateException("Missing field: " + FIELD_URI);
    if (!haveWidth) throw new IllegalStateException("Missing field: " + FIELD_WIDTH);
    if (!haveHeight) throw new IllegalStateException("Missing field: " + FIELD_HEIGHT);
    if (media.format == null) throw new IllegalStateException("Missing field: " + FIELD_FORMAT);
    if (!haveDuration) throw new IllegalStateException("Missing field: " + FIELD_DURATION);
    if (!haveSize) throw new IllegalStateException("Missing field: " + FIELD_SIZE);
    if (media.persons == null) media.persons = new ArrayList<String>();
    if (media.player == null) throw new IllegalStateException("Missing field: " + FIELD_PLAYER);

    return media;
  }
Example #9
0
 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();
   }
 }