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);

Source Link

Document

Invokes default serialization on the specified object.

Usage

From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeSerializer.java

License:Open Source License

@Override
public JsonElement serialize(LwM2mNode src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();

    element.addProperty("id", src.getId());

    if (typeOfSrc == LwM2mObject.class) {
        element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values()));
    } else if (typeOfSrc == LwM2mObjectInstance.class) {
        element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
    } else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) {
        LwM2mResource rsc = (LwM2mResource) src;
        if (rsc.isMultiInstances()) {
            JsonObject values = new JsonObject();
            for (Entry<Integer, ?> entry : rsc.getValues().entrySet()) {
                if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
                    values.add(entry.getKey().toString(),
                            context.serialize(Hex.encodeHex((byte[]) entry.getValue())));
                } else {
                    values.add(entry.getKey().toString(), context.serialize(entry.getValue()));
                }/* w w  w.  ja v a  2  s.c o m*/
            }
            element.add("values", values);
        } else {
            if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
                element.add("value", context.serialize(Hex.encodeHex((byte[]) rsc.getValue())));
            } else {
                element.add("value", context.serialize(rsc.getValue()));
            }
        }
    }

    return element;
}

From source file:org.eclipse.leshan.server.demo.servlet.json.RegistrationSerializer.java

License:Open Source License

@Override
public JsonElement serialize(Registration src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();

    element.addProperty("endpoint", src.getEndpoint());
    element.addProperty("registrationId", src.getId());
    element.add("registrationDate", context.serialize(src.getRegistrationDate()));
    element.add("lastUpdate", context.serialize(src.getLastUpdate()));
    element.addProperty("address", src.getAddress().getHostAddress() + ":" + src.getPort());
    element.addProperty("smsNumber", src.getSmsNumber());
    element.addProperty("lwM2mVersion", src.getLwM2mVersion());
    element.addProperty("lifetime", src.getLifeTimeInSec());
    element.addProperty("bindingMode", src.getBindingMode().toString());
    element.add("rootPath", context.serialize(src.getRootPath()));
    element.add("objectLinks", context.serialize(src.getSortedObjectLinks()));
    element.add("secure", context.serialize(src.getIdentity().isSecure()));
    element.add("additionalRegistrationAttributes",
            context.serialize(src.getAdditionalRegistrationAttributes()));

    if (src.usesQueueMode()) {
        element.add("sleeping", context.serialize(!presenceService.isClientAwake(src)));
    }/*from  w  w w  .  j av a  2 s  . com*/

    return element;
}

From source file:org.eclipse.leshan.server.demo.servlet.json.ResponseSerializer.java

License:Open Source License

@Override
public JsonElement serialize(LwM2mResponse src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();

    element.addProperty("status", src.getCode().toString());
    element.addProperty("valid", src.isValid());
    element.addProperty("success", src.isSuccess());
    element.addProperty("failure", src.isFailure());

    if (typeOfSrc instanceof Class<?>) {
        if (ReadResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) {
            element.add("content", context.serialize(((ReadResponse) src).getContent()));
        } else if (DiscoverResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) {
            element.add("objectLinks", context.serialize(((DiscoverResponse) src).getObjectLinks()));
        }/*from w w w. jav  a2s.  c  o  m*/
    }

    return element;
}

From source file:org.eclipse.leshan.standalone.servlet.json.ClientSerializer.java

License:Open Source License

@Override
public JsonElement serialize(Client src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();

    element.addProperty("endpoint", src.getEndpoint());
    element.addProperty("registrationId", src.getRegistrationId());
    element.add("registrationDate", context.serialize(src.getRegistrationDate()));
    element.addProperty("address", src.getAddress().toString() + ":" + src.getPort());
    element.addProperty("smsNumber", src.getSmsNumber());
    element.addProperty("lwM2MmVersion", src.getLwM2mVersion());
    element.addProperty("lifetime", src.getLifeTimeInSec());
    element.addProperty("bindingMode", src.getBindingMode().toString());
    element.add("rootPath", context.serialize(src.getRootPath()));
    element.add("objectLinks", context.serialize(src.getSortedObjectLinks()));
    element.add("secure",
            context.serialize(src.getRegistrationEndpointAddress().getPort() == LeshanServerBuilder.PORT_DTLS));

    return element;
}

From source file:org.eclipse.leshan.standalone.servlet.json.LwM2mNodeSerializer.java

License:Open Source License

@Override
public JsonElement serialize(LwM2mNode src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();

    element.addProperty("id", src.getId());

    if (typeOfSrc == LwM2mObject.class) {
        element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values()));
    } else if (typeOfSrc == LwM2mObjectInstance.class) {
        element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
    } else if (typeOfSrc == LwM2mResource.class) {
        LwM2mResource rsc = (LwM2mResource) src;
        if (rsc.isMultiInstances()) {
            Object[] values = new Object[rsc.getValues().length];
            for (int i = 0; i < rsc.getValues().length; i++) {
                if (rsc.getValue().type == DataType.OPAQUE) {
                    values[i] = DatatypeConverter.printHexBinary((byte[]) rsc.getValue().value);
                } else {
                    values[i] = rsc.getValues()[i].value;
                }//from   ww w . j av a  2 s.c o  m
            }
            element.add("values", context.serialize(values));
        } else {
            if (rsc.getValue().type == DataType.OPAQUE) {
                element.add("value",
                        context.serialize(DatatypeConverter.printHexBinary((byte[]) rsc.getValue().value)));
            } else {
                element.add("value", context.serialize(rsc.getValue().value));
            }
        }
    }

    return element;
}

From source file:org.eclipse.leshan.standalone.servlet.json.ResponseSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final LwM2mResponse src, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject element = new JsonObject();

    element.addProperty("status", src.getCode().toString());

    if (typeOfSrc == ValueResponse.class) {
        element.add("content", context.serialize(((ValueResponse) src).getContent()));
    }//w  w  w  .  jav  a2s.  c  o m

    return element;
}

From source file:org.eclipse.recommenders.internal.coordinates.rcp.DependencyInfoJsonTypeAdapter.java

License:Open Source License

@Override
public JsonElement serialize(DependencyInfo src, Type typeOfSrc, JsonSerializationContext context) {
    JsonPrimitive file = new JsonPrimitive(src.getFile().getAbsoluteFile().toString());
    JsonElement type = context.serialize(src.getType());
    JsonElement hints = context.serialize(src.getHints());

    JsonObject jsonObject = new JsonObject();
    jsonObject.add("location", file); //$NON-NLS-1$
    jsonObject.add("type", type); //$NON-NLS-1$
    jsonObject.add("hints", hints); //$NON-NLS-1$
    return jsonObject;
}

From source file:org.eclipse.recommenders.utils.gson.OptionalJsonTypeAdapter.java

License:Open Source License

@Override
public JsonElement serialize(Optional<T> src, Type typeOfSrc, JsonSerializationContext context) {
    if (src.isPresent()) {
        return context.serialize(src.get());
    } else {/* w  ww . j av  a 2s  . c  o m*/
        return new JsonPrimitive(ABSENT);
    }
}

From source file:org.excalibur.core.cloud.service.json.serializers.AnnotationJsonDeserializer.java

License:Open Source License

public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    Field[] fields = src.getClass().getDeclaredFields();

    JsonObject element = new JsonObject();

    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }// ww  w . java2  s .co m

        field.setAccessible(true);
        final String property = getProperty(field);

        if (property == null) {
            continue;
        }

        final Object value = getValue(field, src);

        if (!isComplexType(field.getType())) {
            element.addProperty(property, value == null ? null : value.toString());
        } else {
            element.add(property, context.serialize(value));
        }
    }

    return element;
}

From source file:org.helios.apmrouter.dataservice.json.marshalling.encoders.CompositeDataSupportSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/*w  w  w .j  av a  2s.co m*/
 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext)
 */
@Override
public JsonElement serialize(CompositeDataSupport src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    CompositeType ctype = src.getCompositeType();
    for (String key : ctype.keySet()) {
        if (src.containsKey(key)) {
            OpenType<?> oType = ctype.getType(key);
            if (oType instanceof SimpleType) {
                json.addProperty(key, src.get(key).toString());
            } else {
                json.add(key, context.serialize(src.get(key)));
            }
        }
    }
    return json;
}