public static RoomLocationReturn fromServerJson(JsonParser p)
      throws JsonParseException, IOException {

    RoomLocationReturn ret = new RoomLocationReturn();

    RoomLocationEntry entry = new RoomLocationEntry();

    while (p.nextToken() != JsonToken.END_OBJECT) {

      String fieldName = p.getCurrentName();
      if (KEY_BUILDING.equals(fieldName)) {
        p.nextToken();
        entry.setBuildingName(p.getText());
      } else if (KEY_ROOM.equals(fieldName)) {
        p.nextToken();
        entry.setRoomName(p.getText());
      } else if (KEY_LATITUDE.equals(fieldName)) {
        p.nextToken();
        entry.setLatitude(p.getDoubleValue());
      } else if (KEY_LONGITUDE.equals(fieldName)) {
        p.nextToken();
        entry.setLongitude(p.getDoubleValue());
      } else if (KEY_RATING.equals(fieldName)) {
        p.nextToken();
        // We assume that rating <--> productivity (they are interchangeable)
        entry.setProductivity(p.getDoubleValue());
      } else if (KEY_CAPACITY.equals(fieldName)) {
        p.nextToken();
        entry.setCapacity(p.getDoubleValue());
      } else if (KEY_CROWD.equals(fieldName)) {
        p.nextToken();
        entry.setAglCrowd(p.getDoubleValue());
      } else if (KEY_NUM_SURVEYS.equals(fieldName)) {
        p.nextToken();
        entry.setAglSurveys(p.getIntValue());

      } else if (KEY_INTERPOLATED.equals(fieldName)) {
        p.nextToken(); // take the "[" token
        List<NoiseEntry> noises = new ArrayList<NoiseEntry>();
        while (p.nextToken() != JsonToken.END_ARRAY) {
          noises.add(NoiseEntry.fromServerJson(p, entry));
        }
        ret.noises = noises;
      } else if (KEY_COMMENTS.equals(fieldName)) {
        p.nextToken(); // take the "[" token
        List<CommentEntry> comments = new ArrayList<CommentEntry>();
        while (p.nextToken() != JsonToken.END_ARRAY) {
          comments.add(CommentEntry.fromServerJson(p, entry));
        }
        ret.comments = comments;
      }
    }
    entry.setLocal(false);

    ret.entry = entry;

    return ret;
  }
  private void verifyFromMap(String input) throws Exception {
    JsonParser p = new JsonFactory().createParser(input);
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD4, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(TEXT2, getAndVerifyText(p));

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD3, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(-1, p.getIntValue());

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD2, getAndVerifyText(p));
    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    assertEquals(JsonToken.END_ARRAY, p.nextToken());

    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(FIELD1, getAndVerifyText(p));
    assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0);

    assertEquals(JsonToken.END_OBJECT, p.nextToken());

    assertNull(p.nextToken());
    p.close();
  }
  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;
    }
  }
示例#5
0
    @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();
   }
 }