Refine search
public AbstractJsonRowRecordReader(final InputStream in, final ComponentLog logger) throws IOException, MalformedRecordException { this.logger = logger; try { jsonParser = jsonFactory.createJsonParser(in); jsonParser.setCodec(codec); JsonToken token = jsonParser.nextToken(); if (token == JsonToken.START_ARRAY) { array = true; token = jsonParser.nextToken(); // advance to START_OBJECT token } else { array = false; } if (token == JsonToken.START_OBJECT) { // could be END_ARRAY also firstJsonNode = jsonParser.readValueAsTree(); } else { firstJsonNode = null; } } catch (final JsonParseException e) { throw new MalformedRecordException("Could not parse data as JSON", e); } }
protected void parseAndPerform( UriInfo uriInfo, HttpHeaders httpHeaders, HttpServletRequest req, InputStream body, Map<Integer, String> locations ) throws IOException, ServletException JsonParser jp = jsonFactory.createJsonParser(body); JsonToken token; RequestData requestData = RequestData.from( req ); while ( (token = jp.nextToken()) != null ) String jobBody = ""; Integer jobId = null; while ( (token = jp.nextToken()) != JsonToken.END_OBJECT && token != null ) String field = jp.getText(); jp.nextToken(); switch ( field ) jobMethod = jp.getText().toUpperCase(); break; case TO_KEY: jobPath = jp.getText(); break; case ID_KEY: jobId = jp.getIntValue(); break; case BODY_KEY:
/** * Method that fetches next token (as if calling {@link #nextToken}) and * verifies whether it is {@link JsonToken#FIELD_NAME} with specified name * and returns result of that comparison. * It is functionally equivalent to: *<pre> * return (nextToken() == JsonToken.FIELD_NAME) && str.getValue().equals(getCurrentName()); *</pre> * but may be faster for parser to verify, and can therefore be used if caller * expects to get such a property name from input next. * * @param str Property name to compare next token to (if next token is <code>JsonToken.FIELD_NAME<code>) * * @since 1.9 */ public boolean nextFieldName(SerializableString str) throws IOException, JsonParseException { return (nextToken() == JsonToken.FIELD_NAME) && str.getValue().equals(getCurrentName()); }
/** * Method that fetches next token (as if calling {@link #nextToken}) and * if it is {@link JsonToken#VALUE_NUMBER_INT} returns 32-bit int value; * otherwise returns specified default value * It is functionally equivalent to: *<pre> * return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getIntValue() : defaultValue; *</pre> * but may be faster for parser to process, and can therefore be used if caller * expects to get a String value next from input. * * @since 1.9 */ public int nextIntValue(int defaultValue) throws IOException, JsonParseException { return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getIntValue() : defaultValue; }
/** * Method that fetches next token (as if calling {@link #nextToken}) and * if it is {@link JsonToken#VALUE_NUMBER_INT} returns 64-bit long value; * otherwise returns specified default value * It is functionally equivalent to: *<pre> * return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getLongValue() : defaultValue; *</pre> * but may be faster for parser to process, and can therefore be used if caller * expects to get a String value next from input. * * @since 1.9 */ public long nextLongValue(long defaultValue) throws IOException, JsonParseException { return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getLongValue() : defaultValue; }
protected final boolean _parseBooleanFromNumber(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { if (jp.getNumberType() == NumberType.LONG) { return (jp.getLongValue() == 0L) ? Boolean.FALSE : Boolean.TRUE; } // no really good logic; let's actually resort to textual comparison String str = jp.getText(); if ("0.0".equals(str) || "0".equals(str)) { return Boolean.FALSE; } return Boolean.TRUE; }
switch (jp.getCurrentToken()) { case START_OBJECT: writeStartObject(); break; case FIELD_NAME: writeFieldName(jp.getCurrentName()); break; case VALUE_STRING: if (jp.hasTextCharacters()) { writeString(jp.getTextCharacters(), jp.getTextOffset(), jp.getTextLength()); } else { writeString(jp.getText()); switch (jp.getNumberType()) { case INT: writeNumber(jp.getIntValue()); break; case BIG_INTEGER: writeNumber(jp.getBigIntegerValue()); break; default: writeNumber(jp.getLongValue()); switch (jp.getNumberType()) { case BIG_DECIMAL: writeNumber(jp.getDecimalValue()); break; case FLOAT:
/** * Reconfigures this JsonDecoder to use the String provided for input. * <p/> * If the String provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonDecoder will reset its state and then * reconfigure its input. * @param in * The String to read from. Cannot be null. * @throws IOException * @return this JsonDecoder */ public JsonDecoder configure(String in) throws IOException { if (null == in) { throw new NullPointerException("String to read from cannot be null!"); } parser.reset(); this.in = new JsonFactory().createJsonParser(in); this.in.nextToken(); return this; }
@Override public ReadablePeriod deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // TODO: perhaps support array of numbers... //if (jp.isExpectedStartArrayToken()) { ] switch (jp.getCurrentToken()) { case VALUE_NUMBER_INT: // assume it's millisecond count return new Period(jp.getLongValue()); case VALUE_STRING: return new Period(jp.getText()); } throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Number or String"); } }
JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) { String fieldName = jp.getCurrentName(); JsonNode value; switch (jp.nextToken()) { case START_OBJECT: value = deserializeObject(jp, ctxt, nodeFactory); break; case VALUE_STRING: value = nodeFactory.textNode(jp.getText()); break; default:
throws IOException, JsonProcessingException JsonToken t = jp.getCurrentToken(); switch (t) { return jp.getText(); return jp.getBigIntegerValue(); return jp.getIntValue(); return jp.getDecimalValue(); return Double.valueOf(jp.getDoubleValue()); return Boolean.FALSE; case VALUE_EMBEDDED_OBJECT: return jp.getEmbeddedObject();
protected Object _unwrapAndDeserialize(JsonParser jp, JavaType rootType, DeserializationContext ctxt, JsonDeserializer<Object> deser) throws IOException, JsonParseException, JsonMappingException { SerializedString rootName = _deserializerProvider.findExpectedRootName(ctxt.getConfig(), rootType); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw JsonMappingException.from(jp, "Current token not START_OBJECT (needed to unwrap root name '" +rootName+"'), but "+jp.getCurrentToken()); } if (jp.nextToken() != JsonToken.FIELD_NAME) { throw JsonMappingException.from(jp, "Current token not FIELD_NAME (to contain expected root name '" +rootName+"'), but "+jp.getCurrentToken()); } String actualName = jp.getCurrentName(); if (!rootName.getValue().equals(actualName)) { throw JsonMappingException.from(jp, "Root name '"+actualName+"' does not match expected ('"+rootName +"') for type "+rootType); } // ok, then move to value itself.... jp.nextToken(); Object result = deser.deserialize(jp, ctxt); // and last, verify that we now get matching END_OBJECT if (jp.nextToken() != JsonToken.END_OBJECT) { throw JsonMappingException.from(jp, "Current token not END_OBJECT (to match wrapper object with root name '" +rootName+"'), but "+jp.getCurrentToken()); } return result; }
switch (jp.getCurrentToken()) { case VALUE_STRING: if (_acceptString) { return jp.getText(); return jp.getIntValue(); return Double.valueOf(jp.getDoubleValue());
throws IOException, JsonProcessingException JsonToken t = jp.getCurrentToken(); int lineNumber = -1; while ((t = jp.nextValue()) != JsonToken.END_OBJECT) { String propName = jp.getCurrentName(); if ("className".equals(propName)) { className = jp.getText(); } else if ("fileName".equals(propName)) { fileName = jp.getText(); } else if ("lineNumber".equals(propName)) { if (t.isNumeric()) { lineNumber = jp.getIntValue(); } else { throw JsonMappingException.from(jp, "Non-numeric token ("+t+") for property 'lineNumber'"); methodName = jp.getText(); } else if ("nativeMethod".equals(propName)) {
/** * Reconfigures this JsonDecoder to use the InputStream provided. * <p/> * If the InputStream provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonDecoder will reset its state and then * reconfigure its input. * @param in * The IntputStream to read from. Cannot be null. * @throws IOException * @return this JsonDecoder */ public JsonDecoder configure(InputStream in) throws IOException { if (null == in) { throw new NullPointerException("InputStream to read from cannot be null!"); } parser.reset(); this.in = jsonFactory.createJsonParser(in); this.in.nextToken(); return this; }
@Override public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // We'll accept either long (timestamp) or array: if (jp.isExpectedStartArrayToken()) { jp.nextToken(); // VALUE_NUMBER_INT int year = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int month = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int day = jp.getIntValue(); if (jp.nextToken() != JsonToken.END_ARRAY) { throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDate ints"); } return new LocalDate(year, month, day); } switch (jp.getCurrentToken()) { case VALUE_NUMBER_INT: return new LocalDate(jp.getLongValue()); case VALUE_STRING: DateTime local = parseLocal(jp); if (local == null) { return null; } return local.toLocalDate(); } throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Array, String or Number"); } }
@Override public void copyCurrentStructure(JsonParser jp) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken(); // Let's handle field-name separately first if (t == JsonToken.FIELD_NAME) { writeFieldName(jp.getCurrentName()); t = jp.nextToken(); // fall-through to copy the associated value } switch (t) { case START_ARRAY: writeStartArray(); while (jp.nextToken() != JsonToken.END_ARRAY) { copyCurrentStructure(jp); } writeEndArray(); break; case START_OBJECT: writeStartObject(); while (jp.nextToken() != JsonToken.END_OBJECT) { copyCurrentStructure(jp); } writeEndObject(); break; default: // others are simple: copyCurrentEvent(jp); } }
/** * Equivalent of {@link #next} but one that may throw checked * exceptions from Jackson due to invalid input. */ public boolean hasNextValue() throws IOException { if (_parser == null) { return false; } JsonToken t = _parser.getCurrentToken(); if (t == null) { // un-initialized or cleared; find next t = _parser.nextToken(); // If EOF, no more if (t == null) { _parser.close(); return false; } // And similarly if we hit END_ARRAY; except that we won't close parser if (t == JsonToken.END_ARRAY) { return false; } } return true; }
private void encode(InputStream in) throws IOException { JsonParser jp = jsonFactory.createJsonParser(in); JsonGenerator jg = smileFactory.createJsonGenerator(System.out, JsonEncoding.UTF8); while ((jp.nextToken()) != null) { jg.copyCurrentEvent(jp); } jp.close(); jg.close(); }
JsonToken t = jp.getCurrentToken(); return jp.getDoubleValue(); String text = jp.getText().trim(); if (text.length() == 0) { return 0.0;