List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:cc.kave.commons.utils.json.legacy.ISO8601DateParser.java
License:Open Source License
@Override public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) { final Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(src);/* w w w . j a v a2 s . c o m*/ return new JsonPrimitive(DatatypeConverter.printDateTime(calendar)); }
From source file:cc.kave.commons.utils.json.LocalDateTimeConverter.java
License:Apache License
@Override public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(src)); }
From source file:cc.kave.commons.utils.json.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//www. j a va 2s.c o m 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); // (kave adaptation) was: ".remove(typeFiledName)" JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(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 { if (value == null) { Streams.write(null, out); return; } 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(); // (kave adaptation) disabled check // 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); } }; }
From source file:cc.recommenders.utils.gson.GsonNameSerializer.java
License:Open Source License
@Override public JsonElement serialize(final IName src, final Type typeOfSrc, final JsonSerializationContext context) { return new JsonPrimitive(src.getIdentifier()); }
From source file:ccm.pay2spawn.permissions.Group.java
License:Open Source License
public JsonElement toJson() { JsonObject root = new JsonObject(); root.addProperty("name", getName()); root.addProperty("parent", getParent()); JsonArray nodes = new JsonArray(); for (Node node : this.nodes) nodes.add(new JsonPrimitive(node.toString())); root.add("nodes", nodes); return root;/*from w w w . j a v a2 s .co m*/ }
From source file:ccm.pay2spawn.permissions.Player.java
License:Open Source License
public JsonElement toJson() { JsonObject root = new JsonObject(); root.addProperty("name", getName()); JsonArray groups = new JsonArray(); for (String group : this.getGroups()) groups.add(new JsonPrimitive(group)); root.add("groups", groups); JsonArray nodes = new JsonArray(); for (Node node : this.overrideNodes) nodes.add(new JsonPrimitive(node.toString())); root.add("overrides", nodes); return root;/*from w w w . j ava2 s. co m*/ }
From source file:ccm.pay2spawn.random.RndColors.java
License:Open Source License
@Override public String solverRandom(int type, String value) { Matcher mRGB = PATTERN.matcher(value); if (type == INT_ARRAY) { JsonArray colors = new JsonArray(); mRGB.find();/*from w ww .j a va 2 s.c o m*/ for (int i = 0; i < Integer.parseInt(mRGB.group(1)); i++) colors.add(new JsonPrimitive( (RANDOM.nextInt(200) << 16) + (RANDOM.nextInt(200) << 8) + RANDOM.nextInt(200))); return mRGB.replaceFirst(colors.toString()); } else { return mRGB.replaceFirst( "" + ((RANDOM.nextInt(200) << 16) + (RANDOM.nextInt(200) << 8) + RANDOM.nextInt(200))); } }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation//from w w w . j a v a 2 s. co m * * @param dataToFormat data to be formatted * @param donation the donation data * * @return the fully var-replaced JsonElement */ public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) { if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) { return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward)); } if (dataToFormat.isJsonArray()) { JsonArray out = new JsonArray(); for (JsonElement element : dataToFormat.getAsJsonArray()) { out.add(formatText(element, donation, reward)); } return out; } if (dataToFormat.isJsonObject()) { JsonObject out = new JsonObject(); for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) { out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward)); } return out; } return dataToFormat; }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
/** * To avoid idiocy later we need to store all things as a string with the type in the string. :( * Please tell your users about this!//from www.j a v a 2 s. c o m * * @see ccm.pay2spawn.util.JsonNBTHelper#parseJSON(com.google.gson.JsonPrimitive) */ public static JsonElement parseNBT(NBTBase element) { switch (element.getId()) { // 0 = END case BYTE: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagByte) element).func_150290_f()); case SHORT: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagShort) element).func_150289_e()); case INT: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagInt) element).func_150287_d()); case LONG: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagLong) element).func_150291_c()); case FLOAT: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagFloat) element).func_150288_h()); case DOUBLE: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagDouble) element).func_150286_g()); case BYTE_ARRAY: return parseNBT((NBTTagByteArray) element); case STRING: return new JsonPrimitive(NBTTypes[element.getId()] + ":" + ((NBTTagString) element).func_150285_a_()); case LIST: return parseNBT((NBTTagList) element); case COMPOUND: return parseNBT((NBTTagCompound) element); case INT_ARRAY: return parseNBT((NBTTagIntArray) element); default: return null; } }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
public static JsonPrimitive parseNBT(NBTTagIntArray nbtArray) { JsonArray jsonArray = new JsonArray(); for (int i : nbtArray.func_150302_c()) jsonArray.add(new JsonPrimitive(i)); return new JsonPrimitive(NBTTypes[nbtArray.getId()] + ":" + jsonArray.toString()); }