private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
   if (val.isBoolean()) return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
   if (val.isString()) return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
   if (val.isNumber()) {
     if (val.getAsDouble() == (long) val.getAsLong()) {
       return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
     } else {
       return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
     }
   }
   // use string as default value
   return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
 }
 private Object deserializeValue(
     JsonPrimitive val, org.eclipse.leshan.core.model.ResourceModel.Type expectedType) {
   switch (expectedType) {
     case BOOLEAN:
       return val.getAsBoolean();
     case STRING:
       return val.getAsString();
     case INTEGER:
       return val.getAsLong();
     case FLOAT:
       return val.getAsDouble();
     case TIME:
     case OPAQUE:
     default:
       // TODO we need to better handle this.
       return val.getAsString();
   }
 }
 private ConfigurationParam makeConfigParam(String key, JsonPrimitive value)
     throws JsonParseException {
   ConfigurationParam param = new ConfigurationParam();
   String valueStr;
   if (value == null && "mail".equals(key)) {
     valueStr =
         ""; // Ghost 0.8.0 gives an empty string instead of null, when no mail transport is set
     // we don't care about the mail transport anyhow
   } else if (value == null) {
     // FIXME temp log to help debug Crashlytics issue #87
     throw new NullPointerException("value for key '" + key + "' is null!");
   } else if (value.isString()) {
     valueStr = value.getAsString();
   } else if (value.isBoolean()) {
     valueStr = String.valueOf(value.getAsBoolean());
   } else if (value.isNumber()) {
     valueStr = String.valueOf(value.getAsDouble());
   } else {
     throw new JsonParseException("unknown value type in Ghost configuration list");
   }
   param.setKey(key);
   param.setValue(valueStr);
   return param;
 }
Example #4
0
  public Link deserialize(
      JsonElement _jsonElement, Type _type, JsonDeserializationContext _jsonDeserializationContext)
      throws JsonParseException {
    LinkBuilder builder = new LinkBuilder();
    final JsonObject root = _jsonElement.getAsJsonObject();
    final String kindString = root.get("kind").getAsString();
    builder.kind(Kind.getFromRedditType(kindString));
    final JsonObject linkObject = root.get("data").getAsJsonObject();
    final String id = linkObject.get("id").getAsString();
    builder.id(id);
    final String name = linkObject.get("name").getAsString();
    builder.name(name);

    final Double created = linkObject.get("created").getAsDouble();
    builder.created(created.longValue());
    final Double createdUTC = linkObject.get("created_utc").getAsDouble();
    builder.createdUTC(createdUTC.longValue());

    final int ups = linkObject.get("ups").getAsInt();
    builder.ups(ups);
    final int downs = linkObject.get("downs").getAsInt();
    builder.downs(downs);

    final String distinguised = JsonHelper.getStringOrNull(linkObject.get("distinguished"));
    builder.distinguised(distinguised);
    final int numComments = linkObject.get("num_comments").getAsInt();
    builder.numComments(numComments);
    final String title = linkObject.get("title").getAsString();
    builder.title(title);
    final String authorFlairText = JsonHelper.getStringOrNull(linkObject.get("author_flair_text"));
    builder.authorFlairCSSText(authorFlairText);
    final String URL = linkObject.get("url").getAsString();
    builder.URL(URL);
    final boolean saved = linkObject.get("saved").getAsBoolean();
    builder.saved(saved);
    final boolean self = linkObject.get("is_self").getAsBoolean();
    builder.self(self);
    final String permalink = linkObject.get("permalink").getAsString();
    builder.permalink(permalink);
    final String domain = linkObject.get("domain").getAsString();
    builder.domain(domain);
    final String subreddit = linkObject.get("subreddit").getAsString();
    builder.subreddit(subreddit);
    final JsonElement selftextHtmlElement = linkObject.get("selftext_html");
    String selftextHtml = null;
    if (!selftextHtmlElement.isJsonNull()) {
      selftextHtml = selftextHtmlElement.getAsString();
    }
    builder.selfTextHTML(selftextHtml);
    final String selftext = linkObject.get("selftext").getAsString();
    builder.selfText(selftext);
    final Boolean likes = JsonHelper.getBooleanOrNull(linkObject.get("likes"));
    builder.likes(likes);
    final String linkFlairText = JsonHelper.getStringOrNull(linkObject.get("link_flair_text"));
    builder.linkFlairText(linkFlairText);
    final String linkFlairCssClass =
        JsonHelper.getStringOrNull(linkObject.get("link_flair_css_class"));
    builder.linkFlairCSSClass(linkFlairCssClass);
    final String authorFlairCssClass =
        JsonHelper.getStringOrNull(linkObject.get("author_flair_css_class"));
    builder.authorFlairCSSClass(authorFlairCssClass);
    final boolean clicked = linkObject.get("clicked").getAsBoolean();
    builder.clicked(clicked);
    final boolean stickied = linkObject.get("stickied").getAsBoolean();
    builder.stickied(stickied);
    final String author = linkObject.get("author").getAsString();
    builder.author(author);
    final int score = linkObject.get("score").getAsInt();
    builder.score(score);
    final boolean over18 = linkObject.get("over_18").getAsBoolean();
    builder.over18(over18);
    final boolean hidden = linkObject.get("hidden").getAsBoolean();
    builder.hidden(hidden);
    final String thumbnail = linkObject.get("thumbnail").getAsString();
    builder.thumbnail(thumbnail);
    final String subredditId = linkObject.get("subreddit_id").getAsString();
    builder.subredditId(subredditId);
    final JsonPrimitive editedPrimitive = linkObject.get("edited").getAsJsonPrimitive();
    if (editedPrimitive.isNumber()) {
      final Double editedTime = editedPrimitive.getAsDouble();
      builder.editedTime(editedTime.longValue());
      builder.edited(true);
    }

    return builder.build();
  }