com.google.gson.internal.Streams Java Examples
The following examples show how to use
com.google.gson.internal.Streams.
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: Gson.java From framework with GNU Affero General Public License v3.0 | 6 votes |
/** * Writes the JSON for {@code jsonElement} to {@code writer}. * @throws JsonIOException if there was a problem writing to the writer */ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException { boolean oldLenient = writer.isLenient(); writer.setLenient(true); boolean oldHtmlSafe = writer.isHtmlSafe(); writer.setHtmlSafe(htmlSafe); boolean oldSerializeNulls = writer.getSerializeNulls(); writer.setSerializeNulls(serializeNulls); try { Streams.write(jsonElement, writer); } catch (IOException e) { throw new JsonIOException(e); } finally { writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); writer.setSerializeNulls(oldSerializeNulls); } }
Example #2
Source File: o.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public void write(JsonWriter jsonwriter, Object obj) { if (a == null) { a().write(jsonwriter, obj); return; } if (obj == null) { jsonwriter.nullValue(); return; } else { Streams.write(a.serialize(obj, d.getType(), c.c), jsonwriter); return; } }
Example #3
Source File: JsonElement.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public String toString() { String s; try { StringWriter stringwriter = new StringWriter(); JsonWriter jsonwriter = new JsonWriter(stringwriter); jsonwriter.setLenient(true); Streams.write(this, jsonwriter); s = stringwriter.toString(); } catch (IOException ioexception) { throw new AssertionError(ioexception); } return s; }
Example #4
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@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 #5
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 6 votes |
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 #6
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?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 #7
Source File: JsonParserTest.java From gson with Apache License 2.0 | 6 votes |
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 #8
Source File: Event.java From flink-stream-processing-refarch with Apache License 2.0 | 6 votes |
public static Event parseEvent(byte[] event) { //parse the event payload and remove the type attribute JsonReader jsonReader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(event))); JsonElement jsonElement = Streams.parse(jsonReader); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(TYPE_FIELD); if (labelJsonElement == null) { throw new IllegalArgumentException("Event does not define a type field: " + new String(event)); } //convert json to POJO, based on the type attribute switch (labelJsonElement.getAsString()) { case "watermark": return gson.fromJson(jsonElement, WatermarkEvent.class); case "trip": return gson.fromJson(jsonElement, TripEvent.class); default: throw new IllegalArgumentException("Found unsupported event type: " + labelJsonElement.getAsString()); } }
Example #9
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 6 votes |
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 #10
Source File: GsonInterfaceAdapter.java From incubator-gobblin with Apache License 2.0 | 6 votes |
@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 #11
Source File: Event.java From amazon-kinesis-analytics-taxi-consumer with Apache License 2.0 | 6 votes |
public static Event parseEvent(byte[] event) { //parse the event payload and remove the type attribute JsonReader jsonReader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(event))); JsonElement jsonElement = Streams.parse(jsonReader); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(TYPE_FIELD); if (labelJsonElement == null) { throw new IllegalArgumentException("Event does not define a type field: " + new String(event)); } //convert json to POJO, based on the type attribute switch (labelJsonElement.getAsString()) { case "watermark": return gson.fromJson(jsonElement, WatermarkEvent.class); case "trip": return gson.fromJson(jsonElement, TripEvent.class); default: throw new IllegalArgumentException("Found unsupported event type: " + labelJsonElement.getAsString()); } }
Example #12
Source File: Gson.java From letv with Apache License 2.0 | 6 votes |
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException { boolean oldLenient = writer.isLenient(); writer.setLenient(true); boolean oldHtmlSafe = writer.isHtmlSafe(); writer.setHtmlSafe(this.htmlSafe); boolean oldSerializeNulls = writer.getSerializeNulls(); writer.setSerializeNulls(this.serializeNulls); try { Streams.write(jsonElement, writer); writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); writer.setSerializeNulls(oldSerializeNulls); } catch (Throwable e) { throw new JsonIOException(e); } catch (Throwable th) { writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); writer.setSerializeNulls(oldSerializeNulls); } }
Example #13
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
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: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
public List<WxCpDepart> departGet() throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; String responseContent = get(url, null); /* * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } */ JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); return WxCpGsonBuilder.INSTANCE.create() .fromJson( tmpJsonElement.getAsJsonObject().get("department"), new TypeToken<List<WxCpDepart>>() { }.getType() ); }
Example #15
Source File: JsonElement.java From framework with GNU Affero General Public License v3.0 | 5 votes |
/** * Returns a String representation of this element. */ @Override public String toString() { try { StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(stringWriter); jsonWriter.setLenient(true); Streams.write(this, jsonWriter); return stringWriter.toString(); } catch (IOException e) { throw new AssertionError(e); } }
Example #16
Source File: TreeTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@Override public void write(JsonWriter out, T value) throws IOException { if (serializer == null) { delegate().write(out, value); return; } if (value == null) { out.nullValue(); return; } JsonElement tree = serializer.serialize(value, typeToken.getType(), gson.serializationContext); Streams.write(tree, out); }
Example #17
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #18
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #19
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #20
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #21
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #22
Source File: GsonInterfaceAdapter.java From incubator-gobblin with Apache License 2.0 | 5 votes |
private <S> void writeObject(S value, JsonWriter out) throws IOException { if (value != null) { JsonObject jsonObject = new JsonObject(); jsonObject.add(OBJECT_TYPE, new JsonPrimitive(value.getClass().getName())); TypeAdapter<S> delegate = (TypeAdapter<S>) this.gson.getDelegateAdapter(this.factory, TypeToken.get(value.getClass())); jsonObject.add(OBJECT_DATA, delegate.toJsonTree(value)); Streams.write(jsonObject, out); } else { out.nullValue(); } }
Example #23
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Override public String[] getCallbackIp() throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip"; String responseContent = get(url, null); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray(); String[] ips = new String[jsonArray.size()]; for(int i = 0; i < jsonArray.size(); i++) { ips[i] = jsonArray.get(i).getAsString(); } return ips; }
Example #24
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
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 #25
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
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 #26
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
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 #27
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); if (jsonObject.get("errcode").getAsInt() == 0) return jsonObject.get("msgid").getAsString(); throw new WxErrorException(WxError.fromJson(responseContent)); }
Example #28
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Override public String[] getCallbackIP() throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip"; String responseContent = get(url, null); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray(); String[] ipArray = new String[ipList.size()]; for (int i = 0; i < ipList.size(); i++) { ipArray[i] = ipList.get(i).getAsString(); } return ipArray; }
Example #29
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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 #30
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@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()); }