com.google.gson.internal.Streams#parse ( )源码实例Demo

下面列出了com.google.gson.internal.Streams#parse ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: gson   文件: JsonParserTest.java
public void testReadWriteTwoObjects() throws Exception {
  Gson gson = new Gson();
  CharArrayWriter writer = new CharArrayWriter();
  BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
  writer.write(gson.toJson(expectedOne).toCharArray());
  BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
  writer.write(gson.toJson(expectedTwo).toCharArray());
  CharArrayReader reader = new CharArrayReader(writer.toCharArray());

  JsonReader parser = new JsonReader(reader);
  parser.setLenient(true);
  JsonElement element1 = Streams.parse(parser);
  JsonElement element2 = Streams.parse(parser);
  BagOfPrimitives actualOne = gson.fromJson(element1, BagOfPrimitives.class);
  assertEquals("one", actualOne.stringValue);
  BagOfPrimitives actualTwo = gson.fromJson(element2, BagOfPrimitives.class);
  assertEquals("two", actualTwo.stringValue);
}
 
源代码2 项目: incubator-gobblin   文件: GsonInterfaceAdapter.java
@Override
public R read(JsonReader in) throws IOException {
  JsonElement element = Streams.parse(in);
  if (element.isJsonNull()) {
    return readNull();
  }
  JsonObject jsonObject = element.getAsJsonObject();

  if (this.typeToken.getRawType() == Optional.class) {
    if (jsonObject.has(OBJECT_TYPE)) {
      return (R) Optional.of(readValue(jsonObject, null));
    } else if (jsonObject.entrySet().isEmpty()) {
      return (R) Optional.absent();
    } else {
      throw new IOException("No class found for Optional value.");
    }
  }
  return this.readValue(jsonObject, this.typeToken);
}
 
源代码3 项目: weixin-java-tools   文件: WxCpServiceImpl.java
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
  if (forceRefresh) {
    wxCpConfigStorage.expireJsapiTicket();
  }
  if (wxCpConfigStorage.isJsapiTicketExpired()) {
    synchronized (globalJsapiTicketRefreshLock) {
      if (wxCpConfigStorage.isJsapiTicketExpired()) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
        String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
        JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
        JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
        String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
        int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
        wxCpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
      }
    }
  }
  return wxCpConfigStorage.getJsapiTicket();
}
 
源代码4 项目: weixin-java-tools   文件: WxMpServiceImpl.java
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
  if (forceRefresh) {
    wxMpConfigStorage.expireJsapiTicket();
  }
  if (wxMpConfigStorage.isJsapiTicketExpired()) {
    synchronized (globalJsapiTicketRefreshLock) {
      if (wxMpConfigStorage.isJsapiTicketExpired()) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
        String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
        JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
        JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
        String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
        int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
        wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
      }
    }
  }
  return wxMpConfigStorage.getJsapiTicket();
}
 
源代码5 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId;
  String params = "";
  if (fetchChild != null) {
    params += "&fetch_child=" + (fetchChild ? "1" : "0");
  }
  if (status != null) {
    params += "&status=" + status;
  } else {
    params += "&status=0";
  }

  String responseContent = get(url, params);
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxCpGsonBuilder.INSTANCE.create()
      .fromJson(
          tmpJsonElement.getAsJsonObject().get("userlist"),
          new TypeToken<List<WxCpUser>>() { }.getType()
      );
}
 
源代码6 项目: weixin-java-tools   文件: WxMpServiceImpl.java
public List<WxMpGroup> groupGet() throws WxErrorException {
  String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
  String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
  /*
   * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
   * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
   */
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"),
      new TypeToken<List<WxMpGroup>>() {
      }.getType());
}
 
源代码7 项目: letv   文件: TreeTypeAdapter.java
public T read(JsonReader in) throws IOException {
    if (this.deserializer == null) {
        return delegate().read(in);
    }
    JsonElement value = Streams.parse(in);
    if (value.isJsonNull()) {
        return null;
    }
    return this.deserializer.deserialize(value, this.typeToken.getType(), this.gson.deserializationContext);
}
 
源代码8 项目: weixin-java-tools   文件: WxMpServiceImpl.java
public String shortUrl(String long_url) throws WxErrorException {
  String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
  JsonObject o = new JsonObject();
  o.addProperty("action", "long2short");
  o.addProperty("long_url", long_url);
  String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}
 
源代码9 项目: MiBandDecompiled   文件: o.java
public Object read(JsonReader jsonreader)
{
    if (b == null)
    {
        return a().read(jsonreader);
    }
    JsonElement jsonelement = Streams.parse(jsonreader);
    if (jsonelement.isJsonNull())
    {
        return null;
    } else
    {
        return b.deserialize(jsonelement, d.getType(), c.b);
    }
}
 
源代码10 项目: gson   文件: TreeTypeAdapter.java
@Override public T read(JsonReader in) throws IOException {
  if (deserializer == null) {
    return delegate().read(in);
  }
  JsonElement value = Streams.parse(in);
  if (value.isJsonNull()) {
    return null;
  }
  return deserializer.deserialize(value, typeToken.getType(), context);
}
 
源代码11 项目: weixin-java-tools   文件: WxMpServiceImpl.java
@Override
public List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
  String url = "https://api.weixin.qq.com/datacube/getusercumulate";
  JsonObject param = new JsonObject();
  param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
  param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
  String responseContent = post(url, param.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
      new TypeToken<List<WxMpUserCumulate>>() {
      }.getType());
}
 
源代码12 项目: weixin-java-tools   文件: WxMpServiceImpl.java
@Override
public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
  String url = "https://api.weixin.qq.com/datacube/getusersummary";
  JsonObject param = new JsonObject();
  param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
  param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
  String responseContent = post(url, param.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"),
      new TypeToken<List<WxMpUserSummary>>() {
      }.getType());
}
 
源代码13 项目: framework   文件: TreeTypeAdapter.java
@Override public T read(JsonReader in) throws IOException {
  if (deserializer == null) {
    return delegate().read(in);
  }
  JsonElement value = Streams.parse(in);
  if (value.isJsonNull()) {
    return null;
  }
  return deserializer.deserialize(value, typeToken.getType(), gson.deserializationContext);
}
 
源代码14 项目: weixin-java-tools   文件: WxCpServiceImpl.java
public Integer departCreate(WxCpDepart depart) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
  String responseContent = execute(
      new SimplePostRequestExecutor(),
      url,
      depart.toJson());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id"));
}
 
源代码15 项目: weixin-java-tools   文件: WxMpServiceImpl.java
public long userGetGroup(String openid) throws WxErrorException {
  String url = "https://api.weixin.qq.com/cgi-bin/groups/getid";
  JsonObject o = new JsonObject();
  o.addProperty("openid", openid);
  String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
}
 
源代码16 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public String tagCreate(String tagName) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
  JsonObject o = new JsonObject();
  o.addProperty("tagname", tagName);
  String responseContent = post(url, o.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
}
 
源代码17 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public List<WxCpTag> tagGet() throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
  String responseContent = get(url, null);
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxCpGsonBuilder.INSTANCE.create()
      .fromJson(
          tmpJsonElement.getAsJsonObject().get("taglist"),
          new TypeToken<List<WxCpTag>>() {
          }.getType()
      );
}
 
源代码18 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId;
  String responseContent = get(url, null);
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return WxCpGsonBuilder.INSTANCE.create()
      .fromJson(
          tmpJsonElement.getAsJsonObject().get("userlist"),
          new TypeToken<List<WxCpUser>>() { }.getType()
      );
}
 
源代码19 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
      + "code=" + code
      + "&agendid=" + agentId;
  String responseText = get(url, null);
  JsonElement je = Streams.parse(new JsonReader(new StringReader(responseText)));
  JsonObject jo = je.getAsJsonObject();
  return new String[] {GsonHelper.getString(jo, "UserId"), GsonHelper.getString(jo, "DeviceId")};
}
 
源代码20 项目: weixin-java-tools   文件: WxCpServiceImpl.java
@Override
public int invite(String userId, String inviteTips) throws WxErrorException {
  String url = "https://qyapi.weixin.qq.com/cgi-bin/invite/send";
  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("userid", userId);
  if (StringUtils.isNotEmpty(inviteTips)) {
    jsonObject.addProperty("invite_tips", inviteTips);
  }
  String responseContent = post(url, jsonObject.toString());
  JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
  return tmpJsonElement.getAsJsonObject().get("type").getAsInt();
}
 
 同类方法