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

The following examples show how to use com.google.gson.stream.JsonWriter#endArray() . 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: y.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public void a(JsonWriter jsonwriter, BitSet bitset)
{
    if (bitset == null)
    {
        jsonwriter.nullValue();
        return;
    }
    jsonwriter.beginArray();
    int i = 0;
    while (i < bitset.length()) 
    {
        int j;
        if (bitset.get(i))
        {
            j = 1;
        } else
        {
            j = 0;
        }
        jsonwriter.value(j);
        i++;
    }
    jsonwriter.endArray();
}
 
Example 2
Source File: MCRRestAPIClassifications.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * output children in JSON format used as input for Dijit Checkbox Tree
 *
 * @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
 *
 * @throws IOException
 */
private static void writeChildrenAsJSONCBTree(Element eParent, JsonWriter writer, String lang, boolean checked)
    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"));
            }
        }
        writer.name("checked").value(checked);
        if (e.getChildren("category").size() > 0) {
            writer.name("children");
            writeChildrenAsJSONCBTree(e, writer, lang, checked);
        }
        writer.endObject();
    }
    writer.endArray();
}
 
Example 3
Source File: ListOfListOfPointCoordinatesTypeAdapter.java    From mapbox-java with MIT License 6 votes vote down vote up
@Override
public void write(JsonWriter out, List<List<Point>> points) throws IOException {

  if (points == null) {
    out.nullValue();
    return;
  }

  out.beginArray();

  for (List<Point> listOfPoints : points) {

    out.beginArray();

    for (Point point : listOfPoints) {
      writePoint(out, point);
    }
    out.endArray();
  }

  out.endArray();
}
 
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: ListOfPointCoordinatesTypeAdapter.java    From mapbox-java with MIT License 6 votes vote down vote up
@Override
public void write(JsonWriter out, List<Point> points) throws IOException {

  if (points == null) {
    out.nullValue();
    return;
  }

  out.beginArray();

  for (Point point : points) {
    writePoint(out, point);
  }

  out.endArray();
}
 
Example 6
Source File: JsonCodec.java    From denominator with Apache License 2.0 6 votes vote down vote up
<T> MockResponse toJsonArray(Iterator<T> elements) {
  elements.hasNext(); // defensive to make certain error cases eager.

  StringWriter out = new StringWriter(); // MWS cannot do streaming responses.
  try {
    JsonWriter writer = new JsonWriter(out);
    writer.setIndent("  ");
    writer.beginArray();
    while (elements.hasNext()) {
      Object next = elements.next();
      json.toJson(next, next.getClass(), writer);
    }
    writer.endArray();
    writer.flush();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return new MockResponse().setResponseCode(200)
      .addHeader("Content-Type", "application/json")
      .setBody(out.toString() + "\n"); // curl nice
}
 
Example 7
Source File: TypeHandler.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public void write(JsonWriter out, GsonProperty property, Object value) throws IOException {
    final Class<?> simpleType = property.getType();
    out.beginArray();
    int length = Array.getLength(value);
    if(length > 0) {
        if (simpleType.isPrimitive() || isBoxedClass(simpleType)) {
            for (int i = 0; i < length; i++) {
                writePrimitiveOrItsBox(out, simpleType, Array.get(value, i));
            }
        } else {
            TypeAdapter adapter = getTypeAdapter(simpleType);
            for (int i = 0; i < length; i++) {
                adapter.write(out, Array.get(value, i));
            }
        }
    }
    out.endArray();
}
 
Example 8
Source File: SpamEntry.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
public void serialize(JsonWriter writer) throws IOException {
  writer.beginObject();
  
  writer.name("enabled");
  writer.value(enabled);
  
  writer.name("keyword");
  writer.value(keyword);
  
  writer.name("type");
  writer.value(type.name());
  
  writer.name("trigger");
  writer.value(trigger.name());
  
  writer.name("delay");
  writer.value(getDelay());
  
  writer.name("messages");
  writer.beginArray();
  for (String msg : messages) {
    writer.value(msg);
  }
  writer.endArray();
  
  writer.endObject();
}
 
Example 9
Source File: JsonTestUtils.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void writeNamespace(Namespace namespace, JsonWriter jsonWriter) throws IOException {
	jsonWriter.beginObject();

	String name = namespace.getName();
	Float scale = namespace.getScalingFactor();

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

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

	Iterable<Feature> features = namespace.getFeatures();

	if (features != null) {
		jsonWriter.name(StructuredJsonPropertyNames.NAMESPACE_FEATURES_PROPERTY);

		jsonWriter.beginArray();

		for (Feature feature : features) {
			writeFeature(feature, jsonWriter);
		}

		jsonWriter.endArray();
	}

	jsonWriter.endObject();
}
 
Example 10
Source File: MessagePart.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public void writeJson(JsonWriter json) {
    try {
        json.beginObject();
        text.writeJson(json);
        json.name("color").value(color.name().toLowerCase());
        for (final ChatColor style : styles) {
            json.name(stylesToNames.get(style)).value(true);
        }
        if (clickActionName != null && clickActionData != null) {
            json.name("clickEvent")
                    .beginObject()
                    .name("action").value(clickActionName)
                    .name("value").value(clickActionData)
                    .endObject();
        }
        if (hoverActionName != null && hoverActionData != null) {
            json.name("hoverEvent")
                    .beginObject()
                    .name("action").value(hoverActionName)
                    .name("value");
            hoverActionData.writeJson(json);
            json.endObject();
        }
        if (insertionData != null) {
            json.name("insertion").value(insertionData);
        }
        if (translationReplacements.size() > 0 && text != null && TextualComponent.isTranslatableText(text)) {
            json.name("with").beginArray();
            for (JsonRepresentedObject obj : translationReplacements) {
                obj.writeJson(json);
            }
            json.endArray();
        }
        json.endObject();
    } catch (IOException e) {
        Bukkit.getLogger().log(Level.WARNING, "A problem occured during writing of JSON string", e);
    }
}
 
Example 11
Source File: LaYourCollectionTypeAdapterFactory.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, Iterable<E> collection) throws IOException {
    if (collection == null) {
        out.nullValue();
        return;
    }

    out.beginArray();
    for (E element : collection) {
        elementTypeAdapter.write(out, element);
    }
    out.endArray();
}
 
Example 12
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 13
Source File: NukkitRegistryDumper.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(final JsonWriter out, final Vector3 vec) throws IOException {
    out.beginArray();
    out.value(vec.getX());
    out.value(vec.getY());
    out.value(vec.getZ());
    out.endArray();
}
 
Example 14
Source File: CollectionGsonAdaptable.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, Collection<?> collection) throws IOException {
    if (gsonOption.isListNullToEmptyWriting()) {
        if (collection == null) {
            out.beginArray();
            out.endArray();
            return; // []
        }
    }
    embedded.write(out, collection);
}
 
Example 15
Source File: PolymorphicTypeAdapterFactory.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, T value) throws IOException {
   if(value == null) {
      out.nullValue();
      return;
   }
   
   out.beginArray();
   out.value(typeOf(value));
   gson.getDelegateAdapter(PolymorphicTypeAdapterFactory.this, token).write(out, value);
   out.endArray();
}
 
Example 16
Source File: StringArrayAdapter.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, List<String> value) throws IOException {
    out.beginArray();
    for(String str : value){
        out.value(str);
    }
    out.endArray();
}
 
Example 17
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();
}
 
Example 18
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();
}
 
Example 19
Source File: DefaultModule.java    From proteus with Apache License 2.0 4 votes vote down vote up
@Override
public CustomValueTypeAdapter<DrawableValue.LayerListValue> create(int type, final ProteusTypeAdapterFactory factory) {
  return new CustomValueTypeAdapter<DrawableValue.LayerListValue>(type) {

    private static final String KEY_IDS = "i";
    private static final String KEY_LAYERS = "l";

    @Override
    public void write(JsonWriter out, DrawableValue.LayerListValue value) throws IOException {

      out.beginObject();

      out.name(KEY_IDS);
      Iterator<Integer> i = value.getIds();
      out.beginArray();
      while (i.hasNext()) {
        out.value(i.next());
      }
      out.endArray();

      out.name(KEY_LAYERS);
      Iterator<Value> l = value.getLayers();
      out.beginArray();
      while (l.hasNext()) {
        factory.COMPILED_VALUE_TYPE_ADAPTER.write(out, l.next());
      }
      out.endArray();

      out.endObject();
    }

    @Override
    public DrawableValue.LayerListValue read(JsonReader in) throws IOException {

      in.beginObject();

      in.nextName();
      int[] ids = new int[0];
      in.beginArray();
      while (in.hasNext()) {
        ids = Arrays.copyOf(ids, ids.length + 1);
        ids[ids.length - 1] = Integer.parseInt(in.nextString());
      }
      in.endArray();

      in.nextName();
      Value[] layers = new Value[0];
      in.beginArray();
      while (in.hasNext()) {
        layers = Arrays.copyOf(layers, layers.length + 1);
        layers[layers.length - 1] = factory.COMPILED_VALUE_TYPE_ADAPTER.read(in);
      }
      in.endArray();

      in.endObject();

      return DrawableValue.LayerListValue.valueOf(ids, layers);
    }
  };
}
 
Example 20
Source File: JsonTestUtils.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
public static void writeExample(JsonWriter jsonWriter, StructuredExample structuredExample) throws IOException {

		jsonWriter.beginObject();

		String label = structuredExample.getLabel();

		//always write the label out, this is how a pipe example is distinguished from an empty example.
		jsonWriter.name(StructuredJsonPropertyNames.EXAMPLE_LABEL_PROPERTY);

		if (StringUtils.isBlank(label)) {
			jsonWriter.nullValue();
		}
		else {
			jsonWriter.value(label);
		}

		//for the tag and namespaces properties, only write them if they're non-null
		String tag = structuredExample.getTag();

		if (StringUtils.isBlank(tag) == false) jsonWriter.name(StructuredJsonPropertyNames.EXAMPLE_TAG_PROPERTY).value(tag);

		Iterable<Namespace> namespaces = structuredExample.getNamespaces();

		if (namespaces != null) {

			jsonWriter.name(StructuredJsonPropertyNames.EXAMPLE_NAMESPACES_PROPERTY);

			jsonWriter.beginArray();

			for (Namespace ns : namespaces) {
				writeNamespace(ns, jsonWriter);
			}

			jsonWriter.endArray();

		}

		jsonWriter.endObject(); //for the empty example, just write the "{}". 

	}