List of usage examples for com.google.gson JsonArray add
public void add(JsonElement element)
From source file:com.github.jsdossier.DocWriter.java
License:Apache License
private IndexReference addTypeInfo(JsonArray array, NominalType type) { String dest = config.getOutput().relativize(linker.getFilePath(type)).toString(); JsonObject details = new JsonObject(); details.addProperty("name", type.getQualifiedName()); details.addProperty("href", dest); details.addProperty("namespace", !type.getJsType().isInstanceType() && !type.getJsType().isConstructor() && !type.getJsType().isEnumType()); details.addProperty("interface", type.getJsType().isInterface()); array.add(details); NominalType resolvedType = typeRegistry.resolve(type.getJsType()); boolean isAlias = resolvedType != type; if (!isAlias || config.isFilteredType(resolvedType)) { List<NominalType> typedefs = FluentIterable.from(type.getTypes()).filter(isTypedef()) .toSortedList(new NameComparator()); for (NominalType typedef : typedefs) { TypeLink link = linker.getLink(typedef); if (link == null) { continue; // TODO: decide what to do with global type links. }/*from www. j av a2 s . c om*/ JsonObject typedefDetails = new JsonObject(); typedefDetails.addProperty("name", link.getText()); typedefDetails.addProperty("href", link.getHref()); array.add(typedefDetails); } } return new IndexReference(type, details); }
From source file:com.github.jsdossier.TypeIndex.java
License:Apache License
private synchronized IndexReference addTypeInfo(JsonArray array, NominalType type) { if (seenTypes.containsKey(type)) { return seenTypes.get(type); }/* ww w .ja va 2s . c o m*/ String dest = dfs.getRelativePath(dfs.getPath(type)).toString(); JsonObject details = new JsonObject(); details.addProperty("name", dfs.getDisplayName(type)); details.addProperty("qualifiedName", dfs.getQualifiedDisplayName(type)); details.addProperty("href", dest); details.addProperty("namespace", type.isNamespace()); details.addProperty("interface", type.getType().isInterface()); array.add(details); List<NominalType> allTypes = typeRegistry.getTypes(type.getType()); if (allTypes.get(0) != type) { List<NominalType> typedefs = FluentIterable.from(typeRegistry.getNestedTypes(type)).filter(isTypedef()) .toSortedList(new QualifiedNameComparator()); for (NominalType typedef : typedefs) { TypeLink link = linkFactory.createLink(typedef); checkArgument(!link.getHref().isEmpty(), "Failed to build link for %s", typedef.getName()); JsonObject typedefDetails = new JsonObject(); typedefDetails.addProperty("name", link.getText()); typedefDetails.addProperty("href", link.getHref()); array.add(typedefDetails); } } IndexReference ref = new IndexReference(type, details); seenTypes.put(type, ref); return ref; }
From source file:com.github.tarsys.android.orm.dataobjects.JSONDataSource.java
public void addDataObject(JsonObject item) { JsonArray datos = this.getData(); if (this.columns.isEmpty()) { if (item != null) { for (Map.Entry<String, JsonElement> e : item.entrySet()) this.columns.add(e.getKey()); }//from w w w . j av a2s.co m } datos.add(item); this.jsonData = datos.toString(); }
From source file:com.github.zhanhb.ishadowsocks.GuiConfigs.java
License:Open Source License
void setConfigs(Collection<JsonObject> configs) { JsonArray array = new JsonArray(configs.size()); for (JsonObject config : configs) { array.add(config); }/*from w ww . j a v a 2 s. c o m*/ jsonObject.add("configs", array); }
From source file:com.github.zhizheng.json.JsonSchemaGeneratorImpl.java
License:Apache License
/** * ? JsonElement? Json Schema //from w ww . j a v a 2 s. c om * * @param jsonElement * @param elementName * @param isFirstLevel * @param required * @return */ private JsonObject makeSchemaElement(JsonElement jsonElement, String elementName, boolean isFirstLevel, JsonArray required) { JsonObject jsonSchemaObject = new JsonObject(); // id, $schema if (isFirstLevel) { if (jsonSchemaConfig.isPrintId()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), jsonSchemaConfig.getId()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.SCHEMA.toString(), jsonSchemaConfig.getVersion()); } else { if (jsonSchemaConfig.isPrintId()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), "/" + elementName); } } // title if (jsonSchemaConfig.isPrintTitle()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.TITLE.toString(), elementName);// jsonSchemaConfig.getTitle() } // description if (jsonSchemaConfig.isPrintDescription()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DESCRIPTION.toString(), jsonSchemaConfig.getDescription()); } // type String jsonElementType = JsonValueTypes.getJsonValueType(jsonElement); jsonSchemaObject.addProperty(JsonSchemaKeywords.TYPE.toString(), jsonElementType); if (jsonElementType.equals(JsonValueTypes.STRING.toString())) {// string if (jsonSchemaConfig.isPrintMinLength()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINLENGTH.toString(), jsonSchemaConfig.getMinLength()); } if (jsonSchemaConfig.isPrintMaxLength()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXLENGTH.toString(), jsonSchemaConfig.getMaxLength()); } if (jsonSchemaConfig.isPrintDefault()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(), jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsString() : jsonSchemaConfig.getDefaultString()); } } if (jsonElementType.equals(JsonValueTypes.NUMBER.toString())) {// number if (jsonSchemaConfig.isPrintMinimum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(), jsonSchemaConfig.getMinimum()); } if (jsonSchemaConfig.isPrintMaximum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(), jsonSchemaConfig.getMaximum()); } if (jsonSchemaConfig.isPrintExclusiveMinimum()) { if (!jsonSchemaConfig.isPrintMinimum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(), jsonSchemaConfig.getMinimum()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMINIMUM.toString(), jsonSchemaConfig.isExclusiveMinimum()); } if (jsonSchemaConfig.isPrintExclusiveMaximum()) { if (!jsonSchemaConfig.isPrintMaximum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(), jsonSchemaConfig.getMaximum()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMAXIMUM.toString(), jsonSchemaConfig.isExclusiveMaximum()); } if (jsonSchemaConfig.isPrintDefault()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(), jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsNumber() : jsonSchemaConfig.getDefaultNumber()); } } // required && V3 if (jsonSchemaConfig.isPrintRequired() && JsonSchemaVersions.V3.toString().equals(jsonSchemaConfig.getVersion())) {// V3 required boolean ??? jsonSchemaObject.addProperty(JsonSchemaKeywords.REQUIRED.toString(), jsonSchemaConfig.isRequired()); } // required && V4 if (jsonSchemaConfig.isPrintRequired() && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion()) && (jsonElementType.equals(JsonValueTypes.STRING.toString()) || jsonElementType.equals(JsonValueTypes.NUMBER.toString()) || jsonElementType.equals(JsonValueTypes.INTEGER.toString()) || jsonElementType.equals(JsonValueTypes.BOOLEAN.toString()))) {// V4 required array ? object required.add(elementName); } // properties, items JsonArray newRequired = new JsonArray(); if (jsonElementType.equals(JsonValueTypes.OBJECT.toString()) && !jsonElement.getAsJsonObject().entrySet().isEmpty()) {// object.properties JsonObject propertiesObject = new JsonObject(); for (Map.Entry<String, JsonElement> propertyElemement : jsonElement.getAsJsonObject().entrySet()) { propertiesObject.add(propertyElemement.getKey(), makeSchemaElement(propertyElemement.getValue(), propertyElemement.getKey(), false, newRequired)); } jsonSchemaObject.add(JsonSchemaKeywords.PROPERTIES.toString(), propertiesObject); } else if (jsonElementType.equals(JsonValueTypes.ARRAY.toString()) && jsonElement.getAsJsonArray().size() > 0) {// array.items JsonArray jsonArray = jsonElement.getAsJsonArray(); jsonSchemaObject.add(JsonSchemaKeywords.ITEMS.toString(), makeSchemaElement(jsonArray.get(0), "0", false, new JsonArray())); } // required && V4 if (jsonElementType.equals(JsonValueTypes.OBJECT.toString()) && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion())) {// object.required jsonSchemaObject.add(JsonSchemaKeywords.REQUIRED.toString(), newRequired); } // minitems , uniqueitems if (jsonElementType.equals(JsonValueTypes.ARRAY.toString())) {// array if (jsonSchemaConfig.isPrintMinItems()) {// array.minitems jsonSchemaObject.addProperty(JsonSchemaKeywords.MINITEMS.toString(), jsonSchemaConfig.getMinItems()); } if (jsonSchemaConfig.isPrintUniqueItems()) {// array.uniqueitems jsonSchemaObject.addProperty(JsonSchemaKeywords.UNIQUEITEMS.toString(), jsonSchemaConfig.isUniqueItems()); } } return jsonSchemaObject; }
From source file:com.gmail.tracebachi.DeltaSkins.Shared.MojangApi.java
License:Open Source License
private String convertUuidRequestsToStringPayload() { synchronized (uuidRequestLock) { Iterator<String> iterator = uuidRequests.iterator(); JsonArray array = new JsonArray(); for (int i = 0; i < 100 && iterator.hasNext(); ++i) { String next = iterator.next(); if (next != null && !next.isEmpty()) { array.add(new JsonPrimitive(next)); }//from w w w . j a v a2 s.com iterator.remove(); } return gson.toJson(array); } }
From source file:com.gmail.tracebachi.DeltaSkins.Shared.Storage.PlayerProfileStorage.java
License:Open Source License
@Override public JsonArray serialize() { JsonArray array = new JsonArray(); synchronized (profileMap) { for (PlayerProfile profile : profileMap.values()) { array.add(profile.serialize()); }// w ww . j a v a2 s . com } return array; }
From source file:com.gmail.tracebachi.DeltaSkins.Shared.Storage.SkinDataStorage.java
License:Open Source License
@Override public JsonArray serialize() { JsonArray array = new JsonArray(); synchronized (skinDataMap) { for (SkinData skinData : skinDataMap.values()) { if ((System.currentTimeMillis() - skinData.getCreatedAt()) < removeOlderThanMinutes * 60 * 1000) { array.add(skinData.serialize()); }/*from w w w . ja v a 2 s. c o m*/ } } return array; }
From source file:com.goodow.realtime.server.model.DeltaSerializer.java
License:Apache License
public static JsonElement dataToClientJson(Delta<String> data, long resultingRevision) { Preconditions.checkArgument(resultingRevision <= MAX_DOUBLE_INTEGER, "Resulting revision %s is too large", resultingRevision);//from w ww. ja v a 2 s .c om // Assume payload is JSON, and parse it to avoid nested json. // TODO: Consider using Delta<JSONObject> instead. // The reason I haven't done it yet is because it's not immutable, // and also for reasons described in Delta. JsonElement payloadJson; try { payloadJson = new JsonParser().parse(data.getPayload()); } catch (JsonParseException e) { throw new IllegalArgumentException("Invalid payload for " + data, e); } JsonArray json = new JsonArray(); try { Preconditions.checkArgument(resultingRevision >= 0, "invalid rev %s", resultingRevision); json.add(new JsonPrimitive(resultingRevision)); long sanityCheck = json.get(0).getAsLong(); if (sanityCheck != resultingRevision) { throw new AssertionError("resultingRevision " + resultingRevision + " not losslessly represented in JSON, got back " + sanityCheck); } json.add(new JsonPrimitive(data.getSession().userId)); json.add(new JsonPrimitive(data.getSession().sessionId)); json.add(payloadJson); return json; } catch (JsonParseException e) { throw new Error(e); } }
From source file:com.goodow.realtime.server.rpc.DeltaHandler.java
License:Apache License
private static JsonArray serializeDeltas(long startVersion, List<Delta<String>> entries) { JsonArray history = new JsonArray(); int index = 0; for (Delta<String> data : entries) { history.add(DeltaSerializer.dataToClientJson(data, startVersion + index + 1)); index++;//from w w w. j av a 2s . c o m } return history; }