List of usage examples for com.google.gson JsonObject add
public void add(String property, JsonElement value)
From source file:com.cognifide.actions.msg.websocket.message.SocketMessageProducer.java
License:Apache License
@Override public boolean sendMessage(String type, Map<String, String> message) { final JsonObject msg = new JsonObject(); msg.addProperty("type", type); msg.add("payload", GSON.toJsonTree(message)); final String serializedMsg = GSON.toJson(msg); return sender.sendMessage(serializedMsg); }
From source file:com.cognifide.aet.vs.metadata.MapSerializer.java
License:Apache License
@Override public JsonElement serialize(Map<?, ?> src, Type typeOfSrc, JsonSerializationContext context) { if (src == null || src.isEmpty()) { return null; }/*from w w w. j av a 2 s. c om*/ JsonObject map = new JsonObject(); for (Map.Entry<?, ?> entry : src.entrySet()) { final Object entryKey = entry.getKey(); final String key = entryKey != null ? entryKey.toString() : ""; map.add(key, context.serialize(entry.getValue())); } return map; }
From source file:com.collaide.fileuploader.models.repositorty.RepoFolder.java
@Override public String toJson() { JsonObject repoFolderJson = new JsonObject(); repoFolderJson.add("repo_folder", gson.toJsonTree(this)); return repoFolderJson.toString(); }
From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java
License:Apache License
private JsonObject deepCopyJsonObject(String path, JsonObject el, Set<String> ignorePaths) { JsonObject result = new JsonObject(); for (Map.Entry<String, JsonElement> entry : el.entrySet()) { String newPath = path + "/" + entry.getKey(); if (!ignorePaths.contains(newPath)) { result.add(entry.getKey(), deepCopy(newPath, entry.getValue(), ignorePaths)); }/* www . j a va 2 s.co m*/ } return result; }
From source file:com.comm.sr.service.elasticsearch.EsUpdateService.java
/** * @param updatesMap/*www . j a v a2 s.c o m*/ * @return */ public static String generateEsUpdateScriptFromMap(Map<String, String> updatesMap) { String id = updatesMap.get("id"); updatesMap.remove("id"); updatesMap.remove("index"); updatesMap.remove("type"); JsonObject jsonObj = new JsonObject(); StringBuffer scriptBuffer = new StringBuffer(); JsonObject jsonObject_1 = new JsonObject(); for (Map.Entry<String, String> entrySet : updatesMap.entrySet()) { String key = entrySet.getKey(); String value = entrySet.getValue(); scriptBuffer.append("ctx._source.").append(key).append("=" + key + ";"); jsonObject_1.addProperty(key, value); } jsonObj.addProperty("script", scriptBuffer.toString()); jsonObj.add("params", jsonObject_1); // add upsert script // if document no existed, then create document by id given jsonObj.add("upsert", jsonObject_1); return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(jsonObj); }
From source file:com.comm.sr.service.elasticsearch.EsUpdateService.java
public static String generateEsUpdateScriptFromMapUsingFastJson(Map<String, String> updatesMap) { String id = updatesMap.get("id"); updatesMap.remove("id"); updatesMap.remove("index"); updatesMap.remove("type"); JsonObject jsonObj = new JsonObject(); StringBuffer scriptBuffer = new StringBuffer(); JsonObject jsonObject_1 = new JsonObject(); for (Map.Entry<String, String> entrySet : updatesMap.entrySet()) { String key = entrySet.getKey(); String value = entrySet.getValue(); scriptBuffer.append("ctx._source.").append(key).append("=" + key + ";"); jsonObject_1.addProperty(key, value); }//from ww w . j a v a 2s . c o m jsonObj.addProperty("script", scriptBuffer.toString()); jsonObj.add("params", jsonObject_1); // add upsert script // if document no existed, then create document by id given jsonObj.add("upsert", jsonObject_1); return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(jsonObj); }
From source file:com.confighub.api.org.AddAdminOrOwner.java
License:Open Source License
@POST @Path("/{orgAccName}") @Produces("application/json") public Response update(@PathParam("orgAccName") String orgAccName, @FormParam("un") String newOwnerName, @FormParam("role") String role, @HeaderParam("Authorization") String token) { JsonObject json = new JsonObject(); Gson gson = new Gson(); Store store = new Store(); if (Utils.anyBlank(orgAccName, newOwnerName, role) || !(role.equalsIgnoreCase("own") || role.equalsIgnoreCase("adm"))) { json.addProperty("success", false); json.addProperty("message", "Missing required field."); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); }/*from w w w. j a v a2 s.c o m*/ try { UserAccount newOwner = store.getUserByUsername(newOwnerName); if (null == newOwner) { json.addProperty("success", false); json.addProperty("message", "Requested user not found."); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } int status = validate(orgAccName, token, store); if (0 != status) return Response.status(status).build(); store.begin(); if (role.equals("adm")) organization.addAdministrator(newOwner); else organization.addOwner(newOwner); store.update(organization, user); store.commit(); JsonObject owner = new JsonObject(); owner.addProperty("un", newOwner.getUsername()); owner.addProperty("name", newOwner.getName()); owner.addProperty("email", newOwner.getEmail()); json.add("no", owner); json.addProperty("success", true); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } catch (ConfigException e) { store.rollback(); json.addProperty("success", false); json.addProperty("message", e.getMessage()); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } finally { store.close(); } }
From source file:com.confighub.api.org.CreateOrganization.java
License:Open Source License
@POST @Path("/{accountName}") @Produces("application/json") public Response update(@PathParam("accountName") String accountName, @HeaderParam("Authorization") String token) { JsonObject json = new JsonObject(); Store store = new Store(); Gson gson = new Gson(); try {/*from w w w . j a v a 2 s. c o m*/ UserAccount user = TokenState.getUser(token, store); if (null == user) return Response.status(401).build(); if (Utils.isBlank(accountName)) { json.addProperty("message", "Organization account name has to be specified."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } store.begin(); Organization organization = store.createOrganization(accountName, user); store.commit(); json.addProperty("success", true); JsonObject jsonOrg = new JsonObject(); jsonOrg.addProperty("un", organization.getAccountName()); jsonOrg.addProperty("name", organization.getName()); jsonOrg.addProperty("id", organization.getId()); json.add("organization", jsonOrg); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } catch (ConfigException e) { store.rollback(); json.addProperty("message", e.getErrorCode().getMessage()); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } finally { store.close(); } }
From source file:com.confighub.api.repository.admin.accessRules.CreateAccessRule.java
License:Open Source License
@POST @Path("/{account}/{repository}/{team}") @Produces("application/json") public Response create(@HeaderParam("Authorization") String token, @PathParam("account") String account, @PathParam("repository") String repositoryName, @PathParam("team") String teamName, @FormParam("type") String type, @FormParam("match") String match, @FormParam("key") String key, @FormParam("context") String contextString, @FormParam("access") String access) { JsonObject json = new JsonObject(); Gson gson = new Gson(); Store store = new Store(); try {//from w w w .ja v a2 s .com int status = validateWrite(account, repositoryName, token, store, true); if (0 != status) return Response.status(status).build(); Team team = repository.getTeam(teamName); if (null == team) { json.addProperty("message", "Unable to find specified team."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } if (Utils.isBlank(type)) { json.addProperty("message", "Rule type has to be specified."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } // Always put new rule at the bottom of priority list int priority = team.getRuleCount() + 1; boolean canEdit = "rw".equals(access); AccessRule rule; switch (type) { case "Key": if (Utils.anyBlank(match, key)) { throw new ConfigException(Error.Code.MISSING_PARAMS); } AccessRule.KeyMatchType keyMatchType; switch (match) { case "Is": keyMatchType = AccessRule.KeyMatchType.Is; break; case "StartsWith": keyMatchType = AccessRule.KeyMatchType.StartsWith; break; case "EndsWith": keyMatchType = AccessRule.KeyMatchType.EndsWith; break; case "Contains": keyMatchType = AccessRule.KeyMatchType.Contains; break; default: throw new ConfigException(Error.Code.INVALID_TYPE); } rule = new AccessRule(team, AccessRule.RuleTarget.Key, keyMatchType, key, canEdit, priority); team.addRule(rule); break; case "Value": Collection<CtxLevel> context = null; try { context = ContextParser.parseAndCreate(contextString, repository, store, user, null); } catch (NumberFormatException e) { json.addProperty("message", "Invalid context element specified."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } AccessRule.ContextMatchType contextMatchType; switch (match) { case "ContainsAny": contextMatchType = AccessRule.ContextMatchType.ContainsAny; break; case "ContainsAll": contextMatchType = AccessRule.ContextMatchType.ContainsAll; break; case "DoesNotContain": contextMatchType = AccessRule.ContextMatchType.DoesNotContain; break; case "Resolves": contextMatchType = AccessRule.ContextMatchType.Resolves; break; case "DoesNotResolve": contextMatchType = AccessRule.ContextMatchType.DoesNotResolve; break; default: throw new ConfigException(Error.Code.INVALID_TYPE); } rule = new AccessRule(team, AccessRule.RuleTarget.Value, contextMatchType, (Set<CtxLevel>) context, canEdit, priority); team.addRule(rule); break; default: json.addProperty("message", "Invalid rule type specified."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } store.begin(); store.updateAccessRule(repository, user, rule); store.commit(); json.addProperty("success", true); json.add("accessRules", GsonHelper.ruleToJson(team)); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } catch (ConfigException e) { store.rollback(); json.addProperty("message", e.getMessage()); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } finally { store.close(); } }
From source file:com.confighub.api.repository.admin.accessRules.DeleteAccessRule.java
License:Open Source License
@POST @Path("/{account}/{repository}/{team}") @Produces("application/json") public Response update(@HeaderParam("Authorization") String token, @PathParam("account") String account, @PathParam("repository") String repositoryName, @PathParam("team") String teamName, @FormParam("id") Long ruleId) { JsonObject json = new JsonObject(); Gson gson = new Gson(); Store store = new Store(); try {//from ww w . j a v a2s .c o m int status = validateWrite(account, repositoryName, token, store, true); if (0 != status) return Response.status(status).build(); Team team = repository.getTeam(teamName); if (null == team) { json.addProperty("message", "Unable to find specified team."); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } store.begin(); store.deleteRule(repository, user, ruleId); store.commit(); json.addProperty("success", true); json.add("accessRules", GsonHelper.ruleToJson(team)); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } catch (ConfigException e) { store.rollback(); json.addProperty("message", e.getMessage()); json.addProperty("success", false); return Response.ok(gson.toJson(json), MediaType.APPLICATION_JSON).build(); } finally { store.close(); } }