public FeatureStructure deserialize()

in src/main/java/org/apache/uima/json/jsoncas2/ser/FeatureStructureDeserializer.java [109:245]


  public FeatureStructure deserialize(JsonParser aParser, DeserializationContext aCtxt)
          throws IOException, JsonProcessingException {

    CAS cas = getCas(aCtxt);

    if (aParser.currentToken() != START_OBJECT) {
      throw new JsonParseException(aParser, "Expected feature structure to start with "
              + START_OBJECT + " but found " + aParser.currentToken() + " instead");
    }

    int fsId = MIN_VALUE;
    // Handle case where feature structures section is represented as a map instead of an array
    if (aParser.getCurrentName() != null) {
      fsId = Integer.parseInt(aParser.getCurrentName());
    }

    FeatureStructure fs = null;
    aParser.nextValue();
    while (aParser.currentToken() != END_OBJECT) {
      String fieldName = aParser.currentName();

      // log.trace("Deserializing {}: {}", fieldName, aParser.getText());

      if (fieldName.startsWith(RESERVED_FIELD_PREFIX)) {
        switch (fieldName) {
          case ID_FIELD:
            // Handle case where feature structures section is represented as an array
            fsId = aParser.getIntValue();
            break;
          case TYPE_FIELD:
            if (fsId == MIN_VALUE) {
              throw new JsonParseException(aParser, TYPE_FIELD + " must come after " + ID_FIELD);
            }
            String typeName = aParser.getValueAsString();

            switch (typeName) {
              case TYPE_NAME_BOOLEAN_ARRAY:
                fs = deserializeBooleanArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_BYTE_ARRAY:
                fs = deserializeByteArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_DOUBLE_ARRAY:
                fs = deserializeDoubleArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_FLOAT_ARRAY:
                fs = deserializeFloatArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_INTEGER_ARRAY:
                fs = deserializeIntegerArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_LONG_ARRAY:
                fs = deserializeLongArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_SHORT_ARRAY:
                fs = deserializeShortArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_STRING_ARRAY:
                fs = deserializeStringArray(aParser, cas);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case CAS.TYPE_NAME_FS_ARRAY:
                fs = deserializeFsArray(aParser, cas, aCtxt);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              case TYPE_NAME_SOFA:
                fs = createSofaFS(cas, aParser, aCtxt);
                FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);
                continue;
              default:
                fs = createFS(aParser, aCtxt, fsId, cas);
                break;
            }
            break;
          // case FLAGS_FIELD:
          // // FIXME: We probably don't need the flags field at all.
          // aParser.nextToken();
          // aParser.skipChildren();
          // aParser.nextToken();
          // break;
        }

        aParser.nextValue();
        continue;
      }

      if (fs == null || fsId == MIN_VALUE) {
        throw new JsonParseException(aParser,
                "Features must come after " + ID_FIELD + "" + TYPE_FIELD);
      }

      FieldType fieldType = FieldType.REGULAR;
      if (fieldName.startsWith(REF_FEATURE_PREFIX)) {
        fieldName = fieldName.substring(REF_FEATURE_PREFIX.length());
        fieldType = FieldType.REFERENCE;
      } else if (fieldName.startsWith(NUMERIC_FEATURE_PREFIX)) {
        fieldName = fieldName.substring(NUMERIC_FEATURE_PREFIX.length());
        fieldType = FieldType.NUMBER;
      } else if (fieldName.startsWith(ANCHOR_FEATURE_PREFIX)) {
        fieldName = fieldName.substring(ANCHOR_FEATURE_PREFIX.length());
        fieldType = FieldType.ANCHOR;
      }

      if (CAS.FEATURE_FULL_NAME_SOFA
              .equals(fs.getType().getFeatureByBaseName(fieldName).getName())) {
        // Ignore the SofA feature of AnnotationBase-derived types - this feature cannot be set
        // manually - this happens (hopefully) when adding the AnnotationBase FS to the indexes of
        // the particular SofA.
        aParser.nextValue();
        continue;
      }

      if (fieldType == FieldType.REFERENCE) {
        deserializeFsReference(aParser, aCtxt, fs, fieldName);
        aParser.nextValue();
        continue;
      }

      deserializePrimitive(aParser, aCtxt, fs, fieldName, fieldType);
      aParser.nextValue();
    }

    // Special handling of the document annotation
    handleDocumentAnnotation(aCtxt, cas, fs);

    // Register the loaded FS
    FeatureStructureToIdIndex.get(aCtxt).put(fsId, fs);

    return fs;
  }