Example usage for org.json.simple JSONObject replace

List of usage examples for org.json.simple JSONObject replace

Introduction

In this page you can find the example usage for org.json.simple JSONObject replace.

Prototype

default V replace(K key, V value) 

Source Link

Document

Replaces the entry for the specified key only if it is currently mapped to some value.

Usage

From source file:app.WebSocketServer.java

public static void main(final String[] args) {
    Undertow server = Undertow.builder().addHttpListener(3030, "0.0.0.0")
            .setHandler(path().addPrefixPath("/ws", websocket(new WebSocketConnectionCallback() {
                @Override/*from   w ww .j  a va  2s . c o  m*/
                public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
                    channels.add(channel);
                    channel.getReceiveSetter().set(new AbstractReceiveListener() {
                        @Override
                        protected void onFullTextMessage(WebSocketChannel channel,
                                BufferedTextMessage message) {
                            JSONObject msg = (JSONObject) JSONValue.parse(message.getData());

                            switch (msg.get("type").toString()) {
                            case "broadcast":
                                final String outbound = msg.toJSONString();
                                channels.forEach(gc -> WebSockets.sendText(outbound, gc, null));
                                msg.replace("type", "broadcastResult");
                                WebSockets.sendText(msg.toJSONString(), channel, null);
                                break;
                            case "echo":
                                WebSockets.sendText(msg.toJSONString(), channel, null);
                                break;
                            default:
                                System.out.println("Unknown message type");
                            }
                        }
                    });

                    channel.addCloseTask(new ChannelListener<WebSocketChannel>() {
                        @Override
                        public void handleEvent(WebSocketChannel channel) {
                            channels.remove(channel);
                        }
                    });
                    channel.resumeReceives();
                }
            }))).build();
    server.start();
}

From source file:com.des.paperbase.ManageData.java

public void update(String Tablename, Map<String, Object> Filter, Map<String, Object> updateValue,
        String FilterType) throws IOException, ParseException {
    String data = g.readFileToString(PATH_FILE + "\\" + Tablename + ".json");
    JSONObject json = (JSONObject) new JSONParser().parse(data);
    List<String> keySet = g.getKeyFromMap(updateValue);
    if (!data.equalsIgnoreCase("{\"data\":[]}")) {
        JSONArray newValue = new JSONArray();
        JSONArray OldValue = (JSONArray) json.get("data");
        for (int s = 0; s < OldValue.size(); s++) {
            JSONObject record = (JSONObject) OldValue.get(s);
            if (MappingRecordAndKey(Filter, record, FilterType)) {
                for (int i = 0; i < keySet.size(); i++) {
                    System.out.println("replace : " + keySet.get(i) + "  to " + updateValue.get(keySet.get(i)));
                    record.replace(keySet.get(i), updateValue.get(keySet.get(i)));
                }/*from w  ww.ja  v a 2 s  .c  o  m*/

            }
            newValue.add(record);
        }
        json.put("data", newValue);
        System.out.println(json.toJSONString());
        g.writefile(json.toJSONString(), PATH_FILE + "\\" + Tablename + ".json");
    } else {
        g.writefile(data, PATH_FILE + "\\" + Tablename + ".json");
    }

}

From source file:app.WebSocketServerHandler.java

private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {

    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
        channels.remove(ctx.channel());//  w w w  .  j a va2 s. c  om
        handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        return;
    }
    if (frame instanceof PingWebSocketFrame) {
        ctx.write(new PongWebSocketFrame(frame.content().retain()));
        return;
    }
    if (frame instanceof TextWebSocketFrame) {
        JSONObject msg = (JSONObject) JSONValue.parse(((TextWebSocketFrame) frame).text());

        if (msg == null) {
            System.out.println("Unknown message type");
            return;
        }

        switch (msg.get("type").toString()) {
        case "broadcast":
            final TextWebSocketFrame outbound = new TextWebSocketFrame(msg.toJSONString());
            channels.forEach(gc -> gc.writeAndFlush(outbound.duplicate().retain()));
            msg.replace("type", "broadcastResult");
            ctx.writeAndFlush(new TextWebSocketFrame(msg.toJSONString()));
            break;
        case "echo":
            ctx.writeAndFlush(new TextWebSocketFrame(msg.toJSONString()));
            break;
        default:
            System.out.println("Unknown message type");
        }

        return;
    }
}

From source file:com.nubits.nubot.options.NuBotOptions.java

private String toStringNoKeys() {
    String toRet = "";

    GsonBuilder gson = new GsonBuilder().setPrettyPrinting();
    //gson.registerTypeAdapter(NuBotOptions.class, new NuBotOptionsSerializer());
    Gson parser = gson.create();//ww w  .j  ava 2  s.c o m

    String serializedOptionsStr = parser.toJson(this);
    org.json.simple.parser.JSONParser p = new org.json.simple.parser.JSONParser();
    try {
        JSONObject serializedOptionsJSON = (JSONObject) (p.parse(serializedOptionsStr));

        //Replace sensitive information
        String[] sensitiveKeys = { "apisecret", "apikey", "rpcpass", "apiSecret", "apiKey", "rpcPass" };
        String replaceString = "hidden";

        for (int i = 0; i < sensitiveKeys.length; i++) {
            if (serializedOptionsJSON.containsKey(sensitiveKeys[i])) {
                serializedOptionsJSON.replace(sensitiveKeys[i], replaceString);
            }
        }

        toRet = serializedOptionsJSON.toString();
    } catch (org.json.simple.parser.ParseException e) {
        LOG.error(e.toString());
    }

    return toRet;
}