List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java
License:Apache License
public void set(String dataPath, boolean newValue) { set(dataPath, new JsonPrimitive(newValue)); }
From source file:com.gemini.domain.dto.serialize.GeminiNetworkRouterSerializer.java
@Override public JsonElement serialize(GeminiNetworkRouterDTO src, Type typeOfSrc, JsonSerializationContext context) { JsonObject routerElement = new JsonObject(); //register all the required custom serializers Gson gson = new GsonBuilder().registerTypeAdapter(GeminiNetworkDTO.class, new GeminiNetworkSerializer()) .create();//w ww . j ava2 s. c om //first the primitives routerElement.addProperty("name", src.getName()); routerElement.addProperty("cloudID", src.getCloudID()); routerElement.add("gateway", gson.toJsonTree(src.getGateway(), GeminiNetworkDTO.class)); //now the routes JsonArray routeArray = new JsonArray(); src.getRoutes().entrySet().stream().forEach(e -> routeArray.add(new JsonPrimitive( new StringBuilder().append(e.getKey()).append(",").append(e.getValue()).append("\n").toString()))); routerElement.add("routes", routeArray); //now the interfaces - while each interface is a GeminiSubnetDTO object //we will only add the name and cloudID because it is only a reference to //a subnet created under applications JsonArray interfaceArray = new JsonArray(); src.getInterfaces().stream().forEach(i -> { JsonObject intElement = new JsonObject(); intElement.add("name", new JsonPrimitive(i.getName())); intElement.add("cloudID", new JsonPrimitive(i.getCloudID())); interfaceArray.add(intElement); }); routerElement.add("interfaces", routeArray); return routerElement; }
From source file:com.gemini.domain.dto.serialize.GeminiServerSerializer.java
@Override public JsonElement serialize(GeminiServerDTO src, Type typeOfSrc, JsonSerializationContext context) { JsonObject serverElement = new JsonObject(); Gson gson = new Gson(); serverElement.addProperty("name", src.getName()); serverElement.addProperty("cloudID", src.getCloudID()); serverElement.addProperty("description", src.getDescription()); serverElement.addProperty("dateCreated", src.getDateCreated()); serverElement.addProperty("address", src.getAddress()); serverElement.addProperty("addressType", src.getAddressType()); serverElement.addProperty("subnetMask", src.getSubnetMask()); serverElement.addProperty("port", src.getPort()); serverElement.addProperty("os", src.getOs()); serverElement.addProperty("type", src.getType()); serverElement.addProperty("admin", src.getAdmin()); serverElement.addProperty("password", src.getPassword()); serverElement.addProperty("serverType", src.getServerType()); //the metadata, since it is a HashMap GSON requires a type token what type //of data is contained in the HashMap TypeToken<HashMap<String, String>> metadataType = new TypeToken<HashMap<String, String>>() { };/*w ww .j ava2 s . c om*/ serverElement.add("metadata", gson.toJsonTree(src.getMetadata(), metadataType.getType())); //the security group names JsonArray secGrpNames = new JsonArray(); src.getSecurityGroupNames().stream().forEach(sg -> { secGrpNames.add(new JsonPrimitive(sg)); }); serverElement.add("securityGroupNames", secGrpNames); return serverElement; }
From source file:com.getperka.flatpack.codexes.EntityMapCodex.java
License:Apache License
@Override public Map<K, V> readNotNull(JsonElement element, DeserializationContext context) throws IOException { Map<K, V> toReturn = FlatPackCollections.mapForIteration(); for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) { context.pushPath("." + entry.getKey()); try {// www.j ava2 s . c o m K key = keyCodex.read(new JsonPrimitive(entry.getKey()), context); V value = valueCodex.read(entry.getValue(), context); toReturn.put(key, value); } finally { context.popPath(); } } return toReturn; }
From source file:com.ghjansen.cas.ui.desktop.swing.SimulationParameterJsonAdapter.java
License:Open Source License
public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context) { UnidimensionalSimulationParameter parameter = (UnidimensionalSimulationParameter) object; final JsonObject member = new JsonObject(); final JsonObject ruleTypeParameter = new JsonObject(); final JsonObject ruleConfigurationParameter = new JsonObject(); final JsonArray stateValues = new JsonArray(); final JsonObject limitsParameter = new JsonObject(); final JsonObject initialConditionParameter = new JsonObject(); final JsonArray sequences = new JsonArray(); ruleTypeParameter.addProperty("elementar", parameter.getRuleTypeParameter().isElementar()); int[] states = parameter.getRuleConfigurationParameter().getStateValues(); for (int i = 0; i < states.length; i++) { stateValues.add(new JsonPrimitive( parameter.getRuleConfigurationParameter().getStateValues()[states.length - 1 - i])); }//from w w w . ja v a2 s. com ruleConfigurationParameter.add("stateValues", stateValues); limitsParameter.addProperty("cells", parameter.getLimitsParameter().getCells()); limitsParameter.addProperty("iterations", parameter.getLimitsParameter().getIterations()); for (int i = 0; i < parameter.getInitialConditionParameter().getSequences().size(); i++) { UnidimensionalSequenceParameter s = (UnidimensionalSequenceParameter) parameter .getInitialConditionParameter().getSequences().get(i); JsonObject sequence = new JsonObject(); sequence.addProperty("initialPosition", s.getInitialPosition()); sequence.addProperty("finalPosition", s.getFinalPosition()); sequence.addProperty("value", s.getValue()); sequences.add(sequence); } initialConditionParameter.add("sequences", sequences); member.add("ruleTypeParameter", ruleTypeParameter); member.add("ruleConfigurationParameter", ruleConfigurationParameter); member.add("limitsParameter", limitsParameter); member.add("initialConditionParameter", initialConditionParameter); return member; }
From source file:com.github.autermann.matlab.json.MatlabRequestSerializer.java
License:Open Source License
private JsonObject serializeResults(MatlabRequest req) { JsonObject results = new JsonObject(); for (Entry<String, MatlabType> result : req.getResults().entrySet()) { results.add(result.getKey(), new JsonPrimitive(result.getValue().toString())); }//from www. ja v a2 s . c o m return results; }
From source file:com.github.consiliens.harv.gson.IRequestLogRecordSerializer.java
License:Open Source License
public static JsonArray paramsToJsonArray(final HttpParams params) { JsonArray paramsJson = new JsonArray(); // BasicHttpParams has no way to access HashMap parameters so use // reflection. try {//ww w . j ava 2 s . c o m final Field parameters = BasicHttpParams.class.getDeclaredField(R.parameters); parameters.setAccessible(true); HashMap<?, ?> targetParameters = (HashMap<?, ?>) parameters.get(params); if (!targetParameters.isEmpty()) { for (Entry<?, ?> entry : targetParameters.entrySet()) { JsonArray entryJson = new JsonArray(); entryJson.add(new JsonPrimitive(entry.getKey().toString())); // TODO: Is there a way to easily support non-string values? entryJson.add(new JsonPrimitive(entry.getValue().toString())); paramsJson.add(entryJson); } } } catch (Exception e) { e.printStackTrace(); } return paramsJson; }
From source file:com.github.consiliens.harv.gson.IRequestLogRecordSerializer.java
License:Open Source License
private static JsonArray headersToJsonArray(Header[] headers) { final JsonArray headersArray = new JsonArray(); for (Header header : headers) { JsonArray nvPair = new JsonArray(); nvPair.add(new JsonPrimitive(header.getName())); nvPair.add(new JsonPrimitive(header.getValue())); headersArray.add(nvPair);// w w w .j a v a2 s . c o m } return headersArray; }
From source file:com.github.jsdossier.Config.java
License:Apache License
/** * Returns this configuration object as a JSON object. *//* w w w .j a v a 2 s .c o m*/ JsonObject toJson() { JsonObject json = new JsonObject(); json.add("output", new JsonPrimitive(output.toString())); json.add("sources", toJsonArray(srcs)); json.add("modules", toJsonArray(modules)); json.add("externs", toJsonArray(externs)); json.add("excludes", toJsonArray(excludes)); json.add("typeFilters", toJsonArray(typeFilters)); json.add("stripModulePrefix", new JsonPrimitive(modulePrefix.toString())); json.add("readme", readme.isPresent() ? new JsonPrimitive(readme.get().toString()) : JsonNull.INSTANCE); json.addProperty("strict", strict); json.addProperty("useMarkdown", useMarkdown); json.addProperty("language", language.name()); JsonArray pages = new JsonArray(); for (Page page : customPages) { pages.add(page.toJson()); } json.add("customPages", pages); return json; }
From source file:com.github.jsdossier.Config.java
License:Apache License
private JsonArray toJsonArray(ImmutableSet<?> items) { JsonArray array = new JsonArray(); for (Object i : items) { array.add(new JsonPrimitive(i.toString())); }//from ww w .j a v a 2s.co m return array; }