Example usage for com.fasterxml.jackson.core JsonParser getValueAsString

List of usage examples for com.fasterxml.jackson.core JsonParser getValueAsString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getValueAsString.

Prototype

public String getValueAsString() throws IOException, JsonParseException 

Source Link

Document

Method that will try to convert value of current token to a java.lang.String .

Usage

From source file:org.dbrain.data.jackson.serializers.JacksonSerializationUtils.java

public static Value parseValue(JsonParser parser, DeserializationContext ctxt) throws IOException {
    JsonToken token = getToken(parser);//www . j  a  va2  s . c om
    if (token != null) {
        Value result;
        switch (token) {
        case VALUE_STRING:
            result = Value.of(parser.getValueAsString());
            break;
        case VALUE_NUMBER_FLOAT:
            result = Value.of(parser.getDoubleValue());
            break;
        case VALUE_NUMBER_INT:
            result = Value.of(parser.getBigIntegerValue());
            break;
        case VALUE_NULL:
            result = NullValueImpl.NULL;
            break;
        case VALUE_TRUE:
            result = Value.of(Boolean.TRUE);
            break;
        case VALUE_FALSE:
            result = Value.of(Boolean.FALSE);
            break;
        case START_OBJECT: {
            ValueMap values = ValueMap.newInstance();
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                String key = parser.getCurrentName();
                parser.nextToken();
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.put(key, v);
            }
            if (getToken(parser) == JsonToken.END_OBJECT) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_OBJECT, null);
            }
            result = values;
        }
            break;
        case START_ARRAY: {
            ValueList values = ValueList.newInstance();
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.add(v);
            }
            if (getToken(parser) == JsonToken.END_ARRAY) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_ARRAY, null);
            }
            result = values;
        }
            break;
        default:
            throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
        }
        return result;
    } else {
        return null;
    }
}

From source file:com.microsoft.azure.storage.core.EncryptionAgent.java

public static EncryptionAgent deserialize(JsonParser parser) throws JsonParseException, IOException {
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();//from ww  w. j  a v a 2s .  c o m

    EncryptionAgent agent = new EncryptionAgent();
    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();

        if (name.equals("Protocol")) {
            agent.setProtocol(parser.getValueAsString());
        } else if (name.equals("EncryptionAlgorithm")) {
            agent.setEncryptionAlgorithm(EncryptionAlgorithm.valueOf(parser.getValueAsString()));
        }
        parser.nextToken();
    }

    JsonUtilities.assertIsEndObjectJsonToken(parser);

    return agent;
}

From source file:nebula.plugin.metrics.dispatcher.AbstractMetricsDispatcher.java

/**
 * Register Jackson module that maps enums as lowercase. Per http://stackoverflow.com/a/24173645.
 *//*from  w  ww. j a va 2  s. co m*/
@SuppressWarnings("rawtypes")
private static void registerEnumModule(ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {
        @Override
        public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
                BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
            return new JsonDeserializer<Enum>() {
                @Override
                public Enum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
                    @SuppressWarnings("unchecked")
                    Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
                    return Enum.valueOf(rawClass, jp.getValueAsString().toUpperCase());
                }
            };
        }
    });
    module.addSerializer(Enum.class, new StdSerializer<Enum>(Enum.class) {
        @Override
        public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.name().toLowerCase());
        }
    });
    mapper.registerModule(module);
}

From source file:com.microsoft.azure.storage.core.WrappedContentKey.java

public static WrappedContentKey deserialize(JsonParser parser) throws JsonParseException, IOException {
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();//from  w  w  w.  j  a v  a 2  s .  c o  m

    WrappedContentKey wrappedContentKey = new WrappedContentKey();
    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();

        if (name.equals("KeyId")) {
            wrappedContentKey.setKeyId(parser.getValueAsString());
        } else if (name.equals("EncryptedKey")) {
            wrappedContentKey.setEncryptedKey(parser.getBinaryValue());
        } else if (name.equals("Algorithm")) {
            wrappedContentKey.setAlgorithm(parser.getValueAsString());
        }
        parser.nextToken();
    }

    JsonUtilities.assertIsEndObjectJsonToken(parser);

    return wrappedContentKey;
}

From source file:org.n52.movingcode.runtime.processors.config.ProcessorConfig.java

/**
 * Parsing routine for processor objects
 * /*  w ww. j  av a2 s  . c o m*/
 * @param jp
 * @return
 * @throws JsonParseException
 * @throws IOException
 */
private static final ProcessorDescription parseProcessorJSON(JsonParser jp)
        throws JsonParseException, IOException {
    ProcessorDescription p = new ProcessorDescription();
    while (jp.nextToken() != END_OBJECT) {
        JsonToken jt = jp.getCurrentToken();

        // look for ID and parse it
        if (jp.getCurrentName().equalsIgnoreCase(KEY_ID) && jt == VALUE_STRING) {
            p.setId(jp.getValueAsString());
        }

        // look for temp workspace and parse it
        if (jp.getCurrentName().equalsIgnoreCase(KEY_TEMPWORKSPACE) && jt == VALUE_STRING) {
            p.setTempWorkspace(jp.getValueAsString());
        }

        // look for containers and parse them (Value Case)
        if (jp.getCurrentName().equalsIgnoreCase(KEY_SUPPORTED_CONTAINER) && jt == VALUE_STRING) {
            p.addContainer(jp.getValueAsString());
        }

        // look for containers and parse them (Array Case)
        if (jp.getCurrentName().equalsIgnoreCase(KEY_SUPPORTED_CONTAINER) && jt == START_ARRAY) {
            while (jp.nextToken() != END_ARRAY) {
                if (jp.getCurrentToken() == VALUE_STRING) {
                    p.addPlatform(jp.getValueAsString());
                }
            }
        }

        // look for platforms and parse them (Array Case)
        if (jp.getCurrentName().equalsIgnoreCase(KEY_AVAILABLE_PLATFORMS) && jt == JsonToken.START_ARRAY) {
            while (jp.nextToken() != END_ARRAY) {
                if (jp.getCurrentToken() == VALUE_STRING) {
                    p.addPlatform(jp.getValueAsString());
                }
            }
        }

        // look for platforms and parse them (Value Case)
        if (jp.getCurrentName().equalsIgnoreCase(KEY_AVAILABLE_PLATFORMS) && jt == JsonToken.VALUE_STRING) {
            p.addPlatform(jp.getValueAsString());
        }

        // look for properties and parse them
        if (jp.getCurrentName().equalsIgnoreCase(KEY_PROPERTIES) && jt == JsonToken.START_ARRAY) {
            HashMap<String, String> props = new HashMap<String, String>();
            while (jp.nextToken() != END_ARRAY) {
                if (jp.getCurrentToken() == FIELD_NAME) {
                    props.put(jp.getCurrentName(), jp.nextTextValue());
                }

            }

            p.setProperties(props);

        }
    }

    return p;
}

From source file:com.microsoft.azure.storage.core.EncryptionData.java

public static HashMap<String, String> deserializeKeyWrappingMetadata(JsonParser parser)
        throws JsonParseException, IOException {
    JsonUtilities.assertIsStartObjectJsonToken(parser);

    parser.nextToken();//from   ww w. ja v a2  s  .c  o  m

    HashMap<String, String> keyWrappingMetadata = new HashMap<String, String>();
    while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        parser.nextToken();
        keyWrappingMetadata.put(name, parser.getValueAsString());
        parser.nextToken();
    }

    JsonUtilities.assertIsEndObjectJsonToken(parser);

    return keyWrappingMetadata;
}

From source file:com.ntsync.shared.ContactGroup.java

/**
 * Creates and returns an instance of the RawContact from encrypted data
 * //ww  w.  j  a  va  2 s . c om
 * */
public static ContactGroup valueOf(String rowId, Map<Byte, ByteBuffer> values, Key privateKey)
        throws InvalidKeyException {
    try {
        String sourceId = null;
        Long rawId = null;

        if (values.containsKey(GroupConstants.SERVERROW_ID)) {
            sourceId = readRawString(values.get(GroupConstants.SERVERROW_ID));
        }

        if (sourceId == null || !sourceId.equals(rowId)) {
            // If ServerContactId is different, then rowId is the clientId
            rawId = Long.parseLong(rowId);
        }

        if (sourceId == null && rawId < 0) {
            throw new IllegalArgumentException("Missing RowId in data");
        }

        AEADBlockCipher cipher = CryptoHelper.getCipher();

        final boolean deleted = values.containsKey(GroupConstants.DELETED);

        final String textData = CryptoHelper.decodeStringValue(GroupConstants.TEXTDATA, values, cipher,
                privateKey);

        if (textData == null && !deleted) {
            LOG.error("No textdata found for row with Id:" + rowId);
            return null;
        }

        String title = null;
        String notes = null;

        if (!isEmpty(textData)) {
            JsonFactory fac = new JsonFactory();
            JsonParser jp = fac.createParser(textData);
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                // move to value, or START_OBJECT/START_ARRAY
                jp.nextToken();
                if (GroupConstants.TITLE.equals(fieldname)) {
                    title = jp.getValueAsString();
                } else if (GroupConstants.NOTES.equals(fieldname)) {
                    notes = jp.getValueAsString();
                } else {
                    LOG.error("Unrecognized field for row with Id:" + rowId + " Fieldname:" + fieldname);
                }
            }
            jp.close();
        }

        String modStr = readRawString(values.get(GroupConstants.MODIFIED));
        Date lastModified = null;
        if (!isEmpty(modStr)) {
            lastModified = new Date(Long.parseLong(modStr));
        }

        return new ContactGroup(rawId, sourceId, title, notes, deleted, lastModified, -1);
    } catch (InvalidCipherTextException ex) {
        throw new InvalidKeyException("Invalid key detected.", ex);
    } catch (final Exception ex) {
        LOG.info("Error parsing contactgroup data. Reason:" + ex.toString(), ex);
    }
    return null;
}

From source file:com.microsoft.azure.storage.blob.BlobEncryptionData.java

public static BlobEncryptionData deserialize(String inputData) throws JsonProcessingException, IOException {
    JsonParser parser = Utility.getJsonParser(inputData);
    BlobEncryptionData data = new BlobEncryptionData();
    try {// w  w  w .j  a va  2s.c o m
        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextToken();

        while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            parser.nextToken();

            if (name.equals(Constants.EncryptionConstants.ENCRYPTION_MODE)) {
                data.setEncryptionMode(parser.getValueAsString());
            } else if (name.equals(Constants.EncryptionConstants.WRAPPED_CONTENT_KEY)) {
                data.setWrappedContentKey(WrappedContentKey.deserialize(parser));
            } else if (name.equals(Constants.EncryptionConstants.ENCRYPTION_AGENT)) {
                data.setEncryptionAgent(EncryptionAgent.deserialize(parser));
            } else if (name.equals(Constants.EncryptionConstants.CONTENT_ENCRYPTION_IV)) {
                data.setContentEncryptionIV(parser.getBinaryValue());
            } else if (name.equals(Constants.EncryptionConstants.KEY_WRAPPING_METADATA)) {
                data.setKeyWrappingMetadata(deserializeKeyWrappingMetadata(parser));
            } else {
                consumeJsonObject(parser);
            }
            parser.nextToken();
        }

        JsonUtilities.assertIsEndObjectJsonToken(parser);
    } finally {
        parser.close();
    }

    return data;
}

From source file:com.boundary.zoocreeper.Restore.java

private static ACL readACL(JsonParser jp) throws IOException {
    expectCurrentToken(jp, JsonToken.START_OBJECT);
    String scheme = null;//from   w w  w.ja v  a 2s .  c om
    String id = null;
    int perms = -1;
    final Set<String> seenFields = Sets.newHashSet();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        jp.nextValue();
        final String fieldName = jp.getCurrentName();
        seenFields.add(fieldName);
        if (Backup.FIELD_ACL_SCHEME.equals(fieldName)) {
            scheme = jp.getValueAsString();
        } else if (Backup.FIELD_ACL_ID.equals(fieldName)) {
            id = jp.getValueAsString();
        } else if (Backup.FIELD_ACL_PERMS.equals(fieldName)) {
            perms = jp.getIntValue();
        } else {
            throw new IOException("Unexpected field: " + fieldName);
        }
    }
    if (!seenFields.containsAll(REQUIRED_ACL_FIELDS)) {
        throw new IOException("Missing required ACL fields: " + REQUIRED_ACL_FIELDS);
    }
    final Id zkId;
    if (Ids.ANYONE_ID_UNSAFE.getScheme().equals(scheme) && Ids.ANYONE_ID_UNSAFE.getId().equals(id)) {
        zkId = Ids.ANYONE_ID_UNSAFE;
    } else {
        zkId = new Id(scheme, id);
    }
    return new ACL(perms, zkId);
}

From source file:com.microsoft.azure.storage.queue.CloudQueueEncryptedMessage.java

public static CloudQueueEncryptedMessage deserialize(String inputMessage)
        throws JsonProcessingException, IOException {
    JsonParser parser = Utility.getJsonParser(inputMessage);
    CloudQueueEncryptedMessage message = new CloudQueueEncryptedMessage();
    try {/*ww  w . j  a  v a 2 s  . c o m*/
        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextToken();

        while (parser.getCurrentToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            parser.nextToken();

            if (name.equals("EncryptedMessageContents")) {
                message.setEncryptedMessageContents(parser.getValueAsString());
            } else if (name.equals("EncryptionData")) {
                message.setEncryptionData(EncryptionData.deserialize(parser));
            }
            parser.nextToken();
        }

        JsonUtilities.assertIsEndObjectJsonToken(parser);
    } finally {
        parser.close();
    }

    return message;
}