Java Code Examples for com.fasterxml.jackson.core.JsonParser#nextIntValue()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#nextIntValue() . 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: DataChunk.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
static void parseFieldName(JsonParser parser, JsonParsingContext context) throws IOException {
    String fieldName = parser.getCurrentName();
    switch (fieldName) {
        case "offset":
            context.offset = parser.nextIntValue(-1);
            context.doubleValues = null;
            context.stringValues = null;
            break;
        case "uncompressedLength":
            context.uncompressedLength = parser.nextIntValue(-1);
            break;
        case "stepLengths":
            context.stepLengths = new TIntArrayList();
            context.valuesOrLengthArray = true;
            break;
        case "values":
        case "stepValues":
            context.valuesOrLengthArray = true;
            break;
        default:
            break;
    }
}
 
Example 2
Source File: OpenSkyStatesDeserializer.java    From opensky-api with GNU General Public License v3.0 6 votes vote down vote up
@Override
public OpenSkyStates deserialize(JsonParser jp, DeserializationContext dc) throws IOException {
	if (jp.getCurrentToken() != null && jp.getCurrentToken() != JsonToken.START_OBJECT) {
		throw dc.mappingException(OpenSkyStates.class);
	}
	try {
		OpenSkyStates res = new OpenSkyStates();
		for (jp.nextToken(); jp.getCurrentToken() != null && jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
			if (jp.getCurrentToken() == JsonToken.FIELD_NAME) {
				if ("time".equalsIgnoreCase(jp.getCurrentName())) {
					int t = jp.nextIntValue(0);
					res.setTime(t);
				} else if ("states".equalsIgnoreCase(jp.getCurrentName())) {
					jp.nextToken();
					res.setStates(deserializeStates(jp));
				} else {
					// ignore other fields, but consume value
					jp.nextToken();
				}
			} // ignore others
		}
		return res;
	} catch (JsonParseException jpe) {
		throw dc.mappingException(OpenSkyStates.class);
	}
}
 
Example 3
Source File: JsonUtilsTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Map<Id, Delta> decode(byte[] json) throws IOException {
  Map<Id, Delta> values = new HashMap<>();
  JsonParser parser = FACTORY.createParser(json);

  // Array start
  Assertions.assertEquals(JsonToken.START_ARRAY, parser.nextToken());

  // String table
  String[] strings = new String[parser.nextIntValue(-1)];
  for (int i = 0; i < strings.length; ++i) {
    strings[i] = parser.nextTextValue();
  }

  // Read measurements
  parser.nextToken();
  while (parser.currentToken() != JsonToken.END_ARRAY) {
    int n = parser.getIntValue();
    Map<String, String> tags = new HashMap<>(n);
    for (int i = 0; i < n; ++i) {
      String k = strings[parser.nextIntValue(-1)];
      String v = strings[parser.nextIntValue(-1)];
      tags.put(k, v);
    }
    String name = tags.get("name");
    tags.remove("name");
    Id id = registry.createId(name).withTags(tags);
    int op = parser.nextIntValue(-1);
    parser.nextToken();
    double value = parser.getDoubleValue();
    values.put(id, new Delta(op, value));

    parser.nextToken();
  }

  return values;
}
 
Example 4
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
private final int _nextInt(JsonParser p) throws IOException {
    int i = p.nextIntValue(-2);
    if (i != -2) {
        return i;
    }
    return p.getValueAsInt();
}
 
Example 5
Source File: NetworkMetadataDeserializer.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public NetworkMetadata deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
    String id = null;
    String sourceFormat = null;
    DateTime caseDate = null;
    int forecastDistance = 0;

    List<Extension<NetworkMetadata>> extensions = Collections.emptyList();

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        switch (parser.getCurrentName()) {
            case "id":
                id = parser.nextTextValue();
                break;

            case "sourceFormat":
                sourceFormat = parser.nextTextValue();
                break;

            case "caseDate":
                caseDate = DateTime.parse(parser.nextTextValue());
                break;

            case "forecastDistance":
                forecastDistance = parser.nextIntValue(0);
                break;

            case "extensions":
                parser.nextToken();
                extensions = JsonUtil.readExtensions(parser, deserializationContext, SUPPLIER.get());
                break;

            default:
                throw new PowsyblException("Unexpected field: " + parser.getCurrentName());
        }
    }

    NetworkMetadata metadata = new NetworkMetadata(id, sourceFormat, caseDate, forecastDistance);
    SUPPLIER.get().addExtensions(metadata, extensions);

    return metadata;
}
 
Example 6
Source File: BulkResultDeserializer.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    int took = 0;
    boolean errors = false;
    BulkError error = null;
    int status = -1;
    List<BulkResultItem> items = null;

    String fieldName;
    while ((fieldName = p.nextFieldName()) != null)  {
        switch (fieldName) {
            case "took": {
                took = p.nextIntValue(-1);
                break;
            }
            case "errors": {
                errors = p.nextBooleanValue();
                break;
            }
            case "status": {
                status = p.nextIntValue(-1);
                break;
            }
            case "error": {
                p.nextValue(); // skip to START_OBJECT or VALUE_NULL
                JsonDeserializer<Object> typeDeserializer = ctxt.findNonContextualValueDeserializer(ctxt.constructType(BulkError.class));
                error = (BulkError) typeDeserializer.deserialize(p, ctxt);
                break;
            }
            case "items": {
                if (errors) {
                    items = new ArrayList<>();
                    p.nextValue(); // skip to START_ARRAY
                    p.nextValue(); // skip to START_OBJECT
                    ObjectMapper mapper = (ObjectMapper) p.getCodec();
                    MappingIterator<BulkResultItem> bulkResultItemMappingIterator = mapper.readValues(p, BulkResultItem.class);

                    while (bulkResultItemMappingIterator.hasNext()) {
                        items.add(bulkResultItemMappingIterator.next());
                    }
                }
                break;
            }
        }
    }

    return new BufferedBulkResult(took, errors, error, status, items);
}
 
Example 7
Source File: BatchResultDeserializer.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    int took = 0;
    boolean errors = false;
    Error error = null;
    int status = -1;
    List<BatchItemResult> items = null;

    String fieldName;
    while ((fieldName = p.nextFieldName()) != null)  {
        switch (fieldName) {
            case "took": {
                took = p.nextIntValue(-1);
                break;
            }
            case "errors": {
                errors = p.nextBooleanValue();
                break;
            }
            case "status": {
                status = p.nextIntValue(-1);
                break;
            }
            case "error": {
                p.nextValue(); // skip to START_OBJECT or VALUE_NULL
                JsonDeserializer<Object> typeDeserializer = ctxt.findNonContextualValueDeserializer(ctxt.constructType(Error.class));
                error = (Error) typeDeserializer.deserialize(p, ctxt);
                break;
            }
            case "items": {
                if (errors) {
                    items = new ArrayList<>();
                    p.nextValue(); // skip to START_ARRAY
                    p.nextValue(); // skip to START_OBJECT
                    ObjectMapper mapper = (ObjectMapper) p.getCodec();
                    MappingIterator<BatchItemResult> batchResultItemMappingIterator = mapper.readValues(p, BatchItemResult.class);

                    while (batchResultItemMappingIterator.hasNext()) {
                        items.add(batchResultItemMappingIterator.next());
                    }
                }
                break;
            }
        }
    }

    return new BatchResult(took, errors, error, status, items);
}
 
Example 8
Source File: OpenSkyStatesDeserializer.java    From opensky-api with GNU General Public License v3.0 4 votes vote down vote up
private Collection<StateVector> deserializeStates(JsonParser jp) throws IOException {
	ArrayList<StateVector> result = new ArrayList<>();

	for (JsonToken next = jp.nextToken(); next != null && next != JsonToken.END_ARRAY; next = jp.nextToken()) {
		if (next == JsonToken.START_ARRAY) {
			continue;
		}
		if (next == JsonToken.END_OBJECT) {
			break;
		}
		String icao24 = jp.getText();
		if ("null".equals(icao24)) {
			throw new JsonParseException("Got 'null' icao24", jp.getCurrentLocation());
		}

		StateVector sv = new StateVector(icao24);
		sv.setCallsign(jp.nextTextValue());
		sv.setOriginCountry(jp.nextTextValue());
		sv.setLastPositionUpdate((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setLastContact((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setLongitude((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setLatitude((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setBaroAltitude((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setOnGround(jp.nextBooleanValue());
		sv.setVelocity((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setHeading((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setVerticalRate((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));

		// sensor serials if present
		next = jp.nextToken();
		if (next == JsonToken.START_ARRAY) {
			for (next = jp.nextToken(); next != null && next != JsonToken.END_ARRAY; next = jp.nextToken()) {
				sv.addSerial(jp.getIntValue());
			}
		}

		sv.setGeoAltitude((jp.nextToken() != null && jp.getCurrentToken() != JsonToken.VALUE_NULL ? jp.getDoubleValue() : null));
		sv.setSquawk(jp.nextTextValue());
		sv.setSpi(jp.nextBooleanValue());

		int psi = jp.nextIntValue(0);
		StateVector.PositionSource ps = psi <= StateVector.PositionSource.values().length ?
				StateVector.PositionSource.values()[psi] : StateVector.PositionSource.UNKNOWN;

		sv.setPositionSource(ps);

		// there are additional fields (upward compatibility), consume until end of this state vector array
		next = jp.nextToken();
		while (next != null && next != JsonToken.END_ARRAY) {
			// ignore
			next = jp.nextToken();
		}
		// consume "END_ARRAY" or next "START_ARRAY"
		jp.nextToken();

		result.add(sv);
	}

	return result;
}
 
Example 9
Source File: YearMonthDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public YearMonth deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    if (parser.hasToken(JsonToken.VALUE_STRING)) {
        String string = parser.getText().trim();
        if (string.length() == 0) {
            if (!isLenient()) {
                return _failForNotLenient(parser, context, JsonToken.VALUE_STRING);
            }
            return null;
        }
        try {
            return YearMonth.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (parser.isExpectedStartArrayToken()) {
        JsonToken t = parser.nextToken();
        if (t == JsonToken.END_ARRAY) {
            return null;
        }
        if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT)
                && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
            final YearMonth parsed = deserialize(parser, context);
            if (parser.nextToken() != JsonToken.END_ARRAY) {
                handleMissingEndArrayForSingle(parser, context);
            }
            return parsed;            
        }
        if (t != JsonToken.VALUE_NUMBER_INT) {
            _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "years");
        }
        int year = parser.getIntValue();
        int month = parser.nextIntValue(-1);
        if (month == -1) {
            if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
                _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "months");
            }
            month = parser.getIntValue();
        }
        if (parser.nextToken() != JsonToken.END_ARRAY) {
            throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
                    "Expected array to end");
        }
        return YearMonth.of(year, month);
    }
    if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
        return (YearMonth) parser.getEmbeddedObject();
    }
    return _handleUnexpectedToken(context, parser,
            JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
}
 
Example 10
Source File: MonthDayDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public MonthDay deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    if (parser.hasToken(JsonToken.VALUE_STRING)) {
        String string = parser.getValueAsString().trim();
        try {
            if (_formatter == null) {
                return MonthDay.parse(string);
            }
            return MonthDay.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (parser.isExpectedStartArrayToken()) {
        JsonToken t = parser.nextToken();
        if (t == JsonToken.END_ARRAY) {
            return null;
        }
        if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT)
                && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
            final MonthDay parsed = deserialize(parser, context);
            if (parser.nextToken() != JsonToken.END_ARRAY) {
                handleMissingEndArrayForSingle(parser, context);
            }
            return parsed;
        }
        if (t != JsonToken.VALUE_NUMBER_INT) {
            _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "month");
        }
        int month = parser.getIntValue();
        int day = parser.nextIntValue(-1);
        if (day == -1) {
            if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
                _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "day");
            }
            day = parser.getIntValue();
        }
        if (parser.nextToken() != JsonToken.END_ARRAY) {
            throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
                    "Expected array to end");
        }
        return MonthDay.of(month, day);
    }
    if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
        return (MonthDay) parser.getEmbeddedObject();
    }
    return _handleUnexpectedToken(context, parser,
            JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
}