List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.ibm.watson.self.topics.TopicClient.java
License:Open Source License
/** * Subscribe to a topic if possible/*ww w . j a v a 2 s .co m*/ * @param path: the path * @param event: the event to subscribe to */ public void subscribe(String path, IEvent event) { logger.entry(); if (!subscriptionMap.containsKey(path)) { subscriptionMap.put(path, event); } JsonObject wrapperObject = new JsonObject(); JsonArray wrapperArray = new JsonArray(); wrapperArray.add(new JsonPrimitive(path)); wrapperObject.add(TopicConstants.TARGETS, wrapperArray); wrapperObject.addProperty(TopicConstants.MSG, TopicConstants.SUBSCRIBE); this.sendMessage(wrapperObject); logger.exit(); }
From source file:com.ibm.watson.self.topics.TopicClient.java
License:Open Source License
/** * Unsubscribe from a given topic// w w w .j a va 2 s . c o m * @param path: the path * @param event: the event to unsubscribe from * @return: true if successfully unscubscribed */ public boolean unsubscribe(String path, IEvent event) { logger.entry(); if (subscriptionMap.containsKey(path)) { subscriptionMap.remove(path); JsonObject wrapperObject = new JsonObject(); JsonArray wrapperArray = new JsonArray(); wrapperArray.add(new JsonPrimitive(path)); wrapperObject.add(TopicConstants.TARGETS, wrapperArray); wrapperObject.addProperty(TopicConstants.MSG, TopicConstants.UNSUBSCRIBE); this.sendMessage(wrapperObject); return logger.exit(true); } return logger.exit(false); }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static JsonElement encode(BasicBSONList a) { JsonArray result = new JsonArray(); for (int i = 0; i < a.size(); ++i) { Object o = a.get(i);//from w ww . j av a 2s .c o m if (o instanceof DBObject) { result.add(encode((DBObject) o)); } else if (o instanceof BasicBSONObject) { result.add(encode((BasicBSONObject) o)); } else if (o instanceof BasicBSONList) { result.add(encode((BasicBSONList) o)); } else if (o instanceof BasicDBList) { result.add(encode((BasicDBList) o)); } else { // Must be a primitive... if (o instanceof String) { result.add(new JsonPrimitive((String) o)); } else if (o instanceof Number) { result.add(new JsonPrimitive((Number) o)); } else if (o instanceof Boolean) { result.add(new JsonPrimitive((Boolean) o)); } // MongoDB special fields else if (o instanceof ObjectId) { JsonObject oid = new JsonObject(); oid.add("$oid", new JsonPrimitive(((ObjectId) o).toString())); result.add(oid); } else if (o instanceof Date) { JsonObject date = new JsonObject(); date.add("$date", new JsonPrimitive(_format.format((Date) o))); result.add(date); } // Ignore BinaryData, should be serializing that anyway... } } return result; }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static JsonElement encode(BSONObject o) { JsonObject result = new JsonObject(); Iterator<?> i = o.keySet().iterator(); while (i.hasNext()) { String k = (String) i.next(); Object v = o.get(k);// w w w . ja va 2 s .co m if (v instanceof BasicBSONList) { result.add(k, encode((BasicBSONList) v)); } else if (v instanceof BasicDBList) { result.add(k, encode((BasicDBList) v)); } else if (v instanceof DBObject) { result.add(k, encode((DBObject) v)); } else if (v instanceof BasicBSONObject) { result.add(k, encode((BasicBSONObject) v)); } else { // Must be a primitive... if (v instanceof String) { result.add(k, new JsonPrimitive((String) v)); } else if (v instanceof Number) { result.add(k, new JsonPrimitive((Number) v)); } else if (v instanceof Boolean) { result.add(k, new JsonPrimitive((Boolean) v)); } // MongoDB special fields else if (v instanceof ObjectId) { JsonObject oid = new JsonObject(); oid.add("$oid", new JsonPrimitive(((ObjectId) v).toString())); result.add(k, oid); } else if (v instanceof Date) { JsonObject date = new JsonObject(); date.add("$date", new JsonPrimitive(_format.format((Date) v))); result.add(k, date); } // Ignore BinaryData, should be serializing that anyway... } } return result; }
From source file:com.impetus.client.couchdb.CouchDBObjectMapper.java
License:Apache License
/** * Gets the json of entity./*from ww w .j ava 2s . co m*/ * * @param m * the m * @param entity * the entity * @param id * the id * @param relations * the relations * @param kunderaMetadata * the kundera metadata * @return the json of entity * @throws OperationNotSupportedException * the operation not supported exception */ static JsonObject getJsonOfEntity(EntityMetadata m, Object entity, Object id, List<RelationHolder> relations, final KunderaMetadata kunderaMetadata) throws OperationNotSupportedException { JsonObject jsonObject = new JsonObject(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); // Add discriminator column and value. String discrColumn = ((AbstractManagedType) entityType).getDiscriminatorColumn(); String discrValue = ((AbstractManagedType) entityType).getDiscriminatorValue(); if (discrValue != null) { jsonObject.add(discrColumn, getJsonPrimitive(discrValue, discrValue.getClass())); } // Populate id attribute. SingularAttribute idAttribute = m.getIdAttribute(); if (metaModel.isEmbeddable(idAttribute.getBindableJavaType())) { Field field = (Field) idAttribute.getJavaMember(); EmbeddableType embeddableType = metaModel.embeddable(idAttribute.getBindableJavaType()); String _id = get_Id(field, PropertyAccessorHelper.getObject(entity, field), embeddableType, m.getTableName()); jsonObject.addProperty("_id", _id); Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) idAttribute.getJavaMember()); Set<Attribute> embeddableAttributes = embeddableType.getAttributes(); jsonObject.add(((AbstractAttribute) idAttribute).getJPAColumnName(), getJsonObject(field.getType().getDeclaredFields(), embeddableType, embeddedObject)); } else { jsonObject.addProperty("_id", m.getTableName() + PropertyAccessorHelper.getString(id)); jsonObject.add(((AbstractAttribute) idAttribute).getJPAColumnName(), getJsonPrimitive(id, idAttribute.getJavaType())); } // Populate columns Set<Attribute> columns = entityType.getAttributes(); for (Attribute column : columns) { if (!column.equals(idAttribute)) { try { Class javaType = ((AbstractAttribute) column).getBindableJavaType(); if (metaModel.isEmbeddable(javaType)) { onEmbeddable(entityType, column, entity, metaModel.embeddable(javaType), jsonObject); } else if (!column.isAssociation()) { Object valueObject = PropertyAccessorHelper.getObject(entity, (Field) column.getJavaMember()); jsonObject.add(((AbstractAttribute) column).getJPAColumnName(), getJsonPrimitive(valueObject, column.getJavaType())); } } catch (PropertyAccessException paex) { log.error("Can't access property {}.", column.getName()); throw new PropertyAccessException(paex); } } } if (relations != null) { for (RelationHolder rh : relations) { jsonObject.add(rh.getRelationName(), getJsonPrimitive(rh.getRelationValue(), rh.getRelationValue().getClass())); } } jsonObject.add(CouchDBConstants.ENTITYNAME, new JsonPrimitive(m.getTableName())); return jsonObject; }
From source file:com.impetus.client.couchdb.CouchDBObjectMapper.java
License:Apache License
/** * Gets the json primitive./*from w w w .ja va2 s.co m*/ * * @param value * the value * @param clazz * the clazz * @return the json primitive */ private static JsonElement getJsonPrimitive(Object value, Class clazz) { if (value != null) { if (clazz.isAssignableFrom(Number.class) || value instanceof Number) { return new JsonPrimitive((Number) value); } else if (clazz.isAssignableFrom(Boolean.class) || value instanceof Boolean) { return new JsonPrimitive((Boolean) value); } else if (clazz.isAssignableFrom(Character.class) || value instanceof Character) { return new JsonPrimitive((Character) value); } else if (clazz.isAssignableFrom(byte[].class) || value instanceof byte[]) { return new JsonPrimitive(PropertyAccessorFactory.STRING.fromBytes(String.class, (byte[]) value)); } else { return new JsonPrimitive(PropertyAccessorHelper.getString(value)); } } return null; }
From source file:com.intuit.ipp.serialization.custom.JAXBElementJsonSerializer.java
License:Apache License
@Override public JsonElement serialize(final JAXBElement<? extends IntuitEntity> jaxbVariable, final Type typeOfSrc, final JsonSerializationContext context) { return new JsonPrimitive(MessageUtils.getGson().toJson(jaxbVariable.getValue())); }
From source file:com.javacreed.examples.gson.part1.BookSerialiser.java
License:Apache License
@Override public JsonElement serialize(final Book book, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("title", book.getTitle()); jsonObject.addProperty("isbn-10", book.getIsbn10()); jsonObject.addProperty("isbn-13", book.getIsbn13()); final JsonArray jsonAuthorsArray = new JsonArray(); for (final String author : book.getAuthors()) { final JsonPrimitive jsonAuthor = new JsonPrimitive(author); jsonAuthorsArray.add(jsonAuthor); }/* w w w . j a va2 s . c o m*/ jsonObject.add("authors", jsonAuthorsArray); return jsonObject; }
From source file:com.javacreed.examples.gson.part1.LargeDataSerialiser.java
License:Apache License
@Override public JsonElement serialize(final LargeData data, final Type typeOfSrc, final JsonSerializationContext context) { final JsonArray jsonNumbers = new JsonArray(); for (final long number : data.getNumbers()) { jsonNumbers.add(new JsonPrimitive(number)); }/* w ww .j a va 2s . c o m*/ final JsonObject jsonObject = new JsonObject(); jsonObject.add("numbers", jsonNumbers); return jsonObject; }