Search in sources :

Example 21 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method qrCodeCreateTmpTicket.

public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
    JsonObject json = new JsonObject();
    json.addProperty("action_name", "QR_SCENE");
    if (expire_seconds != null) {
        json.addProperty("expire_seconds", expire_seconds);
    }
    JsonObject actionInfo = new JsonObject();
    JsonObject scene = new JsonObject();
    scene.addProperty("scene_id", scene_id);
    actionInfo.add("scene", scene);
    json.add("action_info", actionInfo);
    String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString());
    return WxMpQrCodeTicket.fromJson(responseContent);
}
Also used : JsonObject(com.google.gson.JsonObject) SimplePostRequestExecutor(me.chanjar.weixin.common.util.http.SimplePostRequestExecutor)

Example 22 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method shortUrl.

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();
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) SimplePostRequestExecutor(me.chanjar.weixin.common.util.http.SimplePostRequestExecutor)

Example 23 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class YarnWeaveRunnerService method getApplicationId.

/**
   * Decodes application ID stored inside the node data.
   * @param nodeData The node data to decode from. If it is {@code null}, this method would return {@code null}.
   * @return The ApplicationId or {@code null} if failed to decode.
   */
private ApplicationId getApplicationId(NodeData nodeData) {
    byte[] data = nodeData == null ? null : nodeData.getData();
    if (data == null) {
        return null;
    }
    Gson gson = new Gson();
    JsonElement json = gson.fromJson(new String(data, Charsets.UTF_8), JsonElement.class);
    if (!json.isJsonObject()) {
        LOG.warn("Unable to decode live data node.");
        return null;
    }
    JsonObject jsonObj = json.getAsJsonObject();
    json = jsonObj.get("data");
    if (!json.isJsonObject()) {
        LOG.warn("Property data not found in live data node.");
        return null;
    }
    try {
        ApplicationMasterLiveNodeData amLiveNode = gson.fromJson(json, ApplicationMasterLiveNodeData.class);
        return YarnUtils.createApplicationId(amLiveNode.getAppIdClusterTime(), amLiveNode.getAppId());
    } catch (Exception e) {
        LOG.warn("Failed to decode application live node data.", e);
        return null;
    }
}
Also used : JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ApplicationMasterLiveNodeData(com.continuuity.weave.internal.appmaster.ApplicationMasterLiveNodeData) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 24 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class LogEntryDecoder method deserialize.

@Override
public LogEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
        return null;
    }
    JsonObject jsonObj = json.getAsJsonObject();
    final String name = JsonUtils.getAsString(jsonObj, "name");
    final String host = JsonUtils.getAsString(jsonObj, "host");
    final long timestamp = JsonUtils.getAsLong(jsonObj, "timestamp", 0);
    LogEntry.Level l;
    try {
        l = LogEntry.Level.valueOf(JsonUtils.getAsString(jsonObj, "level"));
    } catch (Exception e) {
        l = LogEntry.Level.FATAL;
    }
    final LogEntry.Level logLevel = l;
    final String className = JsonUtils.getAsString(jsonObj, "className");
    final String method = JsonUtils.getAsString(jsonObj, "method");
    final String file = JsonUtils.getAsString(jsonObj, "file");
    final String line = JsonUtils.getAsString(jsonObj, "line");
    final String thread = JsonUtils.getAsString(jsonObj, "thread");
    final String message = JsonUtils.getAsString(jsonObj, "message");
    final StackTraceElement[] stackTraces = context.deserialize(jsonObj.get("stackTraces").getAsJsonArray(), StackTraceElement[].class);
    return new LogEntry() {

        @Override
        public String getLoggerName() {
            return name;
        }

        @Override
        public String getHost() {
            return host;
        }

        @Override
        public long getTimestamp() {
            return timestamp;
        }

        @Override
        public Level getLogLevel() {
            return logLevel;
        }

        @Override
        public String getSourceClassName() {
            return className;
        }

        @Override
        public String getSourceMethodName() {
            return method;
        }

        @Override
        public String getFileName() {
            return file;
        }

        @Override
        public int getLineNumber() {
            if (line.equals("?")) {
                return -1;
            } else {
                return Integer.parseInt(line);
            }
        }

        @Override
        public String getThreadName() {
            return thread;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public StackTraceElement[] getStackTraces() {
            return stackTraces;
        }
    };
}
Also used : JsonObject(com.google.gson.JsonObject) LogEntry(com.continuuity.weave.api.logging.LogEntry) JsonParseException(com.google.gson.JsonParseException)

Example 25 with JsonObject

use of com.google.gson.JsonObject in project weave by continuuity.

the class ZKServiceDecorator method createLiveNode.

private OperationFuture<String> createLiveNode() {
    String liveNode = getLiveNodePath();
    LOG.info("Create live node {}{}", zkClient.getConnectString(), liveNode);
    JsonObject content = new JsonObject();
    content.add("data", liveNodeData.get());
    return ZKOperations.ignoreError(zkClient.create(liveNode, encodeJson(content), CreateMode.EPHEMERAL), KeeperException.NodeExistsException.class, liveNode);
}
Also used : JsonObject(com.google.gson.JsonObject) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

JsonObject (com.google.gson.JsonObject)1124 JsonParser (com.google.gson.JsonParser)263 JsonElement (com.google.gson.JsonElement)228 JsonArray (com.google.gson.JsonArray)226 JsonPrimitive (com.google.gson.JsonPrimitive)78 HashMap (java.util.HashMap)78 Test (org.junit.Test)77 Gson (com.google.gson.Gson)70 Map (java.util.Map)66 ArrayList (java.util.ArrayList)63 Test (org.testng.annotations.Test)61 IOException (java.io.IOException)53 InputStreamReader (java.io.InputStreamReader)27 JsonParseException (com.google.gson.JsonParseException)24 File (java.io.File)21 HttpResponse (org.apache.http.HttpResponse)21 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 JsonReader (com.google.gson.stream.JsonReader)17 InputStream (java.io.InputStream)17 URL (java.net.URL)15