Example #1
0
 public byte[] toJsonBytes() {
   try {
     ByteArrayBuilder bb = new ByteArrayBuilder(JsonFormat.JSON_FACTORY._getBufferRecycler());
     JsonGenerator gen = JsonFormat.JSON_FACTORY.createJsonGenerator(bb, JsonEncoding.UTF8);
     toJson(gen);
     byte[] result = bb.toByteArray();
     bb.release();
     return result;
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #2
0
 public String toJson() {
   try {
     StringWriter writer = new StringWriter();
     JsonGenerator gen = JsonFormat.JSON_FACTORY.createJsonGenerator(writer);
     toJson(gen);
     return writer.toString();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #3
0
  /** Creates a record event from the json data supplied as bytes. */
  public RecordEvent(byte[] data, IdGenerator idGenerator) throws IOException {
    // Using streaming JSON parsing for performance. We expect the JSON to be correct, validation
    // is absent/minimal.

    JsonParser jp = JsonFormat.JSON_FACTORY.createJsonParser(data);

    JsonToken current;
    current = jp.nextToken();

    if (current != JsonToken.START_OBJECT) {
      throw new RuntimeException("Not a JSON object.");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
      String fieldName = jp.getCurrentName();
      current = jp.nextToken(); // move from field name to field value
      if (fieldName.equals("type")) {
        String messageType = jp.getText();
        if (messageType.equals(Type.CREATE.getName())) {
          type = Type.CREATE;
        } else if (messageType.equals(Type.DELETE.getName())) {
          type = Type.DELETE;
        } else if (messageType.equals(Type.UPDATE.getName())) {
          type = Type.UPDATE;
        } else if (messageType.equals(Type.INDEX.getName())) {
          type = Type.INDEX;
        } else {
          throw new RuntimeException("Unexpected kind of message type: " + messageType);
        }
      } else if (fieldName.equals("tableName")) {
        this.tableName = jp.getText();
      } else if (fieldName.equals("versionCreated")) {
        versionCreated = jp.getLongValue();
      } else if (fieldName.equals("versionUpdated")) {
        versionUpdated = jp.getLongValue();
      } else if (fieldName.equals("recordTypeChanged")) {
        recordTypeChanged = jp.getBooleanValue();
      } else if (fieldName.equals("updatedFields")) {
        if (current != JsonToken.START_ARRAY) {
          throw new RuntimeException("updatedFields is not a JSON array");
        }
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          addUpdatedField(idGenerator.getSchemaId(jp.getBinaryValue()));
        }
      } else if (fieldName.equals("vtagsToIndex")) {
        if (current != JsonToken.START_ARRAY) {
          throw new RuntimeException("vtagsToIndex is not a JSON array");
        }
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          addVTagToIndex(idGenerator.getSchemaId(jp.getBinaryValue()));
        }
      } else if (fieldName.equals("attributes")) {
        if (current != JsonToken.START_OBJECT) {
          throw new RuntimeException("Attributes is not a JSON object");
        }
        this.attributes = new HashMap<String, String>();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
          String key = jp.getCurrentName();
          String value = jp.getText();
          attributes.put(key, value);
        }
      } else if (fieldName.equals("indexFilterData")) {
        this.indexRecordFilterData = new IndexRecordFilterData(jp, idGenerator);
      }
    }
  }