001/*
002 * This file is part of the JDrupes JSON utilities project.
003 * Copyright (C) 2017, 2018  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.json;
020
021import java.util.List;
022import java.util.Optional;
023
024public interface JsonRpc extends JsonObject {
025        
026        /**
027         * The invoked method.
028         * 
029         * @return the method
030         */
031        String method();
032
033        /**
034         * The parameters.
035         * 
036         * @return the params
037         */
038        JsonArray params();
039
040        /**
041         * An optional request id.
042         * 
043         * @return the id
044         */
045        Optional<Object> id();
046
047        public JsonRpc setMethod(String method);
048
049        public JsonRpc setParams(JsonArray params);
050
051        public JsonRpc addParam(Object param);
052        
053        /**
054         * Creates an instance of {@link JsonRpc}.
055         * 
056         * @return the result
057         */
058        public static JsonRpc create() {
059                return new DefaultJsonRpc();
060        }
061        
062        public class DefaultJsonRpc extends DefaultJsonObject implements JsonRpc {
063
064                private static final long serialVersionUID = -5874908112198729940L;
065
066                public DefaultJsonRpc() {
067                        put("jsonrpc", "2.0");
068                }
069                
070                @Override
071                public String method() {
072                        return (String)get("method");
073                }
074
075                @Override
076                public DefaultJsonRpc setMethod(String method) {
077                        put("method", method);
078                        return this;
079                }
080                
081                @SuppressWarnings("unchecked")
082                @Override
083                public JsonArray params() {
084                        Object values = computeIfAbsent("params", key -> JsonArray.create());
085                        if (values instanceof JsonArray) {
086                                return (JsonArray) values;
087                        }
088                        if (values instanceof List) {
089                                return JsonArray.from((List<Object>)values);
090                        }
091                        throw new IllegalStateException(
092                                        "Field \"params\" has wrong type " + values.getClass().getName());
093                }
094
095                @Override
096                public DefaultJsonRpc setParams(JsonArray params) {
097                        put("params", params);
098                        return this;
099                }
100
101                @Override
102                public DefaultJsonRpc addParam(Object param) {
103                        JsonArray params = (JsonArray)computeIfAbsent(
104                                        "params", key -> JsonArray.create());
105                        params.append(param);
106                        return this;
107                }
108                
109                @Override
110                public Optional<Object> id() {
111                        return Optional.ofNullable(get("id"));
112                }
113                
114        }
115}