com.google.gson.JsonDeserializationContext Java Examples

The following examples show how to use com.google.gson.JsonDeserializationContext. 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: ReflexJson.java    From arcusplatform with Apache License 2.0 7 votes vote down vote up
private static ReflexMatchZigbeeAttribute convertZa(JsonObject za, JsonDeserializationContext context) {
   ReflexMatchZigbeeAttribute.Type type = ReflexMatchZigbeeAttribute.Type.valueOf(za.get("r").getAsString());
   int pr = za.get("p").getAsInt();
   int ep = za.get("e").getAsInt();
   int cl = za.get("c").getAsInt();
   int at = za.get("a").getAsInt();

   Integer manuf = null;
   Integer flags = null;
   if (za.has("m")) {
      manuf = za.get("m").getAsInt();
   }

   if (za.has("f")) {
      flags = za.get("f").getAsInt();
   }

   ZclData vl = null;
   JsonElement jvl = za.get("v");
   if (jvl != null && !jvl.isJsonNull()) {
      vl = ZclData.serde().fromBytes(ByteOrder.LITTLE_ENDIAN, Base64.decodeBase64(jvl.getAsString()));
   }

   return new ReflexMatchZigbeeAttribute(type, pr, ep, cl, at, vl, manuf, flags);
}
 
Example #3
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexMatchZigbeeIasZoneStatus convertZz(JsonObject zz, JsonDeserializationContext context) {
   ReflexMatchZigbeeIasZoneStatus.Type type = ReflexMatchZigbeeIasZoneStatus.Type.valueOf(zz.get("r").getAsString());
   int pr = zz.get("p").getAsInt();
   int ep = zz.get("e").getAsInt();
   int cl = zz.get("c").getAsInt();
   int st = zz.get("s").getAsInt();
   int us = zz.get("u").getAsInt();
   int mx = zz.get("m").getAsInt();

   Integer manuf = null;
   Integer flags = null;
   if (zz.has("v")) {
      manuf = zz.get("v").getAsInt();
   }

   if (zz.has("f")) {
      flags = zz.get("f").getAsInt();
   }

   return new ReflexMatchZigbeeIasZoneStatus(type, pr, ep, cl, st, us, mx, manuf, flags);
}
 
Example #4
Source File: ripe_md160_object.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ripe_md160_object deserialize(JsonElement json,
                                     Type typeOfT,
                                     JsonDeserializationContext context) throws JsonParseException {
    ripe_md160_object ripemd160Object = new ripe_md160_object();
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    byte[] byteContent = encoding.decode(json.getAsString());
    if (byteContent.length != 20) {
        throw new JsonParseException("ripe_md160_object size not correct.");
    }
    for (int i = 0; i < 5; ++i) {
        ripemd160Object.hash[i] = ((byteContent[i * 4 + 3] & 0xff) << 24) |
                ((byteContent[i * 4 + 2] & 0xff) << 16) |
                ((byteContent[i * 4 + 1] & 0xff) << 8) |
                ((byteContent[i * 4 ] & 0xff));
    }

    return ripemd160Object;
}
 
Example #5
Source File: operations.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
@Override
public operation_type deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    operation_type operationType = new operation_type();
    JsonArray jsonArray = json.getAsJsonArray();

    operationType.nOperationType = jsonArray.get(0).getAsInt();
    Type type = operations_map.getOperationObjectById(operationType.nOperationType);

    if (type != null) {
        operationType.operationContent = context.deserialize(jsonArray.get(1), type);
    } else {
        operationType.operationContent = context.deserialize(jsonArray.get(1), Object.class);
    }

    return operationType;
}
 
Example #6
Source File: gson_common_deserializer.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Date deserialize(JsonElement json,
                        Type typeOfT,
                        JsonDeserializationContext context) throws JsonParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    try {
        Date dateResult = simpleDateFormat.parse(json.getAsString());

        return dateResult;
    } catch (ParseException e) {
        e.printStackTrace();
        throw new JsonParseException(e.getMessage() + json.getAsString());
    }
}
 
Example #7
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 #8
Source File: AuthenticatorMakeCredentialOptions.java    From android-webauthn-authenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<Pair<String, Long>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    List<Pair<String, Long>> credTypes = new ArrayList<>();
    for (JsonElement element : json.getAsJsonArray()) {
        // all elements are arrays like ["public-key", "-7"]
        JsonArray pair = element.getAsJsonArray();
        String type = pair.get(0).getAsString();
        try {
            long alg = Long.parseLong(pair.get(1).getAsString());
            credTypes.add(new Pair<>(type, alg));
        } catch (NumberFormatException e) {
            continue;
        }
    }
    return credTypes;
}
 
Example #9
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexActionAlertmeLifesign convertAlAction(JsonObject al, JsonDeserializationContext context) {
   ReflexActionAlertmeLifesign.Type a = ReflexActionAlertmeLifesign.Type.valueOf(al.get("a").getAsString());

   Double m = null;
   Double n = null;

   JsonElement me = al.get("m");
   if (me != null && !me.isJsonNull()) {
      m = me.getAsDouble();
   }

   JsonElement ne = al.get("n");
   if (ne != null && !ne.isJsonNull()) {
      n = ne.getAsDouble();
   }

   if (m != null && n != null) {
      return new ReflexActionAlertmeLifesign(a,m,n);
   } else {
      return new ReflexActionAlertmeLifesign(a);
   }
}
 
Example #10
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 #11
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 #12
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 #13
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexActionAlertmeLifesign convertAlAction(JsonObject al, JsonDeserializationContext context) {
   ReflexActionAlertmeLifesign.Type a = ReflexActionAlertmeLifesign.Type.valueOf(al.get("a").getAsString());

   Double m = null;
   Double n = null;

   JsonElement me = al.get("m");
   if (me != null && !me.isJsonNull()) {
      m = me.getAsDouble();
   }

   JsonElement ne = al.get("n");
   if (ne != null && !ne.isJsonNull()) {
      n = ne.getAsDouble();
   }

   if (m != null && n != null) {
      return new ReflexActionAlertmeLifesign(a,m,n);
   } else {
      return new ReflexActionAlertmeLifesign(a);
   }
}
 
Example #14
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 #15
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexActionLog convertLg(JsonObject lg, JsonDeserializationContext context) {
   ReflexActionLog.Level l = ReflexActionLog.Level.valueOf(lg.get("l").getAsString());
   String m = lg.get("m").getAsString();

   JsonElement jargsc = lg.get("a");
   if (jargsc != null && jargsc.isJsonArray()) {
      JsonArray jargs = jargsc.getAsJsonArray();
      ImmutableList.Builder<ReflexActionLog.Arg> bld = ImmutableList.builder();
      for (JsonElement elem : jargs) {
         bld.add(ReflexActionLog.Arg.valueOf(elem.getAsString()));
      }

      return new ReflexActionLog(l, m, bld.build());
   } else {
      return new ReflexActionLog(l, m, ImmutableList.<ReflexActionLog.Arg>of());
   }
}
 
Example #16
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static ReflexMatchAlertmeLifesign convertAl(JsonObject al, JsonDeserializationContext context) {
   int pr = al.get("p").getAsInt();
   int ep = al.get("e").getAsInt();
   int cl = al.get("c").getAsInt();
   int st = al.get("s").getAsInt();
   int us = al.get("u").getAsInt();

   return new ReflexMatchAlertmeLifesign(pr, ep, cl, st, us);
}
 
Example #17
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static ReflexActionZigbeeIasZoneEnroll convertZzAction(JsonObject zz, JsonDeserializationContext context) {
   int pr = zz.get("p").getAsInt();
   int ep = zz.get("e").getAsInt();
   int cl = zz.get("c").getAsInt();

   return new ReflexActionZigbeeIasZoneEnroll(ep, pr, cl);
}
 
Example #18
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ReflexDriverDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject drv = json.getAsJsonObject();
   int fmt = drv.get("fmt").getAsInt();
   if (fmt == Format.V1.num) {
      return parseV1(drv, context);
   }

   throw new JsonParseException("unknown hub local reflex db format: " + fmt);
}
 
Example #19
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ReflexDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject obj = json.getAsJsonObject();
   JsonElement ms = obj.get("m");
   JsonElement as = obj.get("a");
   return new ReflexDefinition(convertMatches(ms,context), convertActions(as,context));
}
 
Example #20
Source File: AttributeMapSerializer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
 public AttributeMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
AttributeMap map = AttributeMap.newMap(); 
  JsonArray ja = json.getAsJsonArray();
  for(JsonElement e: ja) {
  	deserializeEntry(e, context, map);
  }
  return map;
 }
 
Example #21
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static ReflexMatch convertMatch(JsonObject match, JsonDeserializationContext context) {
   switch (match.get("t").getAsString()) {
   case "AT": return convertAt(match,context);
   case "LC": return convertLc(match,context);
   case "MG": return convertMg(match,context);
   case "PR": return convertPr(match,context);
   case "ZA": return convertZa(match,context);
   case "ZZ": return convertZz(match,context);
   case "AL": return convertAl(match,context);
   default: throw new RuntimeException("unknown matcher type: " + match);
   }
}
 
Example #22
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 5 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);

   JsonElement rsp = pl.get("r");
   return new ReflexActionSendPlatform(evt, args, rsp != null && rsp.getAsBoolean());
}
 
Example #23
Source File: ReflexDB.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static ReflexMatchZigbeeAttribute convertZa(JsonObject za, JsonDeserializationContext context) {
   ReflexMatchZigbeeAttribute.Type type = ReflexMatchZigbeeAttribute.Type.valueOf(za.get("r").getAsString());
   int pr = za.get("p").getAsInt();
   int ep = za.get("e").getAsInt();
   int cl = za.get("c").getAsInt();
   int at = za.get("a").getAsInt();

   ZclData vl = null;
   JsonElement jvl = za.get("v");
   if (jvl != null && !jvl.isJsonNull()) {
      vl = ZclData.serde().fromBytes(ByteOrder.LITTLE_ENDIAN, Base64.decodeBase64(jvl.getAsString()));
   }

   return new ReflexMatchZigbeeAttribute(type, pr, ep, cl, at, vl, null, null);
}
 
Example #24
Source File: DefaultPrincipalTypeAdapter.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
 public DefaultPrincipal deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
DefaultPrincipal principal = null;
  if (json.isJsonObject()) {
  	JsonObject obj = json.getAsJsonObject();
  	String userName = context.deserialize(obj.get(ATTR_USER_NAME), String.class);
  	UUID userId = context.deserialize(obj.get(ATTR_USER_ID), UUID.class);
  	principal = new DefaultPrincipal(userName, userId);
  }
  return principal;
 }
 
Example #25
Source File: PrincipalCollectionTypeAdapter.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public SimplePrincipalCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
 JsonObject src = json.getAsJsonObject();
 SimplePrincipalCollection spc = new SimplePrincipalCollection();
 
 JsonElement principalsElement = src.get(ATTR_PRINCIPAL_MAP);
 if (principalsElement.isJsonArray()) {
 	JsonArray principalsArray = principalsElement.getAsJsonArray();
  Iterator<JsonElement> principalsIterator = principalsArray.iterator();
  while (principalsIterator.hasNext()) {
  	JsonElement realmElement = principalsIterator.next();
  	if (realmElement.isJsonObject()) {
  		JsonObject realmObject = realmElement.getAsJsonObject();
  		for (Map.Entry<String, JsonElement> entry : realmObject.entrySet()) {
  			if (entry.getValue().isJsonArray()) {
  				Iterator<JsonElement> realmIterator = entry.getValue().getAsJsonArray().iterator();
  				while (realmIterator.hasNext()) {
  					spc.add(context.deserialize(realmIterator.next(), DefaultPrincipal.class), entry.getKey());
  				}
  			}
  		}
  	}
  }
 }
 
 return spc;
}
 
Example #26
Source File: ScopeSerDer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Scope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject obj = json.getAsJsonObject();
   return new Scope(
      obj.get(ATTR_TYPE).getAsString(),
      obj.get(ATTR_TOKEN).getAsString()
   );
}
 
Example #27
Source File: HeaderSerDer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Header deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject obj = json.getAsJsonObject();
   JsonElement token = obj.get(ATTR_CORRELATION_TOKEN);
   return new Header(
      obj.get(ATTR_MESSAGE_ID).getAsString(),
      obj.get(ATTR_NAME).getAsString(),
      obj.get(ATTR_NAMESPACE).getAsString(),
      obj.get(ATTR_PAYLOAD_VERSION).getAsString(),
      token == null || token.isJsonNull() ? null : token.getAsString()
   );
}
 
Example #28
Source File: EndpointSerDer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Endpoint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject obj = json.getAsJsonObject();
   return new Endpoint(
      context.deserialize(obj.get(ATTR_SCOPE), Scope.class),
      obj.get(ATTR_ENDPOINTID).getAsString(),
      context.deserialize(obj.get(ATTR_COOKIE), COOKIE_TYPE.getType())
   );
}
 
Example #29
Source File: AlexaMessageFacadeSerDer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public AlexaMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
   JsonObject obj = json.getAsJsonObject();
   if(obj.has(SerDer.ATTR_DIRECTIVE)) {
      return v3.deserialize(json, typeOfT, context);
   }
   return v2.deserialize(json, typeOfT, context);
}
 
Example #30
Source File: HttpSmartyStreetsClient.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(JsonElement addressElement, Type typeOfT, JsonDeserializationContext context)
   throws JsonParseException
{
   JsonObject addressObject = addressElement.getAsJsonObject();

   String line1 = getMemberAsString(addressObject, "delivery_line_1");
   String line2 = getMemberAsString(addressObject, "delivery_line_2");

   JsonObject componentsObject = addressObject.get("components").getAsJsonObject();

   String city     = getMemberAsString(componentsObject, "city_name");
   String state    = getMemberAsString(componentsObject, "state_abbreviation");
   String zip5     = getMemberAsString(componentsObject, "zipcode");
   String zipPlus4 = getMemberAsString(componentsObject, "plus4_code");

   String zip = StreetAddress.zipBuilder()
         .withZip(zip5)
         .withZipPlus4(zipPlus4)
         .build();

   T streetAddress = newStreetAddress();

   streetAddress.setLine1(line1);
   streetAddress.setLine2(line2);
   streetAddress.setCity(city);
   streetAddress.setState(state);
   streetAddress.setZip(zip);

   return streetAddress;
}