private void writeValue(JsonRecord value) throws IOException { switch (value.getKind()) { case JsonTokenId.ID_STRING: jg.writeString(value.getValue()); break; case JsonTokenId.ID_NULL: jg.writeNull(); break; case JsonTokenId.ID_NUMBER_FLOAT: case JsonTokenId.ID_NUMBER_INT: jg.writeNumber(value.getValue()); break; case JsonTokenId.ID_TRUE: jg.writeBoolean(true); break; case JsonTokenId.ID_FALSE: jg.writeBoolean(false); break; default: } }
@Override public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { if (parser.hasTokenId(JsonTokenId.ID_STRING)) { String string = parser.getText().trim(); if (string.length() == 0) { return null; } try { return LocalDateTime.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeException e) { throw new IOException(e); } } if (parser.hasTokenId(JsonTokenId.ID_EMBEDDED_OBJECT)) { return (LocalDateTime) parser.getEmbeddedObject(); } throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, "Expected string."); }
@Override public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException { if (parser.hasTokenId(JsonTokenId.ID_STRING)) { String string = parser.getText().trim(); if (string.length() == 0) { return null; } try { return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE); } catch (DateTimeException e) { throw new IOException(e); } } if (parser.hasTokenId(JsonTokenId.ID_EMBEDDED_OBJECT)) { return (LocalDate) parser.getEmbeddedObject(); } throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, "Expected string."); }
@Override public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.getCurrentTokenId()) { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return Duration.ofSeconds(seconds, nanoseconds); case JsonTokenId.ID_NUMBER_INT: return Duration.ofSeconds(parser.getLongValue()); case JsonTokenId.ID_STRING: String string = parser.getText().trim(); if (string.length() == 0) { return null; } return Duration.parse(string); } throw context.mappingException("Expected type float, integer, or string."); }
private Cookies deserializeObject(JsonParser p, DeserializationContext ctxt, JsonNodeFactory nodeFactory) throws IOException { Cookies cookies = new Cookies(); NottableString key = string(""); while (true) { JsonToken t = p.nextToken(); switch (t.id()) { case JsonTokenId.ID_FIELD_NAME: key = string(ctxt.readValue(p, String.class)); break; case JsonTokenId.ID_STRING: cookies.withEntry(key, string(ctxt.readValue(p, String.class))); break; case JsonTokenId.ID_END_OBJECT: return cookies; default: throw new RuntimeException("Unexpected token: \"" + t + "\" id: \"" + t.id() + "\" text: \"" + p.getText()); } } }
private JsonToken processToken(JsonToken curToken, JsonParser parser, JsonGenerator jgen) throws IOException { if (curToken.isStructEnd()) { // this is our escape and base case. return curToken; } if (curToken.isStructStart()) { processStructStart(curToken, jgen); JsonToken token = processToken(parser.nextToken(), parser, jgen); while (!token.isStructEnd()) { token = processToken(token, parser, jgen); } processStructEnd(token, jgen); return parser.nextToken(); } if (curToken.id() == JsonTokenId.ID_FIELD_NAME) { String currentName = parser.getCurrentName(); if (isFiltered && filters.containsKey(parser.getCurrentName())) { jgen.writeFieldName(currentName); // perform filtering. return filters.get(parser.getCurrentName()).processToken(parser.nextToken(), parser, jgen); } else if (!isFiltered) { jgen.writeFieldName(currentName); return processToken(parser.nextToken(), parser, jgen); } else { parser.nextToken(); parser.skipChildren(); return processToken(parser.nextToken(), parser, jgen); } } else if (curToken.isScalarValue()) { processValue(curToken, parser, jgen); return parser.nextToken(); } else { LOGGER.error( "Unable to process the token {} with name {}.", curToken, parser.getCurrentName()); throw new RuntimeException( "Unable to process the token " + curToken + " with name" + parser.getCurrentName()); } }
/** * Uses a JsonToken + JsonParser to determine how to write a value to the JsonGenerator. * * <p>This separation exists so we can separate the iteration logic from the parsing logic. * * @param valueToken current token we are interested in from the parser * @param parser current parser * @param jgen JsonGenerator that is used for writing json onto an underlying stream * @throws JsonGenerationException if access to the JsonParser throws a JsonGenerationException * @throws IOException if the Jackson utilties (JsonParser or JsonGenerator) throw an IOException */ private void processValue(JsonToken valueToken, JsonParser parser, JsonGenerator jgen) throws IOException { if (valueToken.isBoolean()) { jgen.writeBoolean(parser.getBooleanValue()); } else if (valueToken.isNumeric()) { if (parser.getNumberType() == JsonParser.NumberType.INT) { jgen.writeNumber(parser.getIntValue()); } else if (parser.getNumberType() == JsonParser.NumberType.DOUBLE) { jgen.writeNumber(parser.getDoubleValue()); } else if (parser.getNumberType() == JsonParser.NumberType.FLOAT) { jgen.writeNumber(parser.getFloatValue()); } else if (parser.getNumberType() == JsonParser.NumberType.LONG) { jgen.writeNumber(parser.getLongValue()); } else if (parser.getNumberType() == JsonParser.NumberType.BIG_DECIMAL) { jgen.writeNumber(parser.getDecimalValue()); } else if (parser.getNumberType() == JsonParser.NumberType.BIG_INTEGER) { jgen.writeNumber(parser.getBigIntegerValue()); } else { LOGGER.error("Found unsupported numeric value with name {}.", parser.getCurrentName()); throw new RuntimeException( "Found unsupported numeric value with name " + parser.getCurrentName()); } } else if (valueToken.id() == JsonTokenId.ID_STRING) { jgen.writeString(parser.getText()); } else { // Something bad just happened. Probably an unsupported type. LOGGER.error( "Found unsupported value type {} for name {}.", valueToken.id(), parser.getCurrentName()); throw new RuntimeException( "Found unsupported value type " + valueToken.id() + " for name " + parser .getCurrentName()); } }
private void processStructEnd(JsonToken token, JsonGenerator jgen) throws IOException { if (token.id() == JsonTokenId.ID_END_OBJECT) { jgen.writeEndObject(); } else if (token.id() == JsonTokenId.ID_END_ARRAY) { jgen.writeEndArray(); } else { LOGGER.error("Illegal struct end {}", token.id()); throw new RuntimeException("Illegal struct end " + token.id()); } }
public LocalDate deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { if (parser.hasTokenId(JsonTokenId.ID_STRING)) { String date = parser.getText().trim(); if (date.isEmpty()) { return null; } return LocalDateTime.ofInstant(Instant.parse(date), ZoneOffset.UTC).toLocalDate(); } return null; }
@Override public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.currentTokenId()) { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return Duration.ofSeconds(seconds, nanoseconds); case JsonTokenId.ID_NUMBER_INT: if(context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) { return Duration.ofSeconds(parser.getLongValue()); } return Duration.ofMillis(parser.getLongValue()); case JsonTokenId.ID_STRING: String string = parser.getText().trim(); if (string.length() == 0) { return null; } try { return Duration.parse(string); } catch (DateTimeException e) { return _rethrowDateTimeException(parser, context, e, string); } case JsonTokenId.ID_EMBEDDED_OBJECT: // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily return (Duration) parser.getEmbeddedObject(); case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(parser, context); } return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT); }
private Cookies deserializeArray(JsonParser p, DeserializationContext ctxt, JsonNodeFactory nodeFactory) throws IOException { Cookies headers = new Cookies(); NottableString key = null; NottableString value = null; String fieldName = null; while (true) { JsonToken t = p.nextToken(); switch (t.id()) { case JsonTokenId.ID_END_ARRAY: return headers; case JsonTokenId.ID_START_OBJECT: key = null; value = null; break; case JsonTokenId.ID_FIELD_NAME: fieldName = ctxt.readValue(p, String.class); break; case JsonTokenId.ID_STRING: if ("name".equals(fieldName)) { key = string(ctxt.readValue(p, String.class)); } else if ("value".equals(fieldName)) { value = string(ctxt.readValue(p, String.class)); } break; case JsonTokenId.ID_END_OBJECT: headers.withEntry(key, value); break; default: throw new RuntimeException("Unexpected token: \"" + t + "\" id: \"" + t.id() + "\" text: \"" + p.getText()); } } }
@Override public int getCurrentTokenId() { return currentToken == null ? JsonTokenId.ID_NO_TOKEN : currentToken.id(); }
@Override public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { if (parser.hasTokenId(JsonTokenId.ID_STRING)) { String string = parser.getText().trim(); if (string.length() == 0) { return null; } return LocalDateTime.parse(string, _formatter); } if (parser.isExpectedStartArrayToken()) { if (parser.nextToken() == JsonToken.END_ARRAY) { return null; } int year = parser.getIntValue(); parser.nextToken(); int month = parser.getIntValue(); parser.nextToken(); int day = parser.getIntValue(); parser.nextToken(); int hour = parser.getIntValue(); parser.nextToken(); int minute = parser.getIntValue(); if (parser.nextToken() != JsonToken.END_ARRAY) { int second = parser.getIntValue(); if (parser.nextToken() != JsonToken.END_ARRAY) { int partialSecond = parser.getIntValue(); if (parser.nextToken() != JsonToken.END_ARRAY) { throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end."); } return LocalDateTime.of(year, month, day, hour, minute, second, partialSecond); } return LocalDateTime.of(year, month, day, hour, minute, second); } return LocalDateTime.of(year, month, day, hour, minute); } throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string."); }
@Override public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException { if (_nonStandardCreation || _needViewProcesing) { return super.deserializeFromObject(p, ctxt); } Object bean = null; if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) { String propertyName = p.getCurrentName(); do { p.nextToken(); SettableBeanProperty property = _beanProperties.find(propertyName); if (property == null) { handleUnknownVanilla(p, ctxt, bean, propertyName); continue; } // lazily create the bean, the id property must be the first property if (bean == null) { if (propertyName.equals(_objectIdReader.propertyName.getSimpleName())) { // deserialize id Object id = property.deserialize(p, ctxt); ReadableObjectId objectId = ctxt.findObjectId(id, _objectIdReader.generator, _objectIdReader.resolver); bean = objectId == null ? null : objectId.resolve(); if (bean == null) { bean = _valueInstantiator.createUsingDefault(ctxt); property.set(bean, id); } } else { bean = _valueInstantiator.createUsingDefault(ctxt); } p.setCurrentValue(bean); } property.deserializeAndSet(p, ctxt, bean); } while ((propertyName = p.nextFieldName()) != null); } return bean; }
public void parseDocument(ElementHandler docHandler) throws IOException { push(docHandler); JsonToken tkn = parser.nextToken(); while (null != tkn) { switch(tkn.id()) { case JsonTokenId.ID_START_OBJECT: ElementHandler newHandler = top().newObject(); push(newHandler); break; case JsonTokenId.ID_END_OBJECT: ElementHandler endHandler = top(); endHandler.endObject(); pop(); break; case JsonTokenId.ID_FIELD_NAME: ElementHandler fieldHandler = top(); fieldHandler.fieldName(parser.getText()); break; case JsonTokenId.ID_START_ARRAY: top().newArray(); break; case JsonTokenId.ID_END_ARRAY: top().endArray(); break; case JsonTokenId.ID_STRING: top().withString(parser.getText()); break; case JsonTokenId.ID_NUMBER_INT: top().withBigInteger(parser.getBigIntegerValue()); break; case JsonTokenId.ID_NUMBER_FLOAT: top().withBigDecimal(parser.getDecimalValue()); break; case JsonTokenId.ID_TRUE: top().withBoolean(true); break; case JsonTokenId.ID_FALSE: top().withBoolean(false); break; case JsonTokenId.ID_NULL: top().withNull(); break; default: PushJsonLogger.LOG.warn(fmtToken(tkn, parser.getText())); break; } // Keep going tkn = parser.nextToken(); } }
@Override public EventList deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String project = (String) ctxt.getAttribute("project"); String collection = (String) ctxt.getAttribute("collection"); String apiKey = (String) ctxt.getAttribute("apiKey"); boolean useheader = Boolean.FALSE != ctxt.getAttribute("useHeader"); Map.Entry<List<SchemaField>, int[]> header; if (useheader) { header = readHeader((CsvParser) jp, project, collection); } else { List<SchemaField> vall = metastore.getCollection(project, collection); header = new AbstractMap.SimpleImmutableEntry<>(vall, IntStream.range(0, vall.size()).toArray()); } List<SchemaField> fields = header.getKey(); int[] indexes = header.getValue(); List<FieldType> types = Arrays.stream(indexes) .mapToObj(i -> header.getKey().get(i).getType()).collect(Collectors.toList()); Schema schema = convertAvroSchema(fields); GenericData.Record record = new GenericData.Record(schema); int idx = 0; List<Event> list = new ArrayList<>(); while (true) { JsonToken t = jp.nextToken(); if (t == null) { break; } switch (t.id()) { case JsonTokenId.ID_START_ARRAY: idx = 0; record = new GenericData.Record(schema); list.add(new Event(project, collection, null, fields, record)); break; case JsonTokenId.ID_END_ARRAY: continue; default: if (idx >= indexes.length) { throw new RakamException(String.format("Table has %d columns but csv file has more than %d columns", indexes.length, indexes.length), HttpResponseStatus.BAD_REQUEST); } record.put(indexes[idx], getValue(types.get(idx), jp)); idx += 1; break; } } return new EventList(Event.EventContext.apiKey(apiKey), project, list); }
@Override public Object deserialize(JsonParser p, DeserializationContext ctxt, Object bean) throws IOException { p.setCurrentValue(bean); if (_injectables != null) { injectValues(ctxt, bean); } if (_unwrappedPropertyHandler != null) { return deserializeWithUnwrapped(p, ctxt, bean); } if (_externalTypeIdHandler != null) { return deserializeWithExternalTypeId(p, ctxt, bean); } String propName; // 23-Mar-2010, tatu: In some cases, we start with full JSON object too... if (p.isExpectedStartObjectToken()) { propName = p.nextFieldName(); if (propName == null) { return bean; } } else { if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) { propName = p.getCurrentName(); } else { return bean; } } if (_needViewProcesing) { Class<?> view = ctxt.getActiveView(); if (view != null) { return deserializeWithView(p, ctxt, bean, view); } } do { p.nextToken(); SettableBeanProperty prop = _beanProperties.find(propName); if (prop != null) { // normal case try { MigrationCurrentInfo migrationCurrentInfo = new MigrationCurrentInfo(bean, propName); migrationCurrentStack.addState(migrationCurrentInfo); if (Collection.class.isAssignableFrom(prop.getType().getRawClass()) || AbstractEntity.class.isAssignableFrom(prop.getType().getRawClass())) { //we need customization here //was prop.deserializeAndSet(p, ctxt, bean); //we did this as we do not want jackson to manipulate refernces prop.deserialize(p, ctxt); } else { prop.deserializeAndSet(p, ctxt, bean); } } catch (Exception e) { wrapAndThrow(e, bean, propName, ctxt); } finally { migrationCurrentStack.popState(); } continue; } handleUnknownVanilla(p, ctxt, bean, propName); } while ((propName = p.nextFieldName()) != null); return bean; }
public LocalDateTime deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { if (parser.hasTokenId(JsonTokenId.ID_STRING)) { String date = parser.getText().trim(); if (date.isEmpty()) { return null; } return LocalDateTime.ofInstant(Instant.parse(date), ZoneOffset.UTC); } return null; }