Java Code Examples for org.codehaus.jackson.JsonToken#FIELD_NAME

The following examples show how to use org.codehaus.jackson.JsonToken#FIELD_NAME . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private static void populateRecord(@Nonnull final List<Object> r,
        @Nonnull final JsonToken token, @Nonnull final JsonParser p,
        @Nonnull final HCatSchema s) throws IOException {
    if (token != JsonToken.FIELD_NAME) {
        throw new IOException("Field name expected");
    }
    String fieldName = p.getText();
    Integer fpos = s.getPosition(fieldName);
    if (fpos == null) {
        fpos = getPositionFromHiveInternalColumnName(fieldName);
        if (fpos == -1) {
            skipValue(p);
            return; // unknown field, we return. We'll continue from the next field onwards.
        }
        // If we get past this, then the column name did match the hive pattern for an internal
        // column name, such as _col0, etc, so it *MUST* match the schema for the appropriate column.
        // This means people can't use arbitrary column names such as _col0, and expect us to ignore it
        // if we find it.
        if (!fieldName.equalsIgnoreCase(getHiveInternalColumnName(fpos))) {
            throw new IOException("Hive internal column name (" + fieldName
                    + ") and position encoding (" + fpos + ") for the column name are at odds");
        }
        // If we reached here, then we were successful at finding an alternate internal
        // column mapping, and we're about to proceed.
    }
    HCatFieldSchema hcatFieldSchema = s.getFields().get(fpos);
    Object currField = extractCurrentField(p, hcatFieldSchema, false);
    r.set(fpos, currField);
}
 
Example 2
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private Object parseMap(JsonParser parser, MapObjectInspector oi)
        throws IOException, SerDeException {
    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        parser.nextToken();
        return null;
    }

    Map<Object, Object> ret = new LinkedHashMap<>();

    if (parser.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new SerDeException("struct expected");
    }

    if (!(oi.getMapKeyObjectInspector() instanceof PrimitiveObjectInspector)) {
        throw new SerDeException("map key must be a primitive");
    }
    PrimitiveObjectInspector keyOI = (PrimitiveObjectInspector) oi.getMapKeyObjectInspector();
    ObjectInspector valOI = oi.getMapValueObjectInspector();

    JsonToken currentToken = parser.nextToken();
    while (currentToken != null && currentToken != JsonToken.END_OBJECT) {

        if (currentToken != JsonToken.FIELD_NAME) {
            throw new SerDeException("unexpected token: " + currentToken);
        }

        Object key = parseMapKey(parser, keyOI);
        Object val = parseDispatcher(parser, valOI);
        ret.put(key, val);

        currentToken = parser.getCurrentToken();
    }
    if (currentToken != null) {
        parser.nextToken();
    }
    return ret;
}
 
Example 3
Source File: WebStorageImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
private void collectDataMap(Map<String, WebStorageEntry> dataMapToCollectIn, String dataAsJson) throws IOException {
    Map<String, Class<? extends WebStorageEntry>> typeMap = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();

    mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);

    for (WebStorageBundle<? extends WebStorageEntry> bundle : WebStorageBundle.definitions()) {
        typeMap.put(bundle.getName(), bundle.getType());
    }

    JsonFactory factory = new JsonFactory();

    try (JsonParser parser = factory.createJsonParser(dataAsJson)) {
        parser.setCodec(mapper);

        if (JsonToken.START_OBJECT != parser.nextToken()) {
            throw new IOException("Missing expected `{` token");
        }

        for ( ; ; ) {
            JsonToken token = parser.nextToken();

            if (token == JsonToken.FIELD_NAME) {
                String name = parser.getCurrentName();
                Class<? extends WebStorageEntry> type = typeMap.get(name);

                parser.nextToken();

                if (type == null) {
                    parser.skipChildren();
                    logger.warn("Missing expected definition for `" + name + "` bundle");
                } else {
                    try {
                        dataMapToCollectIn.put(name, mapper.readValue(parser.readValueAsTree(), type));
                    } catch (JsonMappingException e) {
                        logger.warn("Failed to deserialize `" + name + "` bundle", e);
                    }
                }
            } else if (token == JsonToken.END_OBJECT) {
                return;
            } else {
                throw new IOException("Unexpected token (field name or `}` were expected)");
            }
        }
    }
}