Java Code Examples for com.google.gson.internal.Streams#parse()

The following examples show how to use com.google.gson.internal.Streams#parse() . 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: JsonParserTest.java    From gson with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@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()
      );
}
 
Example 3
Source File: GsonInterfaceAdapter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 5
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 6
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 8
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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")};
}
 
Example 9
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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()
      );
}
 
Example 10
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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()
      );
}
 
Example 11
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 12
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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"));
}
 
Example 13
Source File: WxCpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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"));
}
 
Example 14
Source File: TreeTypeAdapter.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 16
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 17
Source File: TreeTypeAdapter.java    From gson with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 18
Source File: o.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 19
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 20
Source File: TreeTypeAdapter.java    From letv with Apache License 2.0 5 votes vote down vote up
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);
}