Example usage for com.google.gson JsonSerializationContext serialize

List of usage examples for com.google.gson JsonSerializationContext serialize

Introduction

In this page you can find the example usage for com.google.gson JsonSerializationContext serialize.

Prototype

public JsonElement serialize(Object src, Type typeOfSrc);

Source Link

Document

Invokes default serialization on the specified object passing the specific type information.

Usage

From source file:org.lanternpowered.server.text.gson.JsonTextBaseSerializer.java

License:MIT License

public static void serialize(JsonObject json, Text text, JsonSerializationContext context,
        List<Text> children) {
    final TextColor color = text.getColor();
    if (color != TextColors.NONE) {
        json.addProperty(COLOR, color.getId());
    }//w  ww  .ja v a2 s  .co  m
    final TextStyle style = text.getStyle();
    style.isBold().ifPresent(v -> json.addProperty(BOLD, v));
    style.isItalic().ifPresent(v -> json.addProperty(ITALIC, v));
    style.hasUnderline().ifPresent(v -> json.addProperty(UNDERLINE, v));
    style.hasStrikethrough().ifPresent(v -> json.addProperty(STRIKETHROUGH, v));
    style.isObfuscated().ifPresent(v -> json.addProperty(OBFUSCATED, v));
    if (!children.isEmpty()) {
        json.add(CHILDREN, context.serialize(children.toArray(new Text[children.size()]), Text[].class));
    }
    text.getClickAction().ifPresent(clickAction -> {
        final RawAction raw = LanternTextHelper.raw(clickAction);

        final JsonObject jsonEvent = new JsonObject();
        jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
        jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());

        json.add(CLICK_EVENT, jsonEvent);
    });
    text.getHoverAction().ifPresent(clickAction -> {
        final RawAction raw = LanternTextHelper.raw(clickAction);

        final JsonObject jsonEvent = new JsonObject();
        //noinspection ConstantConditions
        jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
        jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());

        json.add(HOVER_EVENT, jsonEvent);
    });
    text.getShiftClickAction().ifPresent(shiftClickAction -> {
        if (shiftClickAction instanceof ShiftClickAction.InsertText) {
            json.addProperty(INSERTION, ((ShiftClickAction.InsertText) shiftClickAction).getResult());
        }
    });
}

From source file:org.lanternpowered.server.text.gson.JsonTextSerializer.java

License:MIT License

@Override
public JsonElement serialize(Text src, Type typeOfSrc, JsonSerializationContext context) {
    if (src instanceof LiteralText) {
        return context.serialize(src, LiteralText.class);
    } else if (src instanceof TranslatableText) {
        return context.serialize(src, TranslatableText.class);
    } else if (src instanceof ScoreText) {
        return context.serialize(src, ScoreText.class);
    } else if (src instanceof SelectorText) {
        return context.serialize(src, SelectorText.class);
    } else {/*w  ww .  j av  a  2s  . c  o m*/
        throw new IllegalStateException(
                "Attempted to serialize an unsupported text type: " + src.getClass().getName());
    }
}

From source file:org.lanternpowered.server.text.gson.JsonTextTranslatableSerializer.java

License:MIT License

@SuppressWarnings("deprecation")
@Override// w  w w. j  a va2  s .c o  m
public JsonElement serialize(TranslatableText src, Type typeOfSrc, JsonSerializationContext context) {
    final Translation translation = src.getTranslation();
    if (this.networkingFormat && !(translation instanceof MinecraftTranslation)) {
        final ImmutableList<Object> arguments = src.getArguments();
        final Object[] rawArguments = arguments.toArray(new Object[arguments.size()]);
        final Locale locale = currentLocale.get();
        for (int i = 0; i < rawArguments.length; i++) {
            Object object = rawArguments[i];
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                rawArguments[i] = ((LanternTextSerializer) TextSerializers.LEGACY_FORMATTING_CODE)
                        .serialize((Text) object, locale);
            } else {
                rawArguments[i] = object.toString();
            }
        }
        final String content = src.getTranslation().get(locale, rawArguments);
        return JsonTextLiteralSerializer.serializeLiteralText(src, content, context, this.removeComplexity);
    }
    final JsonObject obj = new JsonObject();
    obj.addProperty(TRANSLATABLE, src.getTranslation().getId());
    final ImmutableList<Object> arguments = src.getArguments();
    if (!arguments.isEmpty()) {
        final JsonArray argumentsArray = new JsonArray();
        for (Object object : arguments) {
            // Only primitive strings and text json is allowed,
            // so we need to convert the objects if possible
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                argumentsArray.add(context.serialize(object, Text.class));
            } else {
                argumentsArray.add(new JsonPrimitive(object.toString()));
            }
        }
        obj.add(TRANSLATABLE_ARGS, argumentsArray);
    }
    serialize(obj, src, context);
    return obj;
}

From source file:org.livespark.formmodeler.codegen.template.impl.serialization.FieldSerializer.java

License:Apache License

@Override
public JsonElement serialize(FieldDefinition field, Type type, JsonSerializationContext context) {
    JsonArray ja = new JsonArray();
    ja.add(context.serialize(field, field.getClass()));
    return ja;/*  w ww .ja va 2s.  c  o m*/
}

From source file:org.mgenterprises.openbooks.saving.AbstractSaveableAdapter.java

License:Open Source License

@Override
public JsonElement serialize(Saveable src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    result.add("type", new JsonPrimitive(src.getClass().getCanonicalName()));
    result.add("properties", context.serialize(src, src.getClass()));

    return result;
}

From source file:org.myeslib.jdbi.storage.helpers.gson.RuntimeTypeAdapter.java

License:Apache License

public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    Class<?> srcType = src.getClass();
    String label = subtypeToLabel.get(srcType);
    if (label == null) {
        throw new IllegalArgumentException(
                "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
    }//from   w  w  w.  j  a  va2 s. co m
    JsonElement serialized = context.serialize(src, srcType);
    final JsonObject jsonObject = serialized.getAsJsonObject();
    if (jsonObject.has(typeFieldName)) {
        throw new IllegalArgumentException("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());
    }
    return clone;
}

From source file:org.openhab.binding.tado.internal.api.converter.OverlayTerminationConditionTemplateConverter.java

License:Open Source License

@Override
public JsonElement serialize(OverlayTerminationConditionTemplate src, Type srcType,
        JsonSerializationContext context) {
    if (src instanceof TimerTerminationConditionTemplate) {
        return context.serialize(src, TimerTerminationConditionTemplate.class);
    }//from   www  .  j  a v a2  s . c om

    return context.serialize(src, OverlayTerminationConditionTemplate.class);
}

From source file:org.openhab.binding.tado.internal.api.converter.TerminationConditionConverter.java

License:Open Source License

@Override
public JsonElement serialize(OverlayTerminationCondition src, Type srcType, JsonSerializationContext context) {
    if (src instanceof ManualTerminationCondition) {
        return context.serialize(src, ManualTerminationCondition.class);
    } else if (src instanceof TadoModeTerminationCondition) {
        return context.serialize(src, TadoModeTerminationCondition.class);
    } else if (src instanceof TimerTerminationCondition) {
        return context.serialize(src, TimerTerminationCondition.class);
    }//from   w w  w  .  ja  va  2 s  . co  m

    return new JsonObject();
}

From source file:org.openhab.binding.tado.internal.api.converter.ZoneCapabilitiesConverter.java

License:Open Source License

@Override
public JsonElement serialize(GenericZoneCapabilities src, Type srcType, JsonSerializationContext context) {
    if (src instanceof HeatingCapabilities) {
        return context.serialize(src, HeatingCapabilities.class);
    } else if (src instanceof AirConditioningCapabilities) {
        return context.serialize(src, AirConditioningCapabilities.class);
    } else if (src instanceof HotWaterCapabilities) {
        return context.serialize(src, HotWaterCapabilities.class);
    }//  w  w w  .j  a v  a2  s  .co  m

    return new JsonObject();
}

From source file:org.openhab.binding.tado.internal.api.converter.ZoneSettingConverter.java

License:Open Source License

@Override
public JsonElement serialize(GenericZoneSetting src, Type srcType, JsonSerializationContext context) {
    if (src instanceof HeatingZoneSetting) {
        return context.serialize(src, HeatingZoneSetting.class);
    } else if (src instanceof CoolingZoneSetting) {
        return context.serialize(src, CoolingZoneSetting.class);
    } else if (src instanceof HotWaterZoneSetting) {
        return context.serialize(src, HotWaterZoneSetting.class);
    }/*from   ww w.j  ava2s. c o  m*/

    return new JsonObject();
}