List of usage examples for com.google.gson JsonSerializationContext serialize
public JsonElement serialize(Object src);
From source file:net.kyori.text.serializer.gson.GsonComponentSerializer.java
License:MIT License
@Deprecated @Override/*from w w w . ja va2 s . c om*/ public JsonElement serialize(final Component src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject object = new JsonObject(); if (src instanceof TextComponent) { object.addProperty(TEXT, ((TextComponent) src).content()); } else if (src instanceof TranslatableComponent) { final TranslatableComponent tc = (TranslatableComponent) src; object.addProperty(TRANSLATE, tc.key()); if (!tc.args().isEmpty()) { final JsonArray with = new JsonArray(); for (final Component arg : tc.args()) { with.add(context.serialize(arg)); } object.add(TRANSLATE_WITH, with); } } else if (src instanceof ScoreComponent) { final ScoreComponent sc = (ScoreComponent) src; final JsonObject score = new JsonObject(); score.addProperty(SCORE_NAME, sc.name()); score.addProperty(SCORE_OBJECTIVE, sc.objective()); // score component value is optional if (sc.value() != null) score.addProperty(SCORE_VALUE, sc.value()); object.add(SCORE, score); } else if (src instanceof SelectorComponent) { object.addProperty(SELECTOR, ((SelectorComponent) src).pattern()); } else if (src instanceof KeybindComponent) { object.addProperty(KEYBIND, ((KeybindComponent) src).keybind()); } else if (src instanceof NbtComponent) { final NbtComponent<?, ?> nc = (NbtComponent<?, ?>) src; object.addProperty(NBT, nc.nbtPath()); object.addProperty(NBT_INTERPRET, nc.interpret()); if (src instanceof BlockNbtComponent) { final JsonElement position = context.serialize(((BlockNbtComponent) nc).pos()); object.add(NBT_BLOCK, position); } else if (src instanceof EntityNbtComponent) { object.addProperty(NBT_ENTITY, ((EntityNbtComponent) nc).selector()); } else { throw notSureHowToSerialize(src); } } else { throw notSureHowToSerialize(src); } if (!src.children().isEmpty()) { final JsonArray extra = new JsonArray(); for (final Component child : src.children()) { extra.add(context.serialize(child)); } object.add(EXTRA, extra); } if (src.hasStyling()) { final JsonElement style = context.serialize(src.style()); if (style.isJsonObject()) { for (final Map.Entry<String, JsonElement> entry : ((JsonObject) style).entrySet()) { object.add(entry.getKey(), entry.getValue()); } } } return object; }
From source file:net.kyori.text.serializer.gson.StyleSerializer.java
License:MIT License
@Override public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject json = new JsonObject(); for (final TextDecoration decoration : DECORATIONS) { final TextDecoration.State state = src.decoration(decoration); if (state != TextDecoration.State.NOT_SET) { final String name = TextDecoration.NAMES.name(decoration); json.addProperty(name, state == TextDecoration.State.TRUE); }//w w w . ja va2 s .c o m } final /* @Nullable */ TextColor color = src.color(); if (color != null) { json.add(COLOR, context.serialize(color)); } final /* @Nullable */ String insertion = src.insertion(); if (insertion != null) { json.add(INSERTION, context.serialize(insertion)); } final /* @Nullable */ ClickEvent clickEvent = src.clickEvent(); if (clickEvent != null) { final JsonObject eventJson = new JsonObject(); eventJson.add(CLICK_EVENT_ACTION, context.serialize(clickEvent.action())); eventJson.addProperty(CLICK_EVENT_VALUE, clickEvent.value()); json.add(CLICK_EVENT, eventJson); } final /* @Nullable */ HoverEvent hoverEvent = src.hoverEvent(); if (hoverEvent != null) { final JsonObject eventJson = new JsonObject(); eventJson.add(HOVER_EVENT_ACTION, context.serialize(hoverEvent.action())); eventJson.add(HOVER_EVENT_VALUE, context.serialize(hoverEvent.value())); json.add(HOVER_EVENT, eventJson); } return json; }
From source file:net.minecrell.serverlistplus.core.util.ISO8601Serializer.java
License:Open Source License
@Override public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { return src != null ? context.serialize(iso8601.format(src)) : context.serialize(null); }
From source file:net.packet.serializer.DeviceSerializer.java
License:Open Source License
@Override public JsonElement serialize(Device src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); if (StringUtils.isNotBlank(src.getHostname())) { jsonObject.addProperty("hostname", src.getHostname()); }// ww w . ja v a2 s.c o m if (null != src.getPlan() && StringUtils.isNotBlank(src.getPlan().getSlug())) { jsonObject.addProperty("plan", src.getPlan().getSlug()); } if (null != src.getBillingCycle()) { jsonObject.addProperty("billing_cycle", src.getBillingCycle().getValue()); } if (null != src.getOperatingSystem() && StringUtils.isNotBlank(src.getOperatingSystem().getSlug())) { jsonObject.addProperty("operating_system", src.getOperatingSystem().getSlug()); } if (null != src.getFacility() && StringUtils.isNotBlank(src.getFacility().getCode())) { jsonObject.addProperty("facility", src.getFacility().getCode()); } if (StringUtils.isNotBlank(src.getUserdata())) { jsonObject.addProperty("userdata", src.getUserdata()); } if (null != src.isLocked()) { jsonObject.addProperty("locked", src.isLocked().booleanValue()); } if (null != src.getFeatures()) { final JsonObject features = new JsonObject(); for (Map.Entry<String, String> entry : src.getFeatures().entrySet()) { features.addProperty(entry.getKey(), entry.getValue()); } jsonObject.add("features", features); } if (null != src.getTags()) { final JsonArray tags = new JsonArray(); for (String tag : src.getTags()) { tags.add(context.serialize(tag)); } jsonObject.add("tags", tags); } return jsonObject; }
From source file:net.sf.uadetector.json.internal.data.serializer.BrowserSerializer.java
License:Apache License
@Override public JsonElement serialize(final Browser browser, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObj = new JsonObject(); jsonObj.addProperty(FAMILY.getName(), browser.getFamilyName()); jsonObj.addProperty(ICON.getName(), browser.getIcon()); jsonObj.addProperty(INFO_URL.getName(), browser.getInfoUrl()); if (browser.getOperatingSystem() != null) { jsonObj.addProperty(OPERATING_SYSTEM_HASH.getName(), HashCodeGenerator.generate(browser.getOperatingSystem())); }/*from ww w.j av a 2 s. co m*/ jsonObj.add(PATTERNS.getName(), context.serialize(createHashCodeList(browser.getPatterns()))); jsonObj.addProperty(PRODUCER.getName(), browser.getProducer()); jsonObj.addProperty(PRODUCER_URL.getName(), browser.getProducerUrl()); jsonObj.addProperty(BROWSER_TYPE_HASH.getName(), HashCodeGenerator.generate(browser.getType())); jsonObj.addProperty(URL.getName(), browser.getUrl()); jsonObj.addProperty(HASH.getName(), HashCodeGenerator.generate(browser)); return jsonObj; }
From source file:net.sf.uadetector.json.internal.data.serializer.DataSerializer.java
License:Apache License
@Override public JsonElement serialize(final Data data, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObj = new JsonObject(); jsonObj.add(DataJsonFormat.VERSION_1_0.getKey(), context.serialize(DataJsonFormat.VERSION_1_0.getVersion())); jsonObj.add(VERSION.getName(), context.serialize(data.getVersion())); final List<OperatingSystemPattern> osPatterns = new ArrayList<OperatingSystemPattern>( data.getPatternToOperatingSystemMap().keySet()); Collections.sort(osPatterns, new OrderedPatternPositionComparator<OperatingSystemPattern>()); jsonObj.add(OPERATINGSYSTEMPATTERNS.getName(), context.serialize(osPatterns)); final List<OperatingSystem> systems = new ArrayList<OperatingSystem>(data.getOperatingSystems()); Collections.sort(systems, IdentifiableComparator.INSTANCE); jsonObj.add(OPERATINGSYSTEMS.getName(), context.serialize(systems)); final List<BrowserType> browserTypes = new ArrayList<BrowserType>(findAllBrowserTypes(data.getBrowsers())); Collections.sort(browserTypes, IdentifiableComparator.INSTANCE); jsonObj.add(BROWSERTYPES.getName(), context.serialize(browserTypes)); final List<BrowserPattern> browserPatterns = new ArrayList<BrowserPattern>( data.getPatternToBrowserMap().keySet()); Collections.sort(browserPatterns, new OrderedPatternPositionComparator<BrowserPattern>()); jsonObj.add(BROWSERPATTERNS.getName(), context.serialize(browserPatterns)); final List<Browser> browsers = new ArrayList<Browser>(data.getBrowsers()); Collections.sort(browsers, IdentifiableComparator.INSTANCE); jsonObj.add(BROWSERS.getName(), context.serialize(browsers)); final List<Robot> robots = new ArrayList<Robot>(data.getRobots()); Collections.sort(robots, IdentifiableComparator.INSTANCE); jsonObj.add(ROBOTS.getName(), context.serialize(robots)); final List<DevicePattern> devicePatterns = new ArrayList<DevicePattern>( data.getPatternToDeviceMap().keySet()); Collections.sort(devicePatterns, new OrderedPatternPositionComparator<DevicePattern>()); jsonObj.add(DEVICEPATTERNS.getName(), context.serialize(devicePatterns)); final List<Device> devices = new ArrayList<Device>(data.getDevices()); Collections.sort(devices, IdentifiableComparator.INSTANCE); jsonObj.add(DEVICES.getName(), context.serialize(devices)); return jsonObj; }
From source file:net.sf.uadetector.json.internal.data.serializer.DeviceSerializer.java
License:Apache License
@Override public JsonElement serialize(final Device device, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObj = new JsonObject(); jsonObj.addProperty(ICON.getName(), device.getIcon()); jsonObj.addProperty(INFO_URL.getName(), device.getInfoUrl()); jsonObj.addProperty(NAME.getName(), device.getName()); jsonObj.add(PATTERNS.getName(), context.serialize(createHashCodeList(device.getPatterns()))); jsonObj.addProperty(HASH.getName(), HashCodeGenerator.generate(device)); return jsonObj; }
From source file:net.sf.uadetector.json.internal.data.serializer.OperatingSystemSerializer.java
License:Apache License
@Override public JsonElement serialize(final OperatingSystem os, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObj = new JsonObject(); jsonObj.addProperty(FAMILY.getName(), os.getFamily()); jsonObj.addProperty(ICON.getName(), os.getIcon()); jsonObj.addProperty(INFO_URL.getName(), os.getInfoUrl()); jsonObj.addProperty(NAME.getName(), os.getName()); jsonObj.add(PATTERN_HASHS.getName(), context.serialize(createHashCodeList(os.getPatterns()))); jsonObj.addProperty(PRODUCER.getName(), os.getProducer()); jsonObj.addProperty(PRODUCER_URL.getName(), os.getProducerUrl()); jsonObj.addProperty(URL.getName(), os.getUrl()); jsonObj.addProperty(HASH.getName(), HashCodeGenerator.generate(os)); return jsonObj; }
From source file:net.sf.uadetector.json.internal.data.serializer.OrderedPatternSerializer.java
License:Apache License
@Override public JsonElement serialize(final T pattern, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObj = new JsonObject(); jsonObj.add(PATTERN.getName(), context.serialize(pattern.getPattern())); jsonObj.addProperty(HASH.getName(), HashCodeGenerator.generate(pattern)); return jsonObj; }
From source file:org.ambraproject.rhino.view.article.ArticleIngestionView.java
License:Open Source License
@Override public JsonElement serialize(JsonSerializationContext context) { JsonObject serialized = new JsonObject(); serialized.addProperty("doi", ingestion.getArticle().getDoi()); serialized.addProperty("ingestionNumber", ingestion.getIngestionNumber()); serialized.add("journal", context.serialize(journal)); serialized.addProperty("bucketName", bucketName); if (!Strings.isNullOrEmpty(ingestion.getPreprintDoi())) { serialized.addProperty("preprintDoi", ingestion.getPreprintDoi()); }/*from w w w .j a va 2 s . c o m*/ ArticleItem strikingImage = ingestion.getStrikingImage(); if (strikingImage != null) { serialized.add("strikingImage", context.serialize(ItemSetView.getItemView(strikingImage))); } JsonAdapterUtil.copyWithoutOverwriting(context.serialize(metadata).getAsJsonObject(), serialized); JsonAdapterUtil.copyWithoutOverwriting(context.serialize(customMetadata).getAsJsonObject(), serialized); serialized.remove("assets"); List<AssetMetadataView> assetViews = metadata.getAssets().stream().map(AssetMetadataView::new) .collect(Collectors.toList()); serialized.add("assetsLinkedFromManuscript", context.serialize(assetViews)); return serialized; }