@Override protected void read(ImpExt.Builder ext, JsonParser par) throws IOException { switch (getCurrentName(par)) { case "billing_id": for (startArray(par); endArray(par); par.nextToken()) { ext.addBillingId(par.getLongValue()); } break; case "publisher_settings_list_id": for (startArray(par); endArray(par); par.nextToken()) { ext.addPublisherSettingsListId(par.getLongValue()); } break; case "allowed_vendor_type": for (startArray(par); endArray(par); par.nextToken()) { ext.addAllowedVendorType(par.getIntValue()); } break; case "publisher_parameter": for (startArray(par); endArray(par); par.nextToken()) { ext.addPublisherParameter(par.getText()); } break; } }
private void handleNewMembership(String path, SqlSession sql, JsonParser input) throws IOException, EngineException { JsonUtils.nextExpect(input, "entity"); long entityId = input.getLongValue(); JsonUtils.nextExpect(input, "groupId"); input.getLongValue(); // we ignore this JsonUtils.nextExpect(input, "contents"); byte[] contents = input.getBinaryValue(); GroupMembership parsed = groupMembershipSerializer.fromJson(contents, entityId, path); dbGroups.addMemberFromParent( path, new EntityParam(entityId), parsed.getRemoteIdp(), parsed.getTranslationProfile(), parsed.getCreationTs(), sql); JsonUtils.nextExpect(input, JsonToken.END_OBJECT); }
@Override public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); return ISODateTimeFormat.dateTimeParser().parseDateTime(str).toLocalDate(); } if (t == JsonToken.VALUE_NUMBER_INT) { return new LocalDate(jp.getLongValue()); } throw ctxt.mappingException(handledType()); }
/** * Unmarshal. * * @param parser the parser * @param instantNull the instant null * @param expectedClass the expected class * @return the time instant * @throws IOException Signals that an I/O exception has occurred. */ public static TimeInstant unmarshal( JsonParser parser, @Nullable TimeInstant instantNull, Class<TimeInstant> expectedClass) throws IOException { JsonToken token = parser.getCurrentToken(); if (token == JsonToken.VALUE_EMBEDDED_OBJECT) { Date date = (Date) parser.getEmbeddedObject(); return TimeInstant.of(date.getTime()); } return TimeInstant.of(parser.getLongValue()); }
@Override public Date deserialize( final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException { final JsonToken token = jsonParser.getCurrentToken(); switch (token) { case VALUE_NUMBER_FLOAT: return new Date(jsonParser.getDecimalValue().movePointRight(3).longValue()); case VALUE_NUMBER_INT: return new Date(jsonParser.getLongValue() * 1000L); default: return new DateDeserializers.DateDeserializer() .deserialize(jsonParser, deserializationContext); } }
private Integer rangeCheckedInteger(JsonParser parser, DeserializationContext context) throws IOException { long l = parser.getLongValue(); if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { throw context.weirdStringException( _valueClass, "Over/underflow: numeric value (" + l + ") out of range of Integer (" + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE + ")"); } return Integer.valueOf((int) l); }
@Override public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); try { Date date = new SimpleDateFormat(PATTERN).parse(str); return new DateTime(date); } catch (ParseException e) { throw new RuntimeException(e); } } if (t == JsonToken.VALUE_NUMBER_INT) { return new DateTime(jp.getLongValue()); } throw ctxt.mappingException(handledType()); }
public streamParser(CommandLine line) { try { JsonFactory jfactory = new JsonFactory(); JsonParser jParser = null; if (line.hasOption("f")) { jParser = jfactory.createJsonParser(new File(line.getOptionValue("file"))); } else { jParser = jfactory.createJsonParser(System.in); } String idElement = null; String idValue = null; if (line.hasOption("key")) idElement = line.getOptionValue("key"); String[] remainingArguments = line.getArgs(); if (remainingArguments.length != 1) { throw new IOException("table name not provided or too many table arguments"); } Table table = MapRDB.getTable(remainingArguments[0]); // get the table DocumentBuilder b = MapRDB.newDocumentBuilder(); while (jParser.nextToken() != null) { String fieldName = jParser.getCurrentName(); switch (jParser.getCurrentToken()) { case END_ARRAY: depth--; b.endArray(); break; case END_OBJECT: b.endMap(); depth--; // When the depth reaches zero on an end of object, this means we // have constructed a complete JSON object in the DocumentBuilder. // At this point, we can call insert() or insertandreplace() if (depth == 0) { System.out.println(b.getDocument().asJsonString()); if (idElement != null) table.insert(idValue, b.getDocument()); else table.insert(b.getDocument()); } break; case START_ARRAY: if (fieldName == null) { b.addNewArray(); } else { b.putNewArray(fieldName); } depth++; break; case START_OBJECT: if (fieldName == null) { b.addNewMap(); } else { b.putNewMap(fieldName); } depth++; break; // Not sure about these guys case FIELD_NAME: case NOT_AVAILABLE: case VALUE_EMBEDDED_OBJECT: break; // These actually add things to the array or object case VALUE_NULL: if (fieldName != null) { b.putNull(fieldName); } else { b.addNull(); } break; case VALUE_NUMBER_FLOAT: if (fieldName != null) { b.put(fieldName, jParser.getDoubleValue()); } else { b.add(jParser.getDoubleValue()); } break; case VALUE_NUMBER_INT: if (fieldName != null) { b.put(fieldName, jParser.getLongValue()); } else { b.add(jParser.getLongValue()); } break; case VALUE_STRING: if (fieldName != null) { if (fieldName.equals(idElement)) idValue = jParser.getText(); b.put(fieldName, jParser.getText()); } else { b.add(jParser.getText()); } break; case VALUE_FALSE: case VALUE_TRUE: if (fieldName != null) { b.put(fieldName, jParser.getBooleanValue()); } else { b.add(jParser.getBooleanValue()); } break; } System.out.println( "[" + depth + "] " + jParser.getCurrentToken().toString() + ": " + fieldName); // display mkyong } jParser.close(); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private void parseField( List<String> fieldNames, List<ColumnMetaData.Rep> fieldTypes, Row.RowBuilder rowBuilder, JsonParser parser) throws IOException { final String fieldName = parser.getCurrentName(); // Move to next token, which is name's value JsonToken token = parser.nextToken(); int i = fieldNames.indexOf(fieldName); if (i < 0) { return; } ColumnMetaData.Rep type = fieldTypes.get(i); switch (token) { case VALUE_NUMBER_INT: if (type == null) { type = ColumnMetaData.Rep.INTEGER; } // fall through case VALUE_NUMBER_FLOAT: if (type == null) { type = ColumnMetaData.Rep.FLOAT; } switch (type) { case BYTE: rowBuilder.set(i, parser.getByteValue()); break; case SHORT: rowBuilder.set(i, parser.getShortValue()); break; case INTEGER: rowBuilder.set(i, parser.getIntValue()); break; case LONG: rowBuilder.set(i, parser.getLongValue()); break; case FLOAT: rowBuilder.set(i, parser.getFloatValue()); break; case DOUBLE: rowBuilder.set(i, parser.getDoubleValue()); break; } break; case VALUE_TRUE: rowBuilder.set(i, true); break; case VALUE_FALSE: rowBuilder.set(i, false); break; case VALUE_NULL: break; case VALUE_STRING: default: if (type == ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP) { try { final Date parse = UTC_TIMESTAMP_FORMAT.parse(parser.getText()); rowBuilder.set(i, parse.getTime()); } catch (ParseException e) { // ignore bad value } } else { rowBuilder.set(i, parser.getText()); } break; } }
private String parseItemList(Reader in, IItemListHandler handler) throws JsonParseException, IOException, RemoteException { JsonFactory f = new JsonFactory(); JsonParser jp = f.createParser(in); long length = 0; String currName; String mediaUrl = null; String mediaType = null; IItem entry = null; String continuation = null; List<IItem> itemList = new ArrayList<IItem>(); List<String> excludedSubs = handler.excludedStreams(); // excluded subscriptions jp.nextToken(); // will return JsonToken.START_OBJECT (verify?) while (jp.nextToken() != JsonToken.END_OBJECT) { currName = jp.getCurrentName(); jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY if ("continuation".equals(currName)) { continuation = jp.getText(); } else if ("items".equals(currName)) { // contains an object // start items while (jp.nextToken() != JsonToken.END_ARRAY) { // request stop // if (handler.requestStop()) throw new JsonParseException(null, null); if (jp.getCurrentToken() == JsonToken.START_OBJECT) { entry = new IItem(); } else if (jp.getCurrentToken() == JsonToken.END_OBJECT) { if (entry != null && entry.uid.length() > 0) { if (length + entry.getLength() > MAX_TRANSACTION_LENGTH) { handler.items(itemList, STRATEGY_INSERT_DEFAULT); itemList.clear(); length = 0; } itemList.add(entry); length += entry.getLength(); } if (itemList.size() % 200 == 0 || length > MAX_TRANSACTION_LENGTH) { // avoid TransactionTooLargeException, android only // allows 1mb handler.items(itemList, STRATEGY_INSERT_DEFAULT); itemList.clear(); length = 0; } entry = null; } currName = jp.getCurrentName(); if (currName == null || entry == null) continue; jp.nextToken(); // move to value if (currName.equals("id")) { entry.uid = stripItemUid(jp.getText()); } else if (currName.equals("crawlTimeMsec")) { entry.updatedTime = Long.valueOf(jp.getText()) / 1000; } else if (currName.equals("title")) { entry.title = Utils.stripTags(unEscapeEntities(jp.getText()), true); } else if (currName.equals("categories")) { while (jp.nextToken() != JsonToken.END_ARRAY) { String category = jp.getText(); if (category != null && addUserLabel(category, entry)) { entry.addTag(category); } if (category != null && category.endsWith("/state/com.google/read")) { entry.read = true; } } } else if (currName.equals("published")) { entry.publishedTime = jp.getLongValue(); } else if (currName.equals("alternate")) { while (jp.nextToken() != JsonToken.END_ARRAY) { currName = jp.getCurrentName(); if (currName == null) continue; jp.nextToken(); if (currName.equals("href")) { entry.link = jp.getText(); } else { jp.skipChildren(); } } } else if (currName.equals("enclosure")) { while (jp.nextToken() != JsonToken.END_ARRAY) { currName = jp.getCurrentName(); if (currName == null) continue; jp.nextToken(); if (currName.equals("href")) { mediaUrl = jp.getText(); } else if (currName.equals("type")) { mediaType = jp.getText(); if (mediaType.startsWith("image")) { entry.addImage(mediaUrl, mediaType); } else if (mediaType.startsWith("video")) { entry.addVideo(mediaUrl, mediaType); } else if (mediaType.startsWith("audio")) { entry.addAudio(mediaUrl, mediaType); } mediaUrl = null; mediaType = null; } else { jp.skipChildren(); } } } else if (currName.equals("summary") || currName.equals("content")) { while (jp.nextToken() != JsonToken.END_OBJECT) { currName = jp.getCurrentName(); if (currName == null) continue; jp.nextToken(); if (currName.equals("content")) { entry.content = unEscapeEntities(jp.getText()); } else { jp.skipChildren(); } } } else if (currName.equals("author")) { entry.author = jp.getText(); } else if (currName.equals("origin")) { while (jp.nextToken() != JsonToken.END_OBJECT) { currName = jp.getCurrentName(); if (currName == null) continue; jp.nextToken(); if (currName.equals("streamId")) { String streamId = jp.getText(); if (streamId != null && (excludedSubs == null || !excludedSubs.contains(streamId))) { entry.subUid = streamId; } else entry = null; } else { jp.skipChildren(); } } } else { jp.skipChildren(); } } handler.items(itemList, STRATEGY_INSERT_DEFAULT); itemList.clear(); } else { jp.skipChildren(); } } return continuation; }
private void handleLegacyMembership(String path, SqlSession sql, JsonParser input) throws IOException, EngineException { long memberId = input.getLongValue(); dbGroups.addMemberFromParent(path, new EntityParam(memberId), null, null, new Date(), sql); }
@Override public Record deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String id = null; String type = null; Date creationDate = null; JsonToken currentToken = null; Map<String, Field> fields = new HashMap<>(); boolean processingFields = false; while ((currentToken = jp.nextValue()) != null) { switch (currentToken) { case START_OBJECT: processingFields = true; break; case END_OBJECT: processingFields = true; break; case VALUE_NUMBER_INT: try { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.INT, jp.getIntValue())); } catch (JsonParseException ex) { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.LONG, jp.getLongValue())); } break; case VALUE_NUMBER_FLOAT: try { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.FLOAT, jp.getFloatValue())); } catch (JsonParseException ex) { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.DOUBLE, jp.getDoubleValue())); } break; case VALUE_FALSE: case VALUE_TRUE: fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.BOOLEAN, jp.getBooleanValue())); break; case START_ARRAY: logger.info(jp.getCurrentName()); break; case END_ARRAY: break; case VALUE_STRING: if (jp.getCurrentName() != null) { switch (jp.getCurrentName()) { case "id": id = jp.getValueAsString(); break; case "type": type = jp.getValueAsString(); break; case "creationDate": try { creationDate = sdf.parse(jp.getValueAsString()); // "Thu Sep 08 12:11:08 CEST 2016\" } catch (ParseException e) { e.printStackTrace(); } break; default: fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.STRING, jp.getValueAsString())); break; } } break; default: break; } } Record record = new StandardRecord(type); record.setId(id); record.setType(type); record.setTime(creationDate); record.setFields(fields); return record; }
/** * Method for copying contents of the current event that the given parser instance points to. Note * that the method <b>will not</b> copy any other events, such as events contained within Json * Array or Object structures. * * <p>Calling this method will not advance the given parser, although it may cause parser to * internally process more data (if it lazy loads contents of value events, for example) */ @Override public void copyCurrentEvent(JsonParser jp) throws IOException, JsonProcessingException { switch (jp.getCurrentToken()) { case START_OBJECT: writeStartObject(); break; case END_OBJECT: writeEndObject(); break; case START_ARRAY: writeStartArray(); break; case END_ARRAY: writeEndArray(); break; case FIELD_NAME: writeFieldName(jp.getCurrentName()); break; case VALUE_STRING: writeString(jp.getText()); break; case VALUE_NUMBER_INT: switch (jp.getNumberType()) { case INT: writeNumber(jp.getIntValue()); break; case BIG_INTEGER: writeNumber(jp.getBigIntegerValue()); break; default: writeNumber(jp.getLongValue()); } break; case VALUE_NUMBER_FLOAT: switch (jp.getNumberType()) { case BIG_DECIMAL: writeNumber(jp.getDecimalValue()); break; case FLOAT: writeNumber(jp.getFloatValue()); break; default: writeNumber(jp.getDoubleValue()); } break; case VALUE_TRUE: writeBoolean(true); break; case VALUE_FALSE: writeBoolean(false); break; case VALUE_NULL: writeNull(); break; case VALUE_EMBEDDED_OBJECT: writeObject(jp.getEmbeddedObject()); break; default: throw new IllegalStateException(); } }