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

The following examples show how to use com.google.gson.stream.JsonWriter#name() . 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: 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 2
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 3
Source File: QuerySolr.java    From nifi with Apache License 2.0 6 votes vote down vote up
private static void addStatsFromSolrResponseToJsonWriter(final QueryResponse response, final JsonWriter writer) throws IOException {
    writer.beginObject();
    writer.name("stats_fields");
    writer.beginObject();
    for (Map.Entry<String,FieldStatsInfo> entry: response.getFieldStatsInfo().entrySet()) {
        FieldStatsInfo fsi = entry.getValue();
        writer.name(entry.getKey());
        writer.beginObject();
        writer.name("min").value(fsi.getMin().toString());
        writer.name("max").value(fsi.getMax().toString());
        writer.name("count").value(fsi.getCount());
        writer.name("missing").value(fsi.getMissing());
        writer.name("sum").value(fsi.getSum().toString());
        writer.name("mean").value(fsi.getMean().toString());
        writer.name("sumOfSquares").value(fsi.getSumOfSquares());
        writer.name("stddev").value(fsi.getStddev());
        writer.endObject();
    }
    writer.endObject();
    writer.endObject();
}
 
Example 4
Source File: CustomMessageEntry.java    From ForgeHax with MIT License 6 votes vote down vote up
@Override
public void serialize(JsonWriter writer) throws IOException {
  writer.beginObject();
  
  writer.name("messages");
  writer.beginArray();
  for (MessageEntry entry : messages) {
    writer.beginObject();
    writer.name(entry.toString());
    entry.serialize(writer);
    writer.endObject();
  }
  writer.endArray();
  
  writer.endObject();
}
 
Example 5
Source File: JsonLdSerializer.java    From schemaorg-java with Apache License 2.0 6 votes vote down vote up
private void writeContextInternal(JsonWriter out, ValueType value) throws IOException {
  if (value == NullValue.VALUE) {
    out.nullValue();
    return;
  }
  if (!(value instanceof JsonLdContext)) {
    throw new JsonLdSyntaxException(String.format("Invalid value of @context: %s", value));
  }
  JsonLdContext context = (JsonLdContext) value;
  if (context.containsBase()) {
    out.beginObject();
    out.name(JsonLdConstants.BASE);
    try {
      String base = context.getBase();
      if (base == null) {
        out.nullValue();
      } else {
        out.value(base);
      }
    } catch (SchemaOrgException e) {
      throw new JsonLdSyntaxException("JSON-LD @base must be single value", e.getCause());
    }
    out.endObject();
  }
}
 
Example 6
Source File: JsonLdSerializer.java    From schemaorg-java with Apache License 2.0 6 votes vote down vote up
private void writeReverse(JsonWriter out, ImmutableMultimap<String, Thing> map)
    throws IOException {
  Set<String> keySet = map.keySet();
  if (keySet.size() == 0) {
    return;
  }
  out.name(JsonLdConstants.REVERSE);
  out.beginObject();
  for (String key : keySet) {
    out.name(shortNameOf(key));
    Collection<Thing> values = map.get(key);
    if (values.size() == 0) {
      throw new JsonLdSyntaxException(
          String.format("JSON-LD reverse property %s has no value", key));
    } else if (values.size() == 1) {
      writeObject(out, (Thing) values.toArray()[0], false /* isTopLevelObject */);
    } else {
      out.beginArray();
      for (Thing value : values) {
        writeObject(out, value, false /* isTopLevelObject */);
      }
      out.endArray();
    }
  }
  out.endObject();
}
 
Example 7
Source File: D.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public void a(JsonWriter jsonwriter, Calendar calendar)
{
    if (calendar == null)
    {
        jsonwriter.nullValue();
        return;
    } else
    {
        jsonwriter.beginObject();
        jsonwriter.name("year");
        jsonwriter.value(calendar.get(1));
        jsonwriter.name("month");
        jsonwriter.value(calendar.get(2));
        jsonwriter.name("dayOfMonth");
        jsonwriter.value(calendar.get(5));
        jsonwriter.name("hourOfDay");
        jsonwriter.value(calendar.get(11));
        jsonwriter.name("minute");
        jsonwriter.value(calendar.get(12));
        jsonwriter.name("second");
        jsonwriter.value(calendar.get(13));
        jsonwriter.endObject();
        return;
    }
}
 
Example 8
Source File: GsonParceler.java    From Mortar-Flow-Dagger2-demo with MIT License 6 votes vote down vote up
private String encode(Object instance) throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);

    try {
        Class<?> type = instance.getClass();

        writer.beginObject();
        writer.name(type.getName());
        gson.toJson(instance, type, writer);
        writer.endObject();

        return stringWriter.toString();
    } finally {
        writer.close();
    }
}
 
Example 9
Source File: ReflectiveTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override public void write(JsonWriter out, T value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }

  out.beginObject();
  try {
    for (BoundField boundField : boundFields.values()) {
      if (boundField.writeField(value)) {
        out.name(boundField.name);
        boundField.write(out, value);
      }
    }
  } catch (IllegalAccessException e) {
    throw new AssertionError();
  }
  out.endObject();
}
 
Example 10
Source File: ReflectiveTypeAdapterFactory.java    From letv with Apache License 2.0 6 votes vote down vote up
public void write(JsonWriter out, T value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    out.beginObject();
    try {
        for (BoundField boundField : this.boundFields.values()) {
            if (boundField.serialized) {
                out.name(boundField.name);
                boundField.write(out, value);
            }
        }
        out.endObject();
    } catch (IllegalAccessException e) {
        throw new AssertionError();
    }
}
 
Example 11
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 12
Source File: PropertyBasedTypeAdapter.java    From graphical-lsp with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeProperty(JsonWriter out, T instance, Field field) throws IOException, IllegalAccessException {
	field.setAccessible(true);
	out.name(field.getName());
	Object value = field.get(instance);
	if (value == null)
		out.nullValue();
	else if (value == instance)
		throw new RuntimeException("Object has a reference to itself.");
	else
		gson.toJson(value, value.getClass(), out);
}
 
Example 13
Source File: SparseArrayTypeAdapter.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, SparseArray<T> value) throws IOException {
    out.beginObject();
    if(value != null) {
        for (int size = value.size(), i = size - 1; i >= 0; i--) {
            out.name(value.keyAt(i) + "");
            mAdapter.write(out, value.valueAt(i));
        }
    }
    out.endObject();
}
 
Example 14
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 15
Source File: MCRRestAPIClassifications.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * output children in JSON format used as input for a jsTree
 *
 * @param eParent - the parent xml element
 * @param writer - the JSON writer
 * @param lang - the language to be filtered or null if all languages should be displayed
 * @param opened - true, if all leaf nodes should be displayed
 * @param disabled - true, if all nodes should be disabled
 * @param selected - true, if all node should be selected
 *
 * @throws IOException
 */
private static void writeChildrenAsJSONJSTree(Element eParent, JsonWriter writer, String lang, boolean opened,
    boolean disabled, boolean selected) throws IOException {
    writer.beginArray();
    for (Element e : eParent.getChildren("category")) {
        writer.beginObject();
        writer.name("id").value(e.getAttributeValue("ID"));
        for (Element eLabel : e.getChildren("label")) {
            if (lang == null || lang.equals(eLabel.getAttributeValue("lang", Namespace.XML_NAMESPACE))) {
                writer.name("text").value(eLabel.getAttributeValue("text"));
            }
        }
        if (opened || disabled || selected) {
            writer.name("state");
            writer.beginObject();
            if (opened) {
                writer.name("opened").value(true);
            }
            if (disabled) {
                writer.name("disabled").value(true);
            }
            if (selected) {
                writer.name("selected").value(true);
            }
            writer.endObject();
        }
        if (e.getChildren("category").size() > 0) {
            writer.name("children");
            writeChildrenAsJSONJSTree(e, writer, lang, opened, disabled, selected);
        }
        writer.endObject();
    }
    writer.endArray();
}
 
Example 16
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 17
Source File: StreamAclJsonAdapter.java    From esjc with MIT License 5 votes vote down vote up
private static void writeRoles(JsonWriter writer, String name, List<String> roles) throws IOException {
    if (roles != null) {
        writer.name(name);
        if (roles.size() == 1) {
            writer.value(roles.get(0));
        } else {
            writer.beginArray();
            for (String role : roles) {
                writer.value(role);
            }
            writer.endArray();
        }
    }
}
 
Example 18
Source File: CondorGenerator.java    From pegasus with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter writer, Profiles node) throws IOException {
    writer.beginObject();
    Metadata m = (Metadata) node.get(Profiles.NAMESPACES.metadata);
    if (!m.isEmpty()) {
        for (Iterator it = m.getProfileKeyIterator(); it.hasNext(); ) {
            String key = (String) it.next();
            writer.name(key);
            writer.value((String) m.get(key));
        }
    }
    writer.endObject();
}
 
Example 19
Source File: InterpreterSetting.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public static String toJson(InterpreterSetting intpSetting) {
  Gson gson = new GsonBuilder().setPrettyPrinting().create();

  StringWriter stringWriter = new StringWriter();
  JsonWriter jsonWriter = new JsonWriter(stringWriter);
  try {
    // id
    jsonWriter.beginObject();
    jsonWriter.name("id");
    jsonWriter.value(intpSetting.getId());

    // name
    jsonWriter.name("name");
    jsonWriter.value(intpSetting.getName());

    // group
    jsonWriter.name("group");
    jsonWriter.value(intpSetting.getGroup());

    // dependencies
    jsonWriter.name("dependencies");
    String jsonDep = gson.toJson(intpSetting.getDependencies(), new TypeToken<List<Dependency>>() {
    }.getType());
    jsonWriter.value(jsonDep);

    // properties
    jsonWriter.name("properties");
    String jsonProps = gson.toJson(intpSetting.getProperties(), new TypeToken<Map<String, InterpreterProperty>>() {
    }.getType());
    jsonWriter.value(jsonProps);

    // interpreterOption
    jsonWriter.name("interpreterOption");
    String jsonOption = gson.toJson(intpSetting.getOption(), new TypeToken<InterpreterOption>() {
    }.getType());
    jsonWriter.value(jsonOption);

    // interpreterGroup
    jsonWriter.name("interpreterGroup");
    String jsonIntpInfos = gson.toJson(intpSetting.getInterpreterInfos(), new TypeToken<List<InterpreterInfo>>() {
    }.getType());
    jsonWriter.value(jsonIntpInfos);

    jsonWriter.endObject();
    jsonWriter.flush();
  } catch (IOException e) {
    LOGGER.error(e.getMessage(), e);
  }

  return stringWriter.getBuffer().toString();
}
 
Example 20
Source File: AutoPlace.java    From ForgeHax with MIT License 4 votes vote down vote up
@Override
public void serialize(JsonWriter writer) throws IOException {
  writer.beginObject();
  
  writer.name("selection");
  writer.beginObject();
  {
    writer.name("item");
    writer.value(getSelection().getItem().getRegistryName().toString());
    
    writer.name("metadata");
    writer.value(getSelection().getMetadata());
  }
  writer.endObject();
  
  writer.name("targets");
  writer.beginArray();
  {
    for (UniqueBlock info : getTargets()) {
      writer.beginObject();
      
      writer.name("block");
      writer.value(info.getBlock().getRegistryName().toString());
      
      writer.name("metadata");
      writer.value(info.getMetadata());
      
      writer.endObject();
    }
  }
  writer.endArray();
  
  writer.name("use");
  writer.value(isUse());
  
  writer.name("whitelist");
  writer.value(isWhitelist());
  
  writer.name("sides");
  writer.beginArray();
  {
    for (EnumFacing side : getSides()) {
      writer.value(side.getName2());
    }
  }
  writer.endArray();
  
  writer.endObject();
}