本文整理汇总了Java中com.squareup.moshi.JsonWriter类的典型用法代码示例。如果您正苦于以下问题:Java JsonWriter类的具体用法?Java JsonWriter怎么用?Java JsonWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonWriter类属于com.squareup.moshi包,在下文中一共展示了JsonWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: displayCurrentSessionData
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
private void displayCurrentSessionData(PrintStream writer) {
Activity activity = activityProvider.getCurrentActivity();
if (activity instanceof SessionDetailsActivity) {
try {
// Use reflection to access private "session" field
Field field = SessionDetailsActivity.class.getDeclaredField("session");
field.setAccessible(true);
Session session = (Session) field.get(activity);
// Convert sessions to a human readable json
Buffer buffer = new Buffer();
JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setIndent(" ");
moshi.adapter(Session.class).toJson(jsonWriter, session);
String sessionJson = buffer.readUtf8();
writer.println(sessionJson);
} catch (Exception e) {
writer.println(e.getMessage());
}
} else {
writer.println("SessionDetailsActivity not visible");
}
}
示例2: writePackageFile
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
/**
* Generates a dummy package.json file to silence npm warnings
*/
private void writePackageFile(File apolloPackageFile) {
try {
JsonWriter writer = JsonWriter.of(Okio.buffer(Okio.sink(apolloPackageFile)));
writer.beginObject();
writer.name("name").value("apollo-android");
writer.name("version").value("0.0.1");
writer.name("description").value("Generates Java code based on a GraphQL schema and query documents. Uses " +
"apollo-codegen under the hood.");
writer.name("name").value("apollo-android");
writer.name("repository");
writer.beginObject();
writer.name("type").value("git");
writer.name("url").value("git+https://github.com/apollostack/apollo-android.git");
writer.endObject();
writer.name("author").value("Apollo");
writer.name("license").value("MIT");
writer.endObject();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
示例3: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
/**
* Recursively writes the respective roots forming a json object that resembles the {@code path}
* wrapping the type of the {@code adapter}.
*/
private static <T> void toJson(JsonAdapter<T> adapter, JsonWriter writer, T value,
String[] path, int index) throws IOException {
if (value != null || writer.getSerializeNulls()) {
if (index == path.length) {
adapter.toJson(writer, value);
} else {
writer.beginObject();
writer.name(path[index]);
toJson(adapter, writer, value, path, ++index);
writer.endObject();
}
} else {
// If we don't propagate the null value the writer will throw.
writer.nullValue();
}
}
示例4: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@Override
public void toJson(final JsonWriter writer, @Nullable final Set<T> value) throws IOException {
if (value == null) {
return;
}
StringBuilder result = new StringBuilder();
for (final T item : value) {
result.append(ProxerUtils.getApiEnumName(item));
result.append(delimiter);
}
if (result.length() > 0) {
result = new StringBuilder(result.substring(0, result.length() - delimiter.length()));
}
writer.value(result.toString());
}
示例5: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@Override
public void toJson(@NonNull JsonWriter writer, RealmList<T> value) throws IOException {
writer.beginArray();
for (T element : value) {
elementAdapter.toJson(writer, element);
}
writer.endArray();
}
示例6: writeConfigurationToFile
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
private void writeConfigurationToFile(HeimdallApplicationConfiguration configuration) {
try {
Buffer buffer = new Buffer();
JsonWriter writer = JsonWriter.of(buffer);
writer.setSerializeNulls(true);
writer.setIndent(" ");
Moshi moshi = new Moshi.Builder().build();
moshi.adapter(HeimdallApplicationConfiguration.class).toJson(writer, configuration);
String json = buffer.readUtf8();
FileUtils.writeStringToFile(new File(HeimdallApplicationConstants.CONFIG_FILE), json);
} catch (Exception e) {
log.error("Failed writing configuration to file.", e);
}
}
示例7: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@Override
public void toJson(JsonWriter writer) throws IOException {
writer.beginObject();
writer.name("id").value(id);
writer.name("description").value(description());
writer.name("type").value(type);
writer.endObject();
}
示例8: errorResponseFor
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@NotNull
private Response errorResponseFor(final Throwable e) {
Buffer buffer = new Buffer();
JsonWriter writer = JsonWriter.of(buffer);
try {
writer.beginObject();
writer.name("error").value(e.getMessage());
writer.endObject();
} catch (IOException ignored) {
}
return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, JSON, buffer.readByteArray());
}
示例9: measurementsToJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
public void measurementsToJson(BufferedSink sink) throws IOException {
long ts = System.currentTimeMillis();
JsonWriter writer = JsonWriter.of(sink);
writer.beginObject();
writer.name("timestamp").value(ts);
writer.name("data");
writer.beginArray();
for (DoubleSupplier m : measurements) {
writer.value(m.getAsDouble());
}
writer.endArray();
writer.endObject();
}
示例10: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
public void toJson(BufferedSink sink) throws IOException {
long ts = System.currentTimeMillis();
JsonWriter writer = JsonWriter.of(sink);
writer.beginObject();
writer.name("type").value("subscription");
writer.name("timestamp").value(ts);
writer.name("descriptions");
writer.beginArray();
for (String d : descriptions) {
writer.value(d);
}
writer.endArray();
writer.endObject();
}
示例11: writeInventory
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@Override
public void writeInventory(BufferedSink sink) throws IOException {
JsonWriter writer = JsonWriter.of(sink);
writer.setIndent(" ");
writer.beginObject();
writer.name("items");
writeItems(writer);
writer.name("measures");
writeMeasures(writer);
writer.endObject();
}
示例12: writeItems
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
void writeItems(JsonWriter writer) throws IOException {
writer.beginArray();
for (int i = 0; i < items.size(); i++) {
writer.beginObject();
writer.name("id").value(i);
writer.name("type").value(items.get(i).type());
writer.name("description").value(items.get(i).description());
writer.endObject();
}
writer.endArray();
}
示例13: writeMeasures
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
void writeMeasures(JsonWriter writer) throws IOException {
Map<String, Set<Measure>> measures = new HashMap<>();
items.forEach(it -> measures.putIfAbsent(it.type(), it.measures()));
writer.beginArray();
for (Entry<String, Set<Measure>> entry : measures.entrySet()) {
writeDeviceMeasures(writer, entry.getKey(), entry.getValue());
}
writer.endArray();
}
示例14: writeDeviceMeasures
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
void writeDeviceMeasures(JsonWriter writer, String type, Set<Measure> measures)
throws IOException {
writer.beginObject();
writer.name("deviceType").value(type);
writer.name("deviceMeasures");
writer.beginArray();
for (Measure m : measures) {
writeMeasure(writer, m);
}
writer.endArray();
writer.endObject();
}
示例15: toJson
import com.squareup.moshi.JsonWriter; //导入依赖的package包/类
@Override
public void toJson(BufferedSink sink) throws IOException {
JsonWriter writer = JsonWriter.of(sink);
writer.setIndent(" ");
writer.beginArray();
for (Item item : items) {
item.toJson(writer);
}
writer.endArray();
}