List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.googlesource.gerrit.plugins.github.wizard.RepositoriesListController.java
License:Apache License
@Override public void doAction(IdentifiedUser user, GitHubLogin hubLogin, HttpServletRequest req, HttpServletResponse resp, ControllerErrors errors) throws ServletException, IOException { String organisation = req.getParameter("organisation"); JsonArray jsonRepos = new JsonArray(); int numRepos = 0; PagedIterator<GHRepository> repoIter = getRepositories(hubLogin, organisation).iterator(); while (repoIter.hasNext() && numRepos < config.repositoryListLimit) { GHRepository ghRepository = repoIter.next(); JsonObject repository = new JsonObject(); String projectName = organisation + "/" + ghRepository.getName(); if (projects.get(Project.NameKey.parse(projectName)) == null) { repository.add("name", new JsonPrimitive(ghRepository.getName())); repository.add("organisation", new JsonPrimitive(organisation)); repository.add("description", new JsonPrimitive(Strings.nullToEmpty(ghRepository.getDescription()))); repository.add("private", new JsonPrimitive(ghRepository.isPrivate())); jsonRepos.add(repository);// w w w. j a v a 2 s. c o m numRepos++; } } resp.getWriter().println(jsonRepos.toString()); }
From source file:com.googlesource.gerrit.plugins.replication.ListCommand.java
License:Apache License
private void addProperty(JsonObject obj, String key, List<String> values) { if (!values.isEmpty()) { JsonArray list = new JsonArray(); for (String v : values) { list.add(new JsonPrimitive(v)); }//from ww w.jav a2s .c om obj.add(key, list); } }
From source file:com.googlesource.gerrit.plugins.replication.ListCommand.java
License:Apache License
private void addQueueDetails(JsonObject obj, String key, Collection<PushOne> values) { if (values.size() > 0) { JsonArray list = new JsonArray(); for (PushOne p : values) { list.add(new JsonPrimitive(p.toString())); }// w ww . j av a 2s .c o m obj.add(key, list); } }
From source file:com.gsaul.AethonSimulator.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }/* ww w.j a va 2 s . c om*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //used to be .remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.gst.infrastructure.core.api.JodaDateTimeAdapter.java
License:Apache License
@SuppressWarnings("unused") @Override//from w w w.jav a 2 s . com public JsonElement serialize(final DateTime src, final Type typeOfSrc, final JsonSerializationContext context) { JsonElement element = null; if (src != null) { element = new JsonPrimitive(src.getMillis()); } return element; }
From source file:com.gst.infrastructure.core.api.JodaLocalDateAdapter.java
License:Apache License
@SuppressWarnings("unused") @Override//from w w w . ja v a 2 s. co m public JsonElement serialize(final LocalDate src, final Type typeOfSrc, final JsonSerializationContext context) { JsonArray array = null; if (src != null) { array = new JsonArray(); array.add(new JsonPrimitive(src.getYearOfEra())); array.add(new JsonPrimitive(src.getMonthOfYear())); array.add(new JsonPrimitive(src.getDayOfMonth())); } return array; }
From source file:com.gst.infrastructure.core.api.JodaMonthDayAdapter.java
License:Apache License
@SuppressWarnings("unused") @Override/*from www . j av a2 s . c o m*/ public JsonElement serialize(final MonthDay src, final Type typeOfSrc, final JsonSerializationContext context) { JsonArray array = null; if (src != null) { array = new JsonArray(); array.add(new JsonPrimitive(src.getMonthOfYear())); array.add(new JsonPrimitive(src.getDayOfMonth())); } return array; }
From source file:com.hbm.devices.jet.JetPeer.java
License:Open Source License
private JsonObject fillPath(Matcher matcher) { JsonObject path = new JsonObject(); if ((matcher.contains != null && !matcher.contains.isEmpty())) { path.addProperty("contains", matcher.contains); }// w w w . ja va2s . c o m if ((matcher.startsWith != null && !matcher.startsWith.isEmpty())) { path.addProperty("startsWith", matcher.startsWith); } if ((matcher.endsWith != null && !matcher.endsWith.isEmpty())) { path.addProperty("endsWith", matcher.endsWith); } if ((matcher.equals != null && !matcher.equals.isEmpty())) { path.addProperty("equals", matcher.equals); } if ((matcher.equalsNot != null && !matcher.equalsNot.isEmpty())) { path.addProperty("equalsNot", matcher.equalsNot); } if ((matcher.containsAllOf != null) && (matcher.containsAllOf.length > 0)) { JsonArray containsArray = new JsonArray(); for (String element : matcher.containsAllOf) { containsArray.add(new JsonPrimitive(element)); } path.add("containsAllOf", containsArray); } if ((path.entrySet() != null) && (path.entrySet().size() > 0)) { return path; } else { return null; } }
From source file:com.health.smart.ejb.GCMBean.java
private String generateGCMRequest(List<Device> devices, String message) throws Exception { JsonArray jsonDeviceArray = new JsonArray(); for (Device d : devices) { JsonPrimitive p = new JsonPrimitive(d.getDeviceId()); jsonDeviceArray.add(p);/*from w ww .j av a 2 s. c om*/ } JsonObject json = new JsonObject(); json.add("registration_ids", jsonDeviceArray); JsonObject data = new JsonObject(); data.addProperty("message", message); json.add("data", data); return json.toString(); }
From source file:com.hisign.sso.common.gson.CustDateTypeAdapter.java
License:Apache License
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { String dateFormatAsString = custFormat.format(src); return new JsonPrimitive(dateFormatAsString); }