use of com.fasterxml.jackson.core.JsonToken in project moco by dreamhead.
the class TextContainerDeserializerHelper method textContainer.
public TextContainer textContainer(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
JsonToken currentToken = jp.getCurrentToken();
if (currentToken == JsonToken.FIELD_NAME) {
TextContainer.Builder builder = builder();
String operation = jp.getText().trim();
builder.withOperation(operation);
JsonToken token = jp.nextToken();
if (isForTemplate(operation) && token == JsonToken.START_OBJECT) {
return template(jp, builder);
}
if (token == JsonToken.VALUE_STRING) {
String text = jp.getText().trim();
jp.nextToken();
return builder.withText(text).build();
}
}
return (TextContainer) ctxt.handleUnexpectedToken(TextContainer.class, jp);
}
use of com.fasterxml.jackson.core.JsonToken in project killbill by killbill.
the class PluginPropertySerializer method deserialize.
public static Iterable<PluginProperty> deserialize(final byte[] input) throws PluginPropertySerializerException {
final List<PluginProperty> result = new ArrayList<PluginProperty>();
try {
final byte[] uncompressed = LZFDecoder.decode(input);
final InputStream in = new ByteArrayInputStream(uncompressed);
final JsonParser jsonParser = jsonFactory.createParser(in);
PluginProperty prop = null;
String key = null;
JsonToken nextToken = jsonParser.nextToken();
while (nextToken != null && nextToken != JsonToken.END_ARRAY) {
if (nextToken != JsonToken.START_ARRAY) {
if (nextToken == JsonToken.FIELD_NAME && key == null) {
key = jsonParser.getText();
} else if (key != null) {
final Object value = mapper.readValue(jsonParser, Object.class);
prop = new PluginProperty(key, value, false);
key = null;
} else if (nextToken == JsonToken.END_OBJECT) {
result.add(prop);
prop = null;
}
}
nextToken = jsonParser.nextToken();
}
jsonParser.close();
return result;
} catch (final UnsupportedEncodingException e) {
throw new PluginPropertySerializerException(e);
} catch (final JsonParseException e) {
throw new PluginPropertySerializerException(e);
} catch (final IOException e) {
throw new PluginPropertySerializerException(e);
}
}
use of com.fasterxml.jackson.core.JsonToken in project openhab1-addons by openhab.
the class JsonWeatherParser method handleToken.
/**
* Iterates through the JSON structure and collects weather data.
*/
private void handleToken(JsonParser jp, String property, Weather weather) throws Exception {
JsonToken token = jp.getCurrentToken();
String prop = PropertyResolver.add(property, jp.getCurrentName());
if (token.isStructStart()) {
boolean isObject = token == JsonToken.START_OBJECT || token == JsonToken.END_OBJECT;
Weather forecast = !isObject ? weather : startIfForecast(weather, prop);
while (!jp.nextValue().isStructEnd()) {
handleToken(jp, prop, forecast);
}
if (isObject) {
endIfForecast(weather, forecast, prop);
}
} else if (token != null) {
try {
setValue(weather, prop, jp.getValueAsString());
} catch (Exception ex) {
logger.error("Error setting property '{}' with value '{}' ({})", prop, jp.getValueAsString(), ex.getMessage());
}
}
}
use of com.fasterxml.jackson.core.JsonToken in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method readMapInMap.
@Override
protected <K, V> Map<K, V> readMapInMap(JsonParser input, Function<JsonParser, K> keyDeserializer, Function<JsonParser, V> valueDeserializer, Map<K, V> map) throws Exception {
JsonToken token = input.getCurrentToken();
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
token = input.nextToken();
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
}
JsonToken currentToken = input.nextToken();
while (currentToken != JsonToken.END_ARRAY) {
if (currentToken != JsonToken.START_OBJECT) {
throw new ValueSerializationException("Expected an object start at " + input.getCurrentLocation().toString());
}
currentToken = input.nextToken();
K key = null;
V value = null;
while (currentToken != JsonToken.END_OBJECT) {
String objectKey = input.getCurrentName();
input.nextToken();
if ("key".equals(objectKey)) {
key = keyDeserializer.map(input);
} else if ("value".equals(objectKey)) {
value = valueDeserializer.map(input);
} else {
//input.nextToken();
input.skipChildren();
}
currentToken = input.nextToken();
}
if (key != null) {
map.put(key, value);
}
currentToken = input.nextToken();
}
return map;
}
use of com.fasterxml.jackson.core.JsonToken in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method readArrayInCollection.
@Override
protected <T> Collection<T> readArrayInCollection(JsonParser input, Function<JsonParser, T> deserializer, Collection<T> collection) throws Exception {
JsonToken token = input.getCurrentToken();
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
token = input.nextToken();
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
}
while (input.nextToken() != JsonToken.END_ARRAY) {
T element = deserializer.map(input);
collection.add(element);
}
return collection;
}
Aggregations