List of usage examples for com.google.gson JsonObject getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive(String memberName)
From source file:jp.pay.model.ExternalAccountTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ExternalAccount.class.isAssignableFrom(type.getRawType())) { return null; // this class only serializes 'ExternalAccount' and its subtypes }/*from w w w . jav a2s . c o m*/ final String SOURCE_OBJECT_PROP = "object"; final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); final TypeAdapter<ExternalAccount> externalAccountAdapter = gson.getDelegateAdapter(this, TypeToken.get(ExternalAccount.class)); final TypeAdapter<Card> cardAdapter = gson.getDelegateAdapter(this, TypeToken.get(Card.class)); TypeAdapter<ExternalAccount> result = new TypeAdapter<ExternalAccount>() { public void write(JsonWriter out, ExternalAccount value) throws IOException { // TODO: check instance of for correct writer externalAccountAdapter.write(out, value); } public ExternalAccount read(JsonReader in) throws IOException { JsonObject object = elementAdapter.read(in).getAsJsonObject(); String sourceObject = object.getAsJsonPrimitive(SOURCE_OBJECT_PROP).getAsString(); if (sourceObject.equals("card")) { return cardAdapter.fromJsonTree(object); } else { return externalAccountAdapter.fromJsonTree(object); } } }.nullSafe(); return (TypeAdapter<T>) result; }
From source file:me.finalchild.nashornbukkit.util.BukkitImporter.java
License:MIT License
private static void addUsedIdentifiers(JsonObject obj, Set<String> set) { if (obj.has("type")) { String type = obj.getAsJsonPrimitive("type").getAsString(); if (type.equals("Identifier")) { set.add(obj.getAsJsonPrimitive("name").getAsString()); return; }// w w w . j a v a 2 s . co m } for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { if (entry.getValue().isJsonObject()) { addUsedIdentifiers(entry.getValue().getAsJsonObject(), set); } else if (entry.getValue().isJsonArray()) { addUsedIdentifiers(entry.getValue().getAsJsonArray(), set); } } }
From source file:net.conquiris.gson.GsonIndexInfo.java
License:Apache License
private String getOptionalString(JsonObject object, String key) { if (object.has(key)) { return object.getAsJsonPrimitive(key).getAsString(); }/*from w ww .j av a2s . c om*/ return null; }
From source file:net.conquiris.gson.GsonIndexInfo.java
License:Apache License
@Override public IndexInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { checkArgument(json.isJsonObject(), "Expected JSON Object to deserialize IndexInfo."); final JsonObject object = json.getAsJsonObject(); final String checkpoint = getOptionalString(object, IndexInfo.CHECKPOINT_NAME); final String targetCheckpoint = getOptionalString(object, IndexInfo.TARGET_CHECKPOINT_NAME); final int documents = object.getAsJsonPrimitive(N).getAsInt(); final long timestamp = object.getAsJsonPrimitive(IndexInfo.TIMESTAMP_NAME).getAsLong(); final long sequence = object.getAsJsonPrimitive(IndexInfo.SEQUENCE_NAME).getAsLong(); final ImmutableMap.Builder<String, String> b = ImmutableMap.builder(); if (object.has(PROPERTIES)) { JsonObject properties = object.getAsJsonObject(PROPERTIES); for (Entry<String, JsonElement> entry : properties.entrySet()) { b.put(entry.getKey(), entry.getValue().getAsString()); }//from ww w. ja v a2 s . com } return IndexInfo.of(checkpoint, targetCheckpoint, documents, timestamp, sequence, b.build()); }
From source file:net.conquiris.gson.GsonIndexReport.java
License:Apache License
@Override public IndexReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { checkArgument(json.isJsonObject(), "Expected JSON Object to deserialize IndexReport."); final JsonObject object = json.getAsJsonObject(); final IndexReportLevel level = IndexReportLevel.valueOf(object.getAsJsonPrimitive(LEVEL).getAsString()); final boolean started = object.getAsJsonPrimitive(STARTED).getAsBoolean(); final boolean active = object.getAsJsonPrimitive(ACTIVE).getAsBoolean(); final IndexStatus status = IndexStatus.valueOf(object.getAsJsonPrimitive(STATUS).getAsString()); if (level == IndexReportLevel.BASIC) { return IndexReport.basic(started, active, status); }/*from w ww. j a v a2s. com*/ final Delays delays = context.deserialize(object.get(DELAYS), Delays.class); final IndexInfo info = context.deserialize(object.get(INFO), IndexInfo.class); if (level == IndexReportLevel.NORMAL) { return IndexReport.normal(started, active, status, delays, info); } return IndexReport.detailed(started, active, status, delays, info); }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Does the given JsonObject contain a string field with the given name? *//*from w ww. ja v a 2s .c o m*/ public static boolean isString(JsonObject json, String memberName) { return isJsonPrimitive(json, memberName) && json.getAsJsonPrimitive(memberName).isString(); }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
public static boolean isBoolean(JsonObject json, String memberName) { return isJsonPrimitive(json, memberName) && json.getAsJsonPrimitive(memberName).isBoolean(); }
From source file:net.ilexiconn.magister.adapter.AppointmentAdapter.java
License:Apache License
@Override public Appointment[] read(JsonReader in) throws IOException { JsonObject object = gson.getAdapter(JsonElement.class).read(in).getAsJsonObject(); if (object.has("Items")) { JsonArray array = object.get("Items").getAsJsonArray(); List<Appointment> appointmentList = new ArrayList<Appointment>(); for (JsonElement element : array) { JsonObject object1 = element.getAsJsonObject(); Appointment appointment = gson.fromJson(object1, Appointment.class); try { appointment.startDate = DateUtil.stringToDate(appointment.startDateString); appointment.endDate = DateUtil.stringToDate(appointment.endDateString); } catch (ParseException e) { e.printStackTrace();//from ww w . j a v a 2 s. c om } appointment.type = gson.getAdapter(AppointmentType.class) .fromJsonTree(object1.getAsJsonPrimitive("Type")); appointment.displayType = gson.getAdapter(DisplayType.class) .fromJsonTree(object1.getAsJsonPrimitive("WeergaveType")); appointment.infoType = gson.getAdapter(InfoType.class) .fromJsonTree(object1.getAsJsonPrimitive("InfoType")); appointmentList.add(appointment); } return appointmentList.toArray(new Appointment[appointmentList.size()]); } else { Appointment appointment = gson.fromJson(object, Appointment.class); try { appointment.startDate = DateUtil.stringToDate(appointment.startDateString); appointment.endDate = DateUtil.stringToDate(appointment.endDateString); } catch (ParseException e) { e.printStackTrace(); } appointment.type = gson.getAdapter(AppointmentType.class) .fromJsonTree(object.getAsJsonPrimitive("Type")); appointment.displayType = gson.getAdapter(DisplayType.class) .fromJsonTree(object.getAsJsonPrimitive("WeergaveType")); appointment.infoType = gson.getAdapter(InfoType.class) .fromJsonTree(object.getAsJsonPrimitive("InfoType")); return new Appointment[] { appointment }; } }
From source file:net.kyori.text.serializer.gson.GsonComponentSerializer.java
License:MIT License
private BuildableComponent<?, ?> deserialize0(final JsonElement element, final JsonDeserializationContext context) throws JsonParseException { if (element.isJsonPrimitive()) { return TextComponent.of(element.getAsString()); } else if (element.isJsonArray()) { ComponentBuilder<?, ?> parent = null; for (final JsonElement childElement : element.getAsJsonArray()) { final BuildableComponent<?, ?> child = this.deserialize0(childElement, context); if (parent == null) { parent = child.toBuilder(); } else { parent.append(child);/* ww w. j av a 2 s.c om*/ } } if (parent == null) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } return parent.build(); } else if (!element.isJsonObject()) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } final JsonObject object = element.getAsJsonObject(); final ComponentBuilder<?, ?> component; if (object.has(TEXT)) { component = TextComponent.builder(object.get(TEXT).getAsString()); } else if (object.has(TRANSLATE)) { final String key = object.get(TRANSLATE).getAsString(); if (!object.has(TRANSLATE_WITH)) { component = TranslatableComponent.builder(key); } else { final JsonArray with = object.getAsJsonArray(TRANSLATE_WITH); final List<Component> args = new ArrayList<>(with.size()); for (int i = 0, size = with.size(); i < size; i++) { final JsonElement argElement = with.get(i); args.add(this.deserialize0(argElement, context)); } component = TranslatableComponent.builder(key).args(args); } } else if (object.has(SCORE)) { final JsonObject score = object.getAsJsonObject(SCORE); if (!score.has(SCORE_NAME) || !score.has(SCORE_OBJECTIVE)) { throw new JsonParseException( "A score component requires a " + SCORE_NAME + " and " + SCORE_OBJECTIVE); } // score components can have a value sometimes, let's grab it if (score.has(SCORE_VALUE)) { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()) .value(score.get(SCORE_VALUE).getAsString()); } else { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()); } } else if (object.has(SELECTOR)) { component = SelectorComponent.builder().pattern(object.get(SELECTOR).getAsString()); } else if (object.has(KEYBIND)) { component = KeybindComponent.builder().keybind(object.get(KEYBIND).getAsString()); } else if (object.has(NBT)) { final String nbt = object.get(NBT).getAsString(); final boolean interpret = object.has(NBT_INTERPRET) && object.getAsJsonPrimitive(NBT_INTERPRET).getAsBoolean(); if (object.has(NBT_BLOCK)) { final BlockNbtComponent.Pos position = context.deserialize(object.get(NBT_BLOCK), BlockNbtComponent.Pos.class); component = BlockNbtComponent.builder().nbtPath(nbt).interpret(interpret).pos(position); } else if (object.has(NBT_ENTITY)) { component = EntityNbtComponent.builder().nbtPath(nbt).interpret(interpret) .selector(object.get(NBT_ENTITY).getAsString()); } else { throw notSureHowToDeserialize(element); } } else { throw notSureHowToDeserialize(element); } if (object.has(EXTRA)) { final JsonArray extra = object.getAsJsonArray(EXTRA); for (int i = 0, size = extra.size(); i < size; i++) { final JsonElement extraElement = extra.get(i); component.append(this.deserialize0(extraElement, context)); } } final Style style = context.deserialize(element, Style.class); if (!style.isEmpty()) { component.style(style); } return component.build(); }
From source file:net.kyori.text.serializer.gson.StyleSerializer.java
License:MIT License
private Style deserialize(final JsonObject json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Style.Builder style = Style.builder(); if (json.has(COLOR)) { final TextColorWrapper color = context.deserialize(json.get(COLOR), TextColorWrapper.class); if (color.color != null) { style.color(color.color);/*from w w w . ja v a2s . c om*/ } else if (color.decoration != null) { style.decoration(color.decoration, true); } } for (final TextDecoration decoration : DECORATIONS) { final String name = TextDecoration.NAMES.name(decoration); if (json.has(name)) { style.decoration(decoration, json.get(name).getAsBoolean()); } } if (json.has(INSERTION)) { style.insertion(json.get(INSERTION).getAsString()); } if (json.has(CLICK_EVENT)) { final JsonObject clickEvent = json.getAsJsonObject(CLICK_EVENT); if (clickEvent != null) { final /* @Nullable */ JsonPrimitive rawAction = clickEvent.getAsJsonPrimitive(CLICK_EVENT_ACTION); final ClickEvent./*@Nullable*/ Action action = rawAction == null ? null : context.deserialize(rawAction, ClickEvent.Action.class); if (action != null && action.readable()) { final /* @Nullable */ JsonPrimitive rawValue = clickEvent.getAsJsonPrimitive(CLICK_EVENT_VALUE); final /* @Nullable */ String value = rawValue == null ? null : rawValue.getAsString(); if (value != null) { style.clickEvent(ClickEvent.of(action, value)); } } } } if (json.has(HOVER_EVENT)) { final JsonObject hoverEvent = json.getAsJsonObject(HOVER_EVENT); if (hoverEvent != null) { final /* @Nullable */ JsonPrimitive rawAction = hoverEvent.getAsJsonPrimitive(HOVER_EVENT_ACTION); final HoverEvent./*@Nullable*/ Action action = rawAction == null ? null : context.deserialize(rawAction, HoverEvent.Action.class); if (action != null && action.readable()) { final /* @Nullable */ JsonElement rawValue = hoverEvent.get(HOVER_EVENT_VALUE); final /* @Nullable */ Component value = rawValue == null ? null : context.deserialize(rawValue, Component.class); if (value != null) { style.hoverEvent(HoverEvent.of(action, value)); } } } } return style.build(); }