1 /*
2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package build.tools.pandocfilter.json;
24 
25 import java.util.*;
26 import java.util.stream.Collectors;
27 
28 public class JSONObject implements JSONValue {
29     public static class Field {
30         private final String name;
31         private final JSONValue value;
32 
Field(String name, JSONValue value)33         private Field(String name, JSONValue value) {
34             this.name = name;
35             this.value = value;
36         }
37 
name()38         public String name() {
39             return name;
40         }
41 
value()42         public JSONValue value() {
43             return value;
44         }
45     }
46 
47     private final Map<String, JSONValue> value;
48 
JSONObject()49     public JSONObject() {
50         this.value = new HashMap<String, JSONValue>();
51     }
52 
JSONObject(Map<String, JSONValue> map)53     public JSONObject(Map<String, JSONValue> map) {
54         this.value = new HashMap<String, JSONValue>(map);
55     }
56 
57     @Override
isObject()58     public boolean isObject() {
59         return true;
60     }
61 
62     @Override
asObject()63     public JSONObject asObject() {
64         return this;
65     }
66 
put(String k, boolean v)67     public JSONObject put(String k, boolean v) {
68         value.put(k, JSON.of(v));
69         return this;
70     }
71 
put(String k, int v)72     public JSONObject put(String k, int v) {
73         value.put(k, JSON.of(v));
74         return this;
75     }
76 
put(String k, long v)77     public JSONObject put(String k, long v) {
78         value.put(k, JSON.of(v));
79         return this;
80     }
81 
put(String k, String v)82     public JSONObject put(String k, String v) {
83         value.put(k, JSON.of(v));
84         return this;
85     }
86 
put(String k, double v)87     public JSONObject put(String k, double v) {
88         value.put(k, JSON.of(v));
89         return this;
90     }
91 
put(String k, JSONArray v)92     public JSONObject put(String k, JSONArray v) {
93         value.put(k, v);
94         return this;
95     }
96 
put(String k, JSONObject v)97     public JSONObject put(String k, JSONObject v) {
98         value.put(k, v);
99         return this;
100     }
101 
put(String k, JSONValue v)102     public JSONObject put(String k, JSONValue v) {
103         value.put(k, v);
104         return this;
105     }
106 
putNull(String k)107     public JSONObject putNull(String k) {
108         value.put(k, JSON.of());
109         return this;
110     }
111 
remove(String k)112     public JSONValue remove(String k) {
113         return value.remove(k);
114     }
115 
get(String k)116     public JSONValue get(String k) {
117         return value.get(k);
118     }
119 
fields()120     public List<Field> fields() {
121         return value.entrySet()
122                     .stream()
123                     .map(e -> new Field(e.getKey(), e.getValue()))
124                     .collect(Collectors.toList());
125     }
126 
contains(String field)127     public boolean contains(String field) {
128         return value.containsKey(field);
129     }
130 
keys()131     public Set<String> keys() {
132         return value.keySet();
133     }
134 
135     @Override
toString()136     public String toString() {
137         var builder = new StringBuilder();
138         builder.append("{");
139         for (var key : value.keySet()) {
140             builder.append("\"");
141             builder.append(key);
142             builder.append("\":");
143             builder.append(value.get(key).toString());
144             builder.append(",");
145         }
146 
147         var end = builder.length() - 1;
148         if (builder.charAt(end) == ',') {
149             builder.deleteCharAt(end);
150         }
151 
152         builder.append("}");
153         return builder.toString();
154     }
155 }
156