Java Code Examples for com.google.gson.JsonDeserializationContext#deserialize()

The following examples show how to use com.google.gson.JsonDeserializationContext#deserialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientMessageTypeAdapter.java    From arcusplatform with Apache License 2.0 7 votes vote down vote up
@Override
 public ClientMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject src = json.getAsJsonObject();
if(src.isJsonNull()) {
	return null;
}
try {
   String type = context.deserialize(src.get(ATTR_TYPE), String.class);
   Map<String,Object> headers = context.deserialize(src.get(ATTR_HEADERS), TYPE_MAP.getType());
   JsonObject payload = src.getAsJsonObject(ATTR_PAYLOAD);
   
   EventDefinition event = definitions.getEvent(type);
   Map<String, Object> attributes = deserializeEvent(payload.get(ATTR_ATTRIBUTES).getAsJsonObject(), event, context);

 		return 
 		      ClientMessage
    		      .builder()
    		      .withHeaders(headers)
                .withType(type)
    		      .withAttributes(attributes)
    		      .create();
}
catch(Exception e) {
	throw new JsonParseException("Unable to create ClientMessage", e);
}
 }
 
Example 2
Source File: ClientMessageTypeAdapter.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> deserializeEvent(JsonObject object, EventDefinition event, JsonDeserializationContext ctx) {
   if(event == null) {
      return (Map<String, Object>)ctx.deserialize(object, TYPE_MAP.getType());
   }
   
   // TODO cache this
   Map<String, ParameterDefinition> parameters = toMap(event.getParameters());
   
   Map<String, Object> attributes = new HashMap<String, Object>();
   for(Map.Entry<String, JsonElement> entry: object.entrySet()) {
      String name = entry.getKey();
      ParameterDefinition parameter = parameters.get(name);
      Object value = deserializeAttribute(entry.getValue(), parameter != null ? parameter.getType() : null, ctx);
      if(value != null) {
         attributes.put(name, value);
      }
   }
   return attributes;
}
 
Example 3
Source File: ListTypeAdapter.java    From EasyHttp with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    // 如果这是一个数组
    if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        // 获取 List 上的泛型
        Type itemType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
        List list = new ArrayList();
        for (int i = 0; i < array.size(); i++) {
            JsonElement element = array.get(i);
            // 解析 List 中的条目对象
            Object item = context.deserialize(element, itemType);
            list.add(item);
        }
        return list;
    } else {
        // 和接口类型不符,直接返回 null
        return null;
    }
}
 
Example 4
Source File: AlexaMessageV2SerDer.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public AlexaMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   try {
      JsonObject obj = json.getAsJsonObject();
      Header h = context.deserialize(obj.get(SerDer.ATTR_HEADER), Header.class);
      Class clazz = getPayloadClass(h.getName());
      logger.debug("deserializing payload type {}", clazz);

      Payload p = context.deserialize(obj.get(SerDer.ATTR_PAYLOAD), clazz);
      if(p == null) {
         logger.debug("got back null, returning new instance of {}", clazz);
         p = (Payload) clazz.getConstructor().newInstance();
      }
      return new AlexaMessage(h, p);
   } catch(Exception e) {
      throw new JsonParseException(e);
   }
}
 
Example 5
Source File: ResultTypeAdapter.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
 public Result deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject src = json.getAsJsonObject();
if(src.isJsonNull()) {
	return null;
}

try {
 		if(src.get(ATTR_ERRSMG) != null) {
 		   return Results.fromError(new Exception(src.get(ATTR_ERRSMG).getAsString()));
 		}
 		Class valueType = Class.forName(src.get(ATTR_VALUE_TYPE).getAsString());
 		Object value = context.deserialize(src.get(ATTR_VALUE), valueType);
 		return Results.fromValue(value);
}
catch(Exception e) {
	throw new JsonParseException("Unable to create PlatformMessage", e);
}
 }
 
Example 6
Source File: ClientMessageTypeAdapter.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
 public ClientMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject src = json.getAsJsonObject();
if(src.isJsonNull()) {
	return null;
}
try {
   Map<String,Object> headers = context.deserialize(src.get(ATTR_HEADERS), new TypeToken<Map<String, Object>>(){}.getType());
 		MessageBody payload = context.deserialize(src.get(ATTR_PAYLOAD), MessageBody.class);

 		return ClientMessage.builder()
 		      .withHeaders(headers)
 		      .withPayload(payload)
 		      .create();
}
catch(Exception e) {
	throw new JsonParseException("Unable to create ClientMessage", e);
}
 }
 
Example 7
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private ReflexDriverDefinition parseV1(JsonObject drv, JsonDeserializationContext context) throws JsonParseException {
   Version ver = Version.fromRepresentation(drv.get("v").getAsString());
   String drvname = drv.get("n").getAsString();
   String hash = drv.get("h").getAsString();
   long offlineTimeout = drv.get("o").getAsLong();
   ReflexRunMode mode = drv.get("m") == null ? ReflexRunMode.defaultMode() : ReflexRunMode.valueOf(drv.get("m").getAsString());
   List<String> caps = context.deserialize(drv.get("c"), LIST_CAPS);
   List<ReflexDefinition> reflexes = context.deserialize(drv.get("r"), LIST_REFLEXES);

   ReflexDriverDFA dfa = null;
   JsonElement jdfa = drv.get("d");
   if (jdfa != null && !jdfa.isJsonNull()) {
      dfa = context.deserialize(jdfa, REFLEX_DFA);
   }

   Set<String> capabilities = ImmutableSet.copyOf(caps);
   return new ReflexDriverDefinition(drvname, ver, hash, offlineTimeout, capabilities, mode, reflexes, dfa);
}
 
Example 8
Source File: AttributeMapSerializer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void deserializeEntry(JsonElement e, JsonDeserializationContext context, AttributeMap map) {
 JsonArray value = e.getAsJsonArray();
 String key = context.deserialize(value.get(0), String.class);
 Type type = context.deserialize(value.get(1), Type.class);
 Object v = context.deserialize(value.get(2), type);
 
 map.set((AttributeKey) AttributeKey.createType(key, type), v);
}
 
Example 9
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private ReflexDB parseV1(JsonObject rootDb, JsonDeserializationContext context) throws JsonParseException {
   String version = rootDb.get("ver").getAsString();
   Set<Driver> drivers = new HashSet<>();

   JsonArray db = rootDb.get("db").getAsJsonArray();
   for (JsonElement child : db) {
      JsonObject drvset = child.getAsJsonObject();

      String drvname = drvset.get("n").getAsString();
      JsonArray drvs = drvset.get("d").getAsJsonArray();
      for (JsonElement dchild : drvs) {
         JsonObject drv = dchild.getAsJsonObject();

         Version ver = Version.fromRepresentation(drv.get("v").getAsString());
         String hash = drv.get("h").getAsString();
         long offlineTimeout = drv.get("o").getAsLong();
         List<String> caps = context.deserialize(drv.get("c"), LIST_CAPS);
         List<ReflexDefinition> reflexes = context.deserialize(drv.get("r"), LIST_REFLEXES);

         ReflexDFA dfa = null;
         JsonElement jdfa = drv.get("d");
         if (jdfa != null && !jdfa.isJsonNull()) {
            dfa = context.deserialize(jdfa, REFLEX_DFA);
         }

         Set<String> capabilities = ImmutableSet.copyOf(caps);
         drivers.add(new Driver(drvname, ver, hash, offlineTimeout, capabilities, reflexes, dfa));
      }
   }

   return new ReflexDB(version, drivers);
}
 
Example 10
Source File: ContextsDeserializerAdapter.java    From sentry-android with MIT License 5 votes vote down vote up
private @Nullable <T> T parseObject(
    final @NotNull JsonDeserializationContext context,
    final @NotNull JsonObject jsonObject,
    final @NotNull String key,
    final @NotNull Class<T> clazz)
    throws JsonParseException {
  final JsonObject object = jsonObject.getAsJsonObject(key);
  if (object != null && !object.isJsonNull()) {
    return context.deserialize(object, clazz);
  }
  return null;
}
 
Example 11
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexActionSetAttribute convertSa(JsonObject sa, JsonDeserializationContext context) {
   String attr = sa.get("a").getAsString();
   Object val = context.deserialize(sa.get("v"), Object.class);
   return new ReflexActionSetAttribute(attr,val);
}
 
Example 12
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexMatchMessage convertMg(JsonObject mg, JsonDeserializationContext context) {
   MessageBody msg = context.deserialize(mg.get("m"), MessageBody.class);
   return new ReflexMatchMessage(msg);
}
 
Example 13
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexMatchAttribute convertAt(JsonObject at, JsonDeserializationContext context) {
   String attr = at.get("a").getAsString();
   Object val = context.deserialize(at.get("v"), Object.class);
   return new ReflexMatchAttribute(attr,val);
}
 
Example 14
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static List<ReflexAction> convertActions(JsonElement json, JsonDeserializationContext context) {
   return context.deserialize(json, LIST_OF_ACTIONS);
}
 
Example 15
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static List<ReflexMatch> convertMatches(JsonElement json, JsonDeserializationContext context) {
   return context.deserialize(json, LIST_OF_MATCHES);
}
 
Example 16
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexActionSetAttribute convertSa(JsonObject sa, JsonDeserializationContext context) {
   String attr = sa.get("a").getAsString();
   Object val = context.deserialize(sa.get("v"), Object.class);
   return new ReflexActionSetAttribute(attr,val);
}
 
Example 17
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexActionSendPlatform convertPl(JsonObject pl, JsonDeserializationContext context) {
   String evt = pl.get("m").getAsString();
   Map<String,Object> args = context.deserialize(pl.get("a"), SEND_PLATFORM_ARGS);
   return new ReflexActionSendPlatform(evt, args, false);
}
 
Example 18
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexMatchMessage convertMg(JsonObject mg, JsonDeserializationContext context) {
   MessageBody msg = context.deserialize(mg.get("m"), MessageBody.class);
   return new ReflexMatchMessage(msg);
}
 
Example 19
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static ReflexMatchAttribute convertAt(JsonObject at, JsonDeserializationContext context) {
   String attr = at.get("a").getAsString();
   Object val = context.deserialize(at.get("v"), Object.class);
   return new ReflexMatchAttribute(attr,val);
}
 
Example 20
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static List<ReflexMatch> convertMatches(JsonElement json, JsonDeserializationContext context) {
   return context.deserialize(json, LIST_OF_MATCHES);
}