Java Code Examples for com.google.gson.stream.JsonWriter#endObject()

The following examples show how to use com.google.gson.stream.JsonWriter#endObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Car3TypeAdapter.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public void write(JsonWriter writer, Car3 car) throws IOException {
    writer.beginObject();
 
    writer.name("mark").value(car.getMark());
    writer.name("model").value(car.getModel());
    writer.name("type").value(car.getType());
    writer.name("maker").value(car.getMaker());
 
    double costIncludingVAT = car.getCost() + 0.21 * car.getCost();// Add 21% VAT
    writer.name("cost").value(costIncludingVAT);
 
    writer.name("colors");
    writer.beginArray();
    for (String color : car.getColors()) {
        writer.value(color);
    }
    writer.endArray();
    writer.endObject();
}
 
Example 2
Source File: SourceFileJsonTypeAdapter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void write(JsonWriter out, SourceFile src) throws IOException {
    File file = src.getSourceFile();
    String description = src.getDescription();

    if (description == null && file != null) {
        out.value(file.getAbsolutePath());
        return;
    }

    out.beginObject();
    if (description != null) {
        out.name(DESCRIPTION).value(description);
    }
    if (file != null) {
        out.name(PATH).value(file.getAbsolutePath());
    }
    out.endObject();
}
 
Example 3
Source File: SystemSettingsJsonAdapter.java    From esjc with MIT License 6 votes vote down vote up
@Override
public void write(JsonWriter writer, SystemSettings value) throws IOException {
    writer.beginObject();

    if (value.userStreamAcl != null) {
        writer.name(USER_STREAM_ACL);
        streamAclJsonAdapter.write(writer, value.userStreamAcl);
    }

    if (value.systemStreamAcl != null) {
        writer.name(SYSTEM_STREAM_ACL);
        streamAclJsonAdapter.write(writer, value.systemStreamAcl);
    }

    writer.endObject();
}
 
Example 4
Source File: TaskState.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Convert this {@link TaskState} to a json document.
 *
 * @param jsonWriter a {@link com.google.gson.stream.JsonWriter} used to write the json document
 * @throws IOException
 */
public void toJson(JsonWriter jsonWriter, boolean keepConfig) throws IOException {
  jsonWriter.beginObject();

  jsonWriter.name("task id").value(this.getTaskId()).name("task state").value(this.getWorkingState().name())
      .name("start time").value(this.getStartTime()).name("end time").value(this.getEndTime()).name("duration")
      .value(this.getTaskDuration()).name("retry count")
      .value(this.getPropAsInt(ConfigurationKeys.TASK_RETRIES_KEY, 0));

  // Also add failure exception information if it exists. This information is useful even in the
  // case that the task finally succeeds so we know what happened in the course of task execution.
  if (getTaskFailureException().isPresent()) {
    jsonWriter.name("exception").value(getTaskFailureException().get());
  }

  if (keepConfig) {
    jsonWriter.name("properties");
    jsonWriter.beginObject();
    for (String key : this.getPropertyNames()) {
      jsonWriter.name(key).value(this.getProp(key));
    }
    jsonWriter.endObject();
  }

  jsonWriter.endObject();
}
 
Example 5
Source File: JsonTestUtils.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void writeFeature(Feature feature, JsonWriter jsonWriter) throws IOException {
	jsonWriter.beginObject();

	String name = feature.getName();
	Float value = feature.getValue();

	if (StringUtils.isBlank(name) == false) {
		jsonWriter.name(StructuredJsonPropertyNames.FEATURE_NAME_PROPERTY).value(name);
	}

	if (value != null) {
		jsonWriter.name(StructuredJsonPropertyNames.FEATURE_VALUE_PROPERTY).value(value);
	}

	jsonWriter.endObject();
}
 
Example 6
Source File: ObjectTypeAdapter.java    From gson with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, Object value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }

  TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
  if (typeAdapter instanceof ObjectTypeAdapter) {
    out.beginObject();
    out.endObject();
    return;
  }

  typeAdapter.write(out, value);
}
 
Example 7
Source File: IrisObjectTypeAdapter.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, Object value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }

  TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
  if (typeAdapter instanceof IrisObjectTypeAdapter) {
    out.beginObject();
    out.endObject();
    return;
  }

  typeAdapter.write(out, value);
}
 
Example 8
Source File: MessageJsonSerializer.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void write(JsonWriter out, Message message) throws IOException {
    out.beginObject()
            .name(KIND).value(KIND_STRING_ENUM_MAP.get(message.getKind()))
            .name(TEXT).value(message.getText())
            .name(SOURCE_FILE_POSITIONS).beginArray();
    for (SourceFilePosition position : message.getSourceFilePositions()) {
        mSourceFilePositionTypeAdapter.write(out, position);
    }
    out.endArray();
    if (!message.getRawMessage().equals(message.getText())) {
        out.name(RAW_MESSAGE).value(message.getRawMessage());
    }
    if (message.getToolName().isPresent()) {
        out.name(TOOL_NAME).value(message.getToolName().get());
    }
    out.endObject();
}
 
Example 9
Source File: TransactionsTypeAdapter.java    From greenbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter writer, Transactions value) throws IOException {
	checkNotNull(value);

	writer.beginObject();
	writer.name(NAME_TRANSACTIONS);
	writer.beginArray();
	for (Transaction txn : value.getTransactions()) {
		writer.value(toEntryptedString(txn));
	}
	writer.endArray();
	writer.endObject();
}
 
Example 10
Source File: KeyProvider.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize the metadata to a set of bytes.
 * @return the serialized bytes
 * @throws IOException
 */
protected byte[] serialize() throws IOException {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  JsonWriter writer = new JsonWriter(
      new OutputStreamWriter(buffer, Charsets.UTF_8));
  try {
    writer.beginObject();
    if (cipher != null) {
      writer.name(CIPHER_FIELD).value(cipher);
    }
    if (bitLength != 0) {
      writer.name(BIT_LENGTH_FIELD).value(bitLength);
    }
    if (created != null) {
      writer.name(CREATED_FIELD).value(created.getTime());
    }
    if (description != null) {
      writer.name(DESCRIPTION_FIELD).value(description);
    }
    if (attributes != null && attributes.size() > 0) {
      writer.name(ATTRIBUTES_FIELD).beginObject();
      for (Map.Entry<String, String> attribute : attributes.entrySet()) {
        writer.name(attribute.getKey()).value(attribute.getValue());
      }
      writer.endObject();
    }
    writer.name(VERSIONS_FIELD).value(versions);
    writer.endObject();
    writer.flush();
  } finally {
    writer.close();
  }
  return buffer.toByteArray();
}
 
Example 11
Source File: AbstractEntityWithIdJsonAdapter.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, AbstractEntityWithId value) throws IOException{
	if (value != null) {
		out.beginObject();
		out.name(ENTITY_TYPE).value(value.getClass().getName());
		out.name(ID).value(value.getId());
		out.endObject();
	}
}
 
Example 12
Source File: ExecuteCommandParamsTypeAdapter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, ExecuteCommandParams value) throws IOException {
	out.beginObject();
	out.name("command");
	out.value(value.getCommand());
	out.name("arguments");
	out.beginArray();
	for (Object argument : value.getArguments()) {
		gson.toJson(argument, argument.getClass(), out);
	}
	out.endArray();
	out.endObject();
}
 
Example 13
Source File: VersionedTextDocumentIdentifierTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
public void write(final JsonWriter out, final VersionedTextDocumentIdentifier value) throws IOException {
  if (value == null) {
  	out.nullValue();
  	return;
  }
  
  out.beginObject();
  out.name("version");
  writeVersion(out, value.getVersion());
  out.name("uri");
  writeUri(out, value.getUri());
  out.endObject();
}
 
Example 14
Source File: ReportExporter.java    From synthea with Apache License 2.0 5 votes vote down vote up
private static void reportParameters(JsonWriter writer) throws IOException {
  writer.name("run-parameters").beginObject();
  for (String key : Config.allPropertyNames()) {
    writer.name(key).value(Config.get(key, ""));
  }
  writer.endObject(); // run-parameters
}
 
Example 15
Source File: WorkerProcessProtocolZero.java    From buck with Apache License 2.0 5 votes vote down vote up
private static void sendHandshake(JsonWriter writer, int messageId) throws IOException {
  writer.beginArray();
  writer.beginObject();
  writer.name("id").value(messageId);
  writer.name("type").value(TYPE_HANDSHAKE);
  writer.name("protocol_version").value(PROTOCOL_VERSION);
  writer.name("capabilities").beginArray().endArray();
  writer.endObject();
  writer.flush();
}
 
Example 16
Source File: SymbolInformationTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeLocation(final JsonWriter out, final Location value) throws IOException {
  if ((value == null)) {
    out.nullValue();
    return;
  }
  out.beginObject();
  out.name("uri");
  out.value(value.getUri());
  out.name("range");
  this.writeRange(out, value.getRange());
  out.endObject();
}
 
Example 17
Source File: AutoPlaylistRule.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, AutoPlaylistRule rule) throws IOException {
    out.beginObject();
    out.name("type").value(rule.getType());
    out.name("match").value(rule.getMatch());
    out.name("field").value(rule.getField());
    out.name("value").value(rule.getValue());
    out.endObject();
}
 
Example 18
Source File: DataTypeAdaptor.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, JsContractParamsModel value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    out.beginObject();
    out.name("nameOrId");
    gson.getAdapter(String.class).write(out, value.nameOrId);
    out.name("functionName");
    gson.getAdapter(String.class).write(out, value.functionName);
    out.name("valueList");
    gson.getAdapter(List.class).write(out, value.valueList);
    out.endObject();
}
 
Example 19
Source File: SentryEnvelopeHeaderAdapter.java    From sentry-android with MIT License 5 votes vote down vote up
@Override
public void write(JsonWriter writer, SentryEnvelopeHeader value) throws IOException {
  if (value == null) {
    writer.nullValue();
    return;
  }
  writer.beginObject();

  if (value.getEventId() != null) {
    writer.name("event_id");
    writer.value(value.getEventId().toString());
  }

  writer.endObject();
}
 
Example 20
Source File: UsersStreamSerializer.java    From java-json-benchmark with MIT License 4 votes vote down vote up
private void gson(final JsonWriter j, final User u) throws IOException {
    j.beginObject();
    if (u._id != null) {
        j.name("_id");
        j.value(u._id);
    }
    j.name("index");
    j.value(u.index);
    if (u.guid != null) {
        j.name("guid");
        j.value(u.guid);
    }
    j.name("isActive");
    j.value(u.isActive);
    if (u.balance != null) {
        j.name("balance");
        j.value(u.balance);
    }
    if (u.picture != null) {
        j.name("picture");
        j.value(u.picture);
    }
    j.name("age");
    j.value(u.age);
    if (u.eyeColor != null) {
        j.name("eyeColor");
        j.value(u.eyeColor);
    }
    if (u.name != null) {
        j.name("name");
        j.value(u.name);
    }
    if (u.gender != null) {
        j.name("gender");
        j.value(u.gender);
    }
    if (u.company != null) {
        j.name("company");
        j.value(u.company);
    }
    if (u.email != null) {
        j.name("email");
        j.value(u.email);
    }
    if (u.phone != null) {
        j.name("phone");
        j.value(u.phone);
    }
    if (u.address != null) {
        j.name("address");
        j.value(u.address);
    }
    if (u.about != null) {
        j.name("about");
        j.value(u.about);
    }
    if (u.registered != null) {
        j.name("registered");
        j.value(u.registered);
    }
    j.name("latitude");
    j.value(u.latitude);
    j.name("longitude");
    j.value(u.longitude);
    if (u.tags != null) {
        j.name("tags");
        j.beginArray();
        for (String t : u.tags) {
            j.value(t);
        }
        j.endArray();
    }
    if (u.friends != null) {
        j.name("friends");
        j.beginArray();
        for (Friend f : u.friends) {
            j.beginObject();
            j.name("id");
            j.value(f.id);
            j.name("name");
            j.value(f.name);
            j.endObject();
        }
        j.endArray();
    }
    if (u.greeting != null) {
        j.name("greeting");
        j.value(u.greeting);
    }
    if (u.favoriteFruit != null) {
        j.name("favoriteFruit");
        j.value(u.favoriteFruit);
    }
    j.endObject();
}