Revision 2359
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/pom.xml | ||
---|---|---|
1 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
2 |
<modelVersion>4.0.0</modelVersion> |
|
3 |
<parent> |
|
4 |
<groupId>org.gvsig</groupId> |
|
5 |
<artifactId>org.gvsig.tools</artifactId> |
|
6 |
<version>3.0.253</version> |
|
7 |
</parent> |
|
8 |
<artifactId>org.gvsig.tools.util</artifactId> |
|
9 |
<name>org.gvsig.tools.util</name> |
|
10 |
<packaging>pom</packaging> |
|
11 |
<modules> |
|
12 |
<module>org.gvsig.tools.util.api</module> |
|
13 |
<module>org.gvsig.tools.util.impl</module> |
|
14 |
</modules> |
|
15 |
</project> |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/euclidean/DefaultEuclideanManager.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.euclidean; |
|
7 |
|
|
8 |
import java.awt.geom.Point2D; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author fdiaz |
|
13 |
*/ |
|
14 |
public class DefaultEuclideanManager implements EuclideanManager{ |
|
15 |
|
|
16 |
@Override |
|
17 |
public EuclideanLine2D createLine2D(double coefA, double coefB, double coefC) { |
|
18 |
return new EuclideanLine2DImpl(coefA, coefB, coefC); |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public EuclideanLine2D createLine2D(double m, double b) { |
|
23 |
return new EuclideanLine2DImpl(m, b); |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public EuclideanLine2D createLine2D(double x0, double y0, double x1, double y1) { |
|
28 |
return new EuclideanLine2DImpl(x0, y0, x1, y1); |
|
29 |
} |
|
30 |
|
|
31 |
@Override |
|
32 |
public EuclideanLine2D createLine2D(Point2D p1, Point2D p2) { |
|
33 |
return new EuclideanLine2DImpl(p2, p1); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public EuclideanLine2D createLine2D(double m, Point2D p) { |
|
38 |
if(Double.isInfinite(m)){ |
|
39 |
return new EuclideanLine2DImpl(1, 0, -p.getX()); |
|
40 |
} |
|
41 |
return new EuclideanLine2DImpl(m, getYIntercept(m, p)); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public double getYIntercept(double m, Point2D p) { |
|
46 |
return getYIntercept(m, p.getX(), p.getY()); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public double getYIntercept(double m, double x, double y) { |
|
51 |
return -m*x+y; |
|
52 |
} |
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/euclidean/EuclideanLine2DImpl.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.euclidean; |
|
7 |
|
|
8 |
import java.awt.geom.Point2D; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author fdiaz |
|
13 |
*/ |
|
14 |
public class EuclideanLine2DImpl implements EuclideanLine2D { |
|
15 |
|
|
16 |
double coefA; |
|
17 |
double coefB; |
|
18 |
double coefC; |
|
19 |
double m; |
|
20 |
double b; |
|
21 |
|
|
22 |
public EuclideanLine2DImpl(double coefA, double coefB, double coefC) { |
|
23 |
|
|
24 |
this.coefA = coefA; |
|
25 |
this.coefB = coefB; |
|
26 |
this.coefC = coefC; |
|
27 |
|
|
28 |
reduceCoefs(); |
|
29 |
|
|
30 |
this.m = -coefA/coefB; |
|
31 |
if(this.m == -0.0){ |
|
32 |
this.m = 0.0; |
|
33 |
} |
|
34 |
this.b = -coefC / coefB; |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* If m parameter m is infinite, preferably use the constructor EuclideanLine2DImpl(double coefA, double coefB, double coefC) |
|
40 |
* |
|
41 |
* @param m |
|
42 |
* @param b |
|
43 |
*/ |
|
44 |
|
|
45 |
public EuclideanLine2DImpl(double m, double b) { |
|
46 |
|
|
47 |
this.m = m; |
|
48 |
this.b = b; |
|
49 |
|
|
50 |
this.coefA = m; |
|
51 |
this.coefB = -1.0; |
|
52 |
this.coefC = b; |
|
53 |
|
|
54 |
reduceCoefs(); |
|
55 |
|
|
56 |
} |
|
57 |
|
|
58 |
public EuclideanLine2DImpl(double x0, double y0, double x1, double y1) { |
|
59 |
this(y1 - y0, -(x1 - x0), -(y1 - y0) * x0 + (x1 - x0) * y0); |
|
60 |
} |
|
61 |
|
|
62 |
public EuclideanLine2DImpl(Point2D p0, Point2D p1) { |
|
63 |
this(p0.getX(), p0.getY(), p1.getX(), p1.getY()); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
private void reduceCoefs() { |
|
68 |
if(coefA != 0 && Double.isFinite(coefA)){ |
|
69 |
coefC = coefC/coefA; |
|
70 |
coefB = coefB/coefA; |
|
71 |
coefA = 1.0; |
|
72 |
} else if(coefB != 0 && Double.isFinite(coefB)){ |
|
73 |
coefA = coefA/coefB; // for change the sign of coefA when coefA is infinite && coefB = -1; |
|
74 |
coefC = coefC/coefB; |
|
75 |
coefB = 1.0; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public double getA() { |
|
81 |
return coefA; |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public double getB() { |
|
86 |
return coefB; |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public double getC() { |
|
91 |
return coefC; |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public double getSlope() { |
|
96 |
return m; |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public double getYIntercept() { |
|
101 |
return b; |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public double getY(double x) { |
|
106 |
return (-coefA*x-coefC)/coefB; |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public double getX(double y) { |
|
111 |
return (-coefB*y-coefC)/coefA; |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public double getAngle() { |
|
116 |
return Math.atan(m); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public double getDegreesAngle() { |
|
121 |
return Math.toDegrees(getAngle()); |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public double getAngle(EuclideanLine2D line) { |
|
126 |
double m1 = line.getSlope(); |
|
127 |
return Math.atan(Math.abs((m1-m)/(1+m1*m))); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public double getAngle(EuclideanLine2D line, Point2D quadrant) { |
|
132 |
EuclideanLine2D[] bisectors = this.getBisectors(line); |
|
133 |
if(bisectors[0].getDistance(quadrant)<=bisectors[1].getDistance(quadrant)){ |
|
134 |
return 2*this.getAngle(bisectors[0]); |
|
135 |
} else { |
|
136 |
return 2*this.getAngle(bisectors[1]); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
@Override |
|
141 |
public double getDegreesAngle(EuclideanLine2D line) { |
|
142 |
return Math.toDegrees(getAngle(line)); |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public double getDegreesAngle(EuclideanLine2D line, Point2D quadrant) { |
|
147 |
return Math.toDegrees(getAngle(line, quadrant)); |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public double getDistance(double pointX, double pointY) { |
|
152 |
// d(P,r)=|A*px+B*py+C|/SQRT(A?+B?) |
|
153 |
|
|
154 |
double den = Math.abs(Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2))); |
|
155 |
return Math.abs((coefA * pointX + coefB * pointY + coefC)) / den; |
|
156 |
} |
|
157 |
|
|
158 |
@Override |
|
159 |
public double getDistance(Point2D point) { |
|
160 |
return getDistance(point.getX(), point.getY()); |
|
161 |
} |
|
162 |
|
|
163 |
@Override |
|
164 |
public double getDistance(EuclideanLine2D line) { |
|
165 |
|
|
166 |
if (isParallel(line)) { //Parallel lines |
|
167 |
Double num = Math.abs(line.getC() - coefC); |
|
168 |
if (Double.isInfinite(m)) { //Vertical lines |
|
169 |
return num; |
|
170 |
} |
|
171 |
Double den = Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2)); |
|
172 |
return num / den; |
|
173 |
} |
|
174 |
return 0d; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public boolean isParallel(EuclideanLine2D line) { |
|
179 |
|
|
180 |
double m1 = line.getSlope(); |
|
181 |
return (m == m1 || (Double.isInfinite(m) && Double.isInfinite(m1))); |
|
182 |
} |
|
183 |
|
|
184 |
@Override |
|
185 |
public boolean isPerpendicularl(EuclideanLine2D line) { |
|
186 |
double m1 = line.getSlope(); |
|
187 |
return (m * m1 == -1 || (Double.isInfinite(m) && m1 == 0.0) || (m==0 && Double.isInfinite(m1))); |
|
188 |
|
|
189 |
// return (getA()*line.getA()+getB()*line.getB() == 0); |
|
190 |
} |
|
191 |
|
|
192 |
@Override |
|
193 |
public Point2D getIntersection(EuclideanLine2D line) { |
|
194 |
//Using Cramer's rule |
|
195 |
double a1 = coefA; |
|
196 |
double b1 = coefB; |
|
197 |
double c1 = coefC; |
|
198 |
|
|
199 |
Double a2 = line.getA(); |
|
200 |
Double b2 = line.getB(); |
|
201 |
Double c2 = line.getC(); |
|
202 |
|
|
203 |
Double det = a1 * b2 - a2 * b1; |
|
204 |
Double detX = -c1 * b2 + c2 * b1; |
|
205 |
Double detY = -a1 * c2 + a2 * c1; |
|
206 |
|
|
207 |
Double x = detX / det; |
|
208 |
Double y = detY / det; |
|
209 |
|
|
210 |
return new Point2D.Double(x, y); |
|
211 |
|
|
212 |
//Using Apache commons math library, need import the library |
|
213 |
// RealMatrix coefficients = |
|
214 |
// new Array2DRowRealMatrix(new double[][] { { coefA, coefB }, { line.getA(), line.getB() } }, false); |
|
215 |
// DecompositionSolver solver = new LUDecomposition(coefficients).getSolver(); |
|
216 |
// RealVector constants = new ArrayRealVector(new double[] { -coefC, -line.getC() }, false); |
|
217 |
// RealVector solution = solver.solve(constants); |
|
218 |
// return new Point2D.Double(solution.getEntry(0), solution.getEntry(1)); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
222 |
@Override |
|
223 |
public EuclideanLine2D getPerpendicular(double pointX, double pointY) { |
|
224 |
if(Math.abs(m)==0.0){ |
|
225 |
return new EuclideanLine2DImpl(1, 0, -pointX); |
|
226 |
} |
|
227 |
if(Double.isInfinite(m)){ |
|
228 |
return new EuclideanLine2DImpl(0, 1, -pointY); |
|
229 |
} |
|
230 |
// perpendicular slope (m) |
|
231 |
Double m1 = -1 / m; |
|
232 |
|
|
233 |
// perpendicular y-intercept (b) |
|
234 |
Double b1 = pointY - (m1 * pointX); |
|
235 |
|
|
236 |
return new EuclideanLine2DImpl(m1, b1); |
|
237 |
} |
|
238 |
|
|
239 |
@Override |
|
240 |
public EuclideanLine2D getPerpendicular(Point2D point) { |
|
241 |
return getPerpendicular(point.getX(), point.getY()); |
|
242 |
} |
|
243 |
|
|
244 |
@Override |
|
245 |
public EuclideanLine2D getParallel(double pointX, double pointY) { |
|
246 |
if(Math.abs(m)==0.0){ |
|
247 |
return new EuclideanLine2DImpl(0, 1, -pointY); |
|
248 |
} |
|
249 |
if(Double.isInfinite(m)){ |
|
250 |
return new EuclideanLine2DImpl(1, 0, -pointX); |
|
251 |
} |
|
252 |
// parallel slope (m) |
|
253 |
Double m1 = m; |
|
254 |
|
|
255 |
// parallel y-intercept (b) |
|
256 |
Double b1 = pointY - (m1 * pointX); |
|
257 |
|
|
258 |
return new EuclideanLine2DImpl(m1, b1); |
|
259 |
} |
|
260 |
|
|
261 |
@Override |
|
262 |
public EuclideanLine2D getParallel(Point2D point) { |
|
263 |
return getParallel(point.getX(), point.getY()); |
|
264 |
} |
|
265 |
|
|
266 |
|
|
267 |
@Override |
|
268 |
public Point2D getNearestPoint(double pointX, double pointY) { |
|
269 |
double x; |
|
270 |
double y; |
|
271 |
|
|
272 |
if (Double.isInfinite(m)) { |
|
273 |
y = pointY; |
|
274 |
x = (-coefB * y - coefC) / coefA; |
|
275 |
} else if (Math.abs(m) == 0) { |
|
276 |
x = pointX; |
|
277 |
y = b; |
|
278 |
} else { |
|
279 |
|
|
280 |
EuclideanLine2D perp = getPerpendicular(pointX, pointY); |
|
281 |
Double m1 = perp.getSlope(); |
|
282 |
Double b1 = perp.getYIntercept(); |
|
283 |
|
|
284 |
x = (b1 - b) / (m - m1); |
|
285 |
y = m * x + b; |
|
286 |
} |
|
287 |
|
|
288 |
return new Point2D.Double(x, y); |
|
289 |
} |
|
290 |
|
|
291 |
@Override |
|
292 |
public Point2D getNearestPoint(Point2D point) { |
|
293 |
return getNearestPoint(point.getX(), point.getY()); |
|
294 |
} |
|
295 |
|
|
296 |
@Override |
|
297 |
public EuclideanLine2D[] getBisectors(EuclideanLine2D line) { |
|
298 |
/* |
|
299 |
r) A1x + B1y + C1 = 0 |
|
300 |
s) A2x + B2y + C2 = 0 |
|
301 |
|
|
302 |
|A1x+B1y+C1|/SQRT(A1?+B1?)= |A2x+B2y+C2|/SQRT(A2?+B2?) |
|
303 |
*/ |
|
304 |
|
|
305 |
Double a1 = coefA; |
|
306 |
Double b1 = coefB; |
|
307 |
Double c1 = coefC; |
|
308 |
|
|
309 |
Double a2 = line.getA(); |
|
310 |
Double b2 = line.getB(); |
|
311 |
Double c2 = line.getC(); |
|
312 |
|
|
313 |
Double den1 = Math.sqrt(Math.pow(a1,2) + Math.pow(b1,2)); |
|
314 |
Double den2 = Math.sqrt(Math.pow(a2,2) + Math.pow(b2,2)); |
|
315 |
|
|
316 |
EuclideanLine2D[] result = new EuclideanLine2DImpl[2]; |
|
317 |
result[0] = new EuclideanLine2DImpl( |
|
318 |
den2 * a1 - den1 * a2, |
|
319 |
den2 * b1 - den1 * b2, |
|
320 |
den2 * c1 - den1 * c2 |
|
321 |
); |
|
322 |
result[1] = new EuclideanLine2DImpl( |
|
323 |
den2 * a1 + den1 * a2, |
|
324 |
den2 * b1 + den1 * b2, |
|
325 |
den2 * c1 + den1 * c2 |
|
326 |
); |
|
327 |
|
|
328 |
return result; |
|
329 |
|
|
330 |
} |
|
331 |
|
|
332 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonObjectImpl.java | ||
---|---|---|
1 |
/* |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2020 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
18 |
* |
|
19 |
* For any additional information, do not hesitate to contact us |
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
21 |
*/ |
|
22 |
|
|
23 |
package org.gvsig.json; |
|
24 |
|
|
25 |
import java.io.StringWriter; |
|
26 |
import java.util.Collection; |
|
27 |
import java.util.Collections; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.Set; |
|
30 |
import javax.json.JsonArray; |
|
31 |
import javax.json.JsonNumber; |
|
32 |
import javax.json.JsonObject; |
|
33 |
import javax.json.JsonString; |
|
34 |
import javax.json.JsonValue; |
|
35 |
import javax.json.JsonWriter; |
|
36 |
import javax.json.JsonWriterFactory; |
|
37 |
import javax.json.stream.JsonGenerator; |
|
38 |
|
|
39 |
/** |
|
40 |
* |
|
41 |
* @author gvSIG Team |
|
42 |
*/ |
|
43 |
public class JsonObjectImpl implements javax.json.JsonObject { |
|
44 |
|
|
45 |
private final javax.json.JsonObject jsonObject; |
|
46 |
|
|
47 |
public JsonObjectImpl(javax.json.JsonObject jsonObject) { |
|
48 |
this.jsonObject = jsonObject; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public JsonArray getJsonArray(String name) { |
|
53 |
JsonValue value = this.jsonObject.get(name); |
|
54 |
return (JsonArray) JsonManagerImpl.wrap(value); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public JsonObject getJsonObject(String name) { |
|
59 |
JsonValue value = this.jsonObject.get(name); |
|
60 |
return (JsonObject) JsonManagerImpl.wrap(value); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public JsonNumber getJsonNumber(String name) { |
|
65 |
JsonValue value = this.jsonObject.get(name); |
|
66 |
if( value == null || value == JsonValue.NULL ) { |
|
67 |
return null; |
|
68 |
} |
|
69 |
return (JsonNumber)value; |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public JsonString getJsonString(String name) { |
|
74 |
JsonValue value = this.jsonObject.get(name); |
|
75 |
if( value == null || value == JsonValue.NULL ) { |
|
76 |
return null; |
|
77 |
} |
|
78 |
return (JsonString)value; |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public String getString(String name) { |
|
83 |
JsonValue value = this.jsonObject.get(name); |
|
84 |
if( value == null || value == JsonValue.NULL ) { |
|
85 |
return null; |
|
86 |
} |
|
87 |
return ((JsonString)value).getString(); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public String getString(String name, String defaultValue) { |
|
92 |
JsonValue value = this.jsonObject.get(name); |
|
93 |
if( value == null || value == JsonValue.NULL ) { |
|
94 |
return defaultValue; |
|
95 |
} |
|
96 |
return ((JsonString)value).getString(); |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public int getInt(String name) { |
|
101 |
return this.jsonObject.getInt(name); |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public int getInt(String name, int defaultValue) { |
|
106 |
return this.jsonObject.getInt(name, defaultValue); |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public boolean getBoolean(String name) { |
|
111 |
return this.jsonObject.getBoolean(name); |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public boolean getBoolean(String name, boolean defaultValue) { |
|
116 |
return this.jsonObject.getBoolean(name, defaultValue); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public boolean isNull(String name) { |
|
121 |
return this.jsonObject.isNull(name); |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public ValueType getValueType() { |
|
126 |
return this.jsonObject.getValueType(); |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public int size() { |
|
131 |
return this.jsonObject.size(); |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public boolean isEmpty() { |
|
136 |
return this.jsonObject.isEmpty(); |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public boolean containsKey(Object key) { |
|
141 |
return this.jsonObject.containsKey(key); |
|
142 |
} |
|
143 |
|
|
144 |
@Override |
|
145 |
public boolean containsValue(Object value) { |
|
146 |
return this.jsonObject.containsValue(value); |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public JsonValue get(Object key) { |
|
151 |
JsonValue value = this.jsonObject.get(key); |
|
152 |
return JsonManagerImpl.wrap(value); |
|
153 |
} |
|
154 |
|
|
155 |
@Override |
|
156 |
public JsonValue put(String key, JsonValue value) { |
|
157 |
return this.jsonObject.put(key,value); |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public JsonValue remove(Object key) { |
|
162 |
return this.jsonObject.remove(key); |
|
163 |
} |
|
164 |
|
|
165 |
@Override |
|
166 |
public void putAll(Map<? extends String, ? extends JsonValue> m) { |
|
167 |
this.jsonObject.putAll(m); |
|
168 |
} |
|
169 |
|
|
170 |
@Override |
|
171 |
public void clear() { |
|
172 |
this.jsonObject.clear(); |
|
173 |
} |
|
174 |
|
|
175 |
@Override |
|
176 |
public Set<String> keySet() { |
|
177 |
return this.jsonObject.keySet(); |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public Collection<JsonValue> values() { |
|
182 |
return this.jsonObject.values(); |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public Set<Entry<String, JsonValue>> entrySet() { |
|
187 |
return this.jsonObject.entrySet(); |
|
188 |
} |
|
189 |
|
|
190 |
@Override |
|
191 |
public String toString() { |
|
192 |
StringWriter sw = new StringWriter(); |
|
193 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
|
194 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
|
195 |
); |
|
196 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { |
|
197 |
jsonWriter.writeObject(this.jsonObject); |
|
198 |
} |
|
199 |
String s = sw.toString(); |
|
200 |
return s; |
|
201 |
} |
|
202 |
|
|
203 |
|
|
204 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonManagerImpl.java | ||
---|---|---|
1 |
package org.gvsig.json; |
|
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.StringWriter; |
|
5 |
import java.math.BigDecimal; |
|
6 |
import java.util.Arrays; |
|
7 |
import java.util.Collections; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.Iterator; |
|
10 |
import java.util.Map; |
|
11 |
import javax.json.JsonArray; |
|
12 |
import javax.json.JsonNumber; |
|
13 |
import javax.json.JsonObject; |
|
14 |
import javax.json.JsonReader; |
|
15 |
import javax.json.JsonString; |
|
16 |
import javax.json.JsonStructure; |
|
17 |
import javax.json.JsonValue; |
|
18 |
import javax.json.JsonWriter; |
|
19 |
import javax.json.JsonWriterFactory; |
|
20 |
import javax.json.stream.JsonGenerator; |
|
21 |
import static org.gvsig.json.JsonManager.ATTRIBUTE_NAME_CLASS; |
|
22 |
import org.gvsig.json.serializers.DataTypeSerializer; |
|
23 |
import org.gvsig.json.serializers.DefaultObjectSerializer; |
|
24 |
import org.gvsig.json.serializers.DynObjectSerializer; |
|
25 |
import org.gvsig.json.serializers.DynObjectValueItemSerializer; |
|
26 |
import org.gvsig.json.serializers.FileSerializer; |
|
27 |
import org.gvsig.json.serializers.LocaleSerializer; |
|
28 |
import org.gvsig.json.serializers.TagsSerializer; |
|
29 |
import org.gvsig.json.serializers.UriSerializer; |
|
30 |
import org.gvsig.json.serializers.UrlSerializer; |
|
31 |
import org.gvsig.tools.dynobject.DynClass; |
|
32 |
import org.gvsig.tools.dynobject.DynField; |
|
33 |
import org.gvsig.tools.dynobject.DynObject; |
|
34 |
import org.gvsig.tools.util.GetItemWithSizeAndIterator; |
|
35 |
import org.gvsig.tools.util.IsApplicable; |
|
36 |
|
|
37 |
/** |
|
38 |
* |
|
39 |
* @author gvSIG Team |
|
40 |
*/ |
|
41 |
@SuppressWarnings("UseSpecificCatch") |
|
42 |
public class JsonManagerImpl implements JsonManager { |
|
43 |
|
|
44 |
private final Map<String,JsonSerializer> serializers1; |
|
45 |
private final Map<String,IsApplicable> serializers2; |
|
46 |
|
|
47 |
private final DynObjectSerializer dynObjectSerializer; |
|
48 |
|
|
49 |
public JsonManagerImpl() { |
|
50 |
this.dynObjectSerializer = new DynObjectSerializer(); |
|
51 |
this.serializers1 = new HashMap<>(); |
|
52 |
this.serializers2 = new HashMap<>(); |
|
53 |
this.registerSerializer(new FileSerializer()); |
|
54 |
this.registerSerializer(new UrlSerializer()); |
|
55 |
this.registerSerializer(new UriSerializer()); |
|
56 |
this.registerSerializer(new LocaleSerializer()); |
|
57 |
this.registerSerializer(new TagsSerializer()); |
|
58 |
this.registerSerializer(new DynObjectValueItemSerializer()); |
|
59 |
this.registerSerializer(new DataTypeSerializer()); |
|
60 |
// this.registerSerializer(this.dynObjectSerializer); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public void registerSerializer(Class<?extends SupportFromJson> theClass) { |
|
65 |
if (theClass == null) { |
|
66 |
return; |
|
67 |
} |
|
68 |
if (!SupportFromJson.class.isAssignableFrom(theClass)) { |
|
69 |
throw new IllegalArgumentException("the class '" + theClass.getName() + "'must implement SupportFromJson"); |
|
70 |
} |
|
71 |
this.serializers1.put(theClass.getName(), new DefaultObjectSerializer(theClass)); |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public void registerSerializer(JsonSerializer serializer) { |
|
76 |
this.serializers1.put(serializer.getObjectClass().getName(), serializer); |
|
77 |
if( serializer instanceof IsApplicable ) { |
|
78 |
this.serializers2.put(serializer.getObjectClass().getName(), (IsApplicable) serializer); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public JsonSerializer getSerializer(Object value) { |
|
84 |
JsonSerializer serializer1 = this.serializers1.get(value.getClass().getName()); |
|
85 |
if( serializer1!=null ) { |
|
86 |
return serializer1; |
|
87 |
} |
|
88 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
|
89 |
if( serializer2.isApplicable(value) ) { |
|
90 |
return (JsonSerializer) serializer2; |
|
91 |
} |
|
92 |
} |
|
93 |
if( value instanceof DynObject ) { |
|
94 |
return this.dynObjectSerializer; |
|
95 |
} |
|
96 |
return null; |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public JsonSerializer getSerializer(JsonObject json) { |
|
101 |
String className = json.getString(ATTRIBUTE_NAME_CLASS, null); |
|
102 |
JsonSerializer serializer1 = this.serializers1.get(className); |
|
103 |
if( serializer1!=null ) { |
|
104 |
return serializer1; |
|
105 |
} |
|
106 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
|
107 |
if( serializer2.isApplicable(json) ) { |
|
108 |
return (JsonSerializer) serializer2; |
|
109 |
} |
|
110 |
} |
|
111 |
if( this.dynObjectSerializer.isApplicable(json) ) { |
|
112 |
return this.dynObjectSerializer; |
|
113 |
} |
|
114 |
return null; |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public JsonObjectBuilder createObjectBuilder() { |
|
119 |
return new JsonObjectBuilderImpl(); |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public JsonArrayBuilder createArrayBuilder() { |
|
124 |
return new JsonArrayBuilderImpl(); |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public JsonObject createObject(String json) { |
|
129 |
try { |
|
130 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
|
131 |
JsonReader reader = javax.json.Json.createReader(data); |
|
132 |
JsonObject x = reader.readObject(); |
|
133 |
return new org.gvsig.json.JsonObjectImpl(x); |
|
134 |
} catch (Exception ex) { |
|
135 |
return null; |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public JsonArray createArray(String json) { |
|
141 |
try { |
|
142 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
|
143 |
JsonReader reader = javax.json.Json.createReader(data); |
|
144 |
JsonArray x = reader.readArray(); |
|
145 |
return new org.gvsig.json.JsonArrayImpl(x); |
|
146 |
} catch (Exception ex) { |
|
147 |
return null; |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
@Override |
|
152 |
public String toString(JsonObject obj) { |
|
153 |
return this.toString((JsonStructure) obj); |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public String toString(JsonArray array) { |
|
158 |
return this.toString((JsonStructure) array); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public String toString(JsonStructure obj) { |
|
163 |
StringWriter sw = new StringWriter(); |
|
164 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
|
165 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
|
166 |
); |
|
167 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { |
|
168 |
jsonWriter.write(obj); |
|
169 |
} |
|
170 |
String s = sw.toString(); |
|
171 |
return s; |
|
172 |
} |
|
173 |
|
|
174 |
@Override |
|
175 |
public Object toObject(String name, JsonObject json) { |
|
176 |
JsonValue value = json.get(name); |
|
177 |
return toObject(value); |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public Object toObject(int index, JsonArray json) { |
|
182 |
JsonValue value = json.get(index); |
|
183 |
return toObject(value); |
|
184 |
} |
|
185 |
|
|
186 |
@Override |
|
187 |
public Object toObject(JsonObject json) { |
|
188 |
return this.toObject(json, null); |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
public Object toObject(JsonObject json, Object defaultValue) { |
|
193 |
if (json == null) { |
|
194 |
return defaultValue; |
|
195 |
} |
|
196 |
JsonSerializer serializer = this.getSerializer(json); |
|
197 |
if (serializer == null) { |
|
198 |
return defaultValue; |
|
199 |
} |
|
200 |
Object obj = serializer.toObject(json); |
|
201 |
if (obj == null) { |
|
202 |
return defaultValue; |
|
203 |
} |
|
204 |
return obj; |
|
205 |
} |
|
206 |
|
|
207 |
@Override |
|
208 |
public Iterable<Object> toIterable(final JsonArray jsonArray) { |
|
209 |
@SuppressWarnings("Convert2Lambda") |
|
210 |
Iterable<Object> iterable = new Iterable<Object>() { |
|
211 |
@Override |
|
212 |
public Iterator<Object> iterator() { |
|
213 |
Iterator<Object> it = new Iterator<Object>() { |
|
214 |
int current= 0; |
|
215 |
@Override |
|
216 |
public boolean hasNext() { |
|
217 |
return current<jsonArray.size(); |
|
218 |
} |
|
219 |
|
|
220 |
@Override |
|
221 |
public Object next() { |
|
222 |
return toObject(jsonArray.get(current++)); |
|
223 |
} |
|
224 |
}; |
|
225 |
return it; |
|
226 |
} |
|
227 |
}; |
|
228 |
return iterable; |
|
229 |
} |
|
230 |
|
|
231 |
@Override |
|
232 |
public GetItemWithSizeAndIterator<Object> toItems(final JsonArray jsonArray) { |
|
233 |
GetItemWithSizeAndIterator<Object> items = new GetItemWithSizeAndIterator<Object>() { |
|
234 |
|
|
235 |
@Override |
|
236 |
public int size() { |
|
237 |
return jsonArray.size(); |
|
238 |
} |
|
239 |
|
|
240 |
@Override |
|
241 |
public Object get(int position) { |
|
242 |
return toObject(jsonArray.get(position)); |
|
243 |
} |
|
244 |
|
|
245 |
@Override |
|
246 |
public Iterator<Object> iterator() { |
|
247 |
Iterator<Object> it = new Iterator<Object>() { |
|
248 |
int current= 0; |
|
249 |
@Override |
|
250 |
public boolean hasNext() { |
|
251 |
return current<jsonArray.size(); |
|
252 |
} |
|
253 |
|
|
254 |
@Override |
|
255 |
public Object next() { |
|
256 |
return toObject(jsonArray.get(current++)); |
|
257 |
} |
|
258 |
}; |
|
259 |
return it; |
|
260 |
}; |
|
261 |
}; |
|
262 |
return items; |
|
263 |
} |
|
264 |
|
|
265 |
@Override |
|
266 |
public Object[] toArray(JsonArray jsonArray, Object[] a) { |
|
267 |
if( jsonArray==null ) { |
|
268 |
return null; |
|
269 |
} |
|
270 |
int sz = jsonArray.size(); |
|
271 |
if( a==null ) { |
|
272 |
a = new Object[sz]; |
|
273 |
} else if (a.length != sz) { |
|
274 |
a = Arrays.copyOf(a, sz); |
|
275 |
} |
|
276 |
for (int i = 0; i < sz; i++) { |
|
277 |
JsonValue value = jsonArray.get(i); |
|
278 |
a[i] = this.toObject(value); |
|
279 |
} |
|
280 |
return a; |
|
281 |
} |
|
282 |
|
|
283 |
@Override |
|
284 |
public Object toObject(JsonValue value) { |
|
285 |
if( value == null ) { |
|
286 |
return null; |
|
287 |
} |
|
288 |
switch (value.getValueType()) { |
|
289 |
case ARRAY: |
|
290 |
return this.toArray((JsonArray) value, new Object[0]); |
|
291 |
case FALSE: |
|
292 |
return false; |
|
293 |
case NUMBER: |
|
294 |
JsonNumber n = (JsonNumber) value; |
|
295 |
if (n.isIntegral()) { |
|
296 |
int nint = n.intValue(); |
|
297 |
long nlong = n.longValue(); |
|
298 |
if (nint == nlong) { |
|
299 |
return nint; |
|
300 |
} |
|
301 |
if (n.bigDecimalValue().compareTo(new BigDecimal(nlong)) == 0) { |
|
302 |
return nlong; |
|
303 |
} |
|
304 |
return n.bigIntegerValue(); |
|
305 |
} else { |
|
306 |
double ndouble = n.doubleValue(); |
|
307 |
if (n.bigDecimalValue().compareTo(new BigDecimal(ndouble)) == 0) { |
|
308 |
return ndouble; |
|
309 |
} |
|
310 |
return n.bigDecimalValue(); |
|
311 |
} |
|
312 |
|
|
313 |
case OBJECT: |
|
314 |
return this.toObject((JsonObject) value, value); |
|
315 |
|
|
316 |
case STRING: |
|
317 |
return ((JsonString) value).getString(); |
|
318 |
case TRUE: |
|
319 |
return false; |
|
320 |
case NULL: |
|
321 |
default: |
|
322 |
return null; |
|
323 |
} |
|
324 |
} |
|
325 |
|
|
326 |
@Override |
|
327 |
public DynObject addAll(DynObject target, JsonObject json) { |
|
328 |
if( json == null || target==null ) { |
|
329 |
return target; |
|
330 |
} |
|
331 |
DynClass dynClass = target.getDynClass(); |
|
332 |
String name = null; |
|
333 |
try { |
|
334 |
for (DynField dynField : dynClass.getDynFields()) { |
|
335 |
name = dynField.getName(); |
|
336 |
if( json.containsKey(name) ) { |
|
337 |
target.setDynValue(name, this.toObject(json.get(name))); |
|
338 |
} |
|
339 |
} |
|
340 |
} catch (Exception ex) { |
|
341 |
throw new RuntimeException("Can't copy values from json to DynObject (class "+dynClass.getFullName()+", field "+name+").", ex); |
|
342 |
} |
|
343 |
return target; |
|
344 |
} |
|
345 |
|
|
346 |
@Override |
|
347 |
public Map toHashMap(JsonObject json) { |
|
348 |
if( json == null ) { |
|
349 |
return null; |
|
350 |
} |
|
351 |
Map map = new HashMap(); |
|
352 |
for (String name : json.keySet()) { |
|
353 |
map.put(name, this.toObject(json.get(name))); |
|
354 |
} |
|
355 |
return map; |
|
356 |
} |
|
357 |
|
|
358 |
public static JsonValue wrap(JsonValue value) { |
|
359 |
if( value == null ) { |
|
360 |
return null; |
|
361 |
} else if( value == JsonValue.NULL ) { |
|
362 |
return null; |
|
363 |
} else if( value instanceof JsonObject && |
|
364 |
!(value instanceof org.gvsig.json.JsonObjectImpl)) { |
|
365 |
value = new org.gvsig.json.JsonObjectImpl((JsonObject) value); |
|
366 |
} else if( value instanceof JsonArray && |
|
367 |
!(value instanceof org.gvsig.json.JsonArrayImpl)) { |
|
368 |
value = new org.gvsig.json.JsonArrayImpl((JsonArray) value); |
|
369 |
} |
|
370 |
return value; |
|
371 |
} |
|
372 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonObjectBuilderImpl.java | ||
---|---|---|
1 |
package org.gvsig.json; |
|
2 |
|
|
3 |
import java.io.StringWriter; |
|
4 |
import java.math.BigDecimal; |
|
5 |
import java.math.BigInteger; |
|
6 |
import java.util.Collections; |
|
7 |
import java.util.Iterator; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import java.util.Set; |
|
11 |
import javax.json.JsonObject; |
|
12 |
import javax.json.JsonWriter; |
|
13 |
import javax.json.JsonWriterFactory; |
|
14 |
import javax.json.stream.JsonGenerator; |
|
15 |
import static org.gvsig.json.JsonManager.ATTRIBUTE_NAME_CLASS; |
|
16 |
import org.gvsig.json.JsonManager.JsonSerializer; |
|
17 |
import org.gvsig.tools.dynobject.DynClass; |
|
18 |
import org.gvsig.tools.dynobject.DynField; |
|
19 |
import org.gvsig.tools.dynobject.DynObject; |
|
20 |
import org.gvsig.tools.util.GetItemByKeyWithGetKeys; |
|
21 |
import org.gvsig.tools.util.GetItemWithSize; |
|
22 |
|
|
23 |
/** |
|
24 |
* |
|
25 |
* @author gvSIG Team |
|
26 |
*/ |
|
27 |
public class JsonObjectBuilderImpl implements JsonObjectBuilder { |
|
28 |
|
|
29 |
private final javax.json.JsonObjectBuilder builder; |
|
30 |
private final JsonManager manager; |
|
31 |
|
|
32 |
public JsonObjectBuilderImpl() { |
|
33 |
this(Json.getManager()); |
|
34 |
} |
|
35 |
|
|
36 |
public JsonObjectBuilderImpl(JsonManager manager) { |
|
37 |
this.manager = manager; |
|
38 |
this.builder = javax.json.Json.createObjectBuilder(); |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public JsonObjectBuilder add(String name, SupportToJson value) { |
|
43 |
if (value == null) { |
|
44 |
this.builder.addNull(name); |
|
45 |
return this; |
|
46 |
} |
|
47 |
this.builder.add(name, value.toJsonBuilder()); |
|
48 |
return this; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public JsonObjectBuilder add(String name, Object value) { |
|
53 |
if (value == null) { |
|
54 |
this.builder.addNull(name); |
|
55 |
} else if (value instanceof SupportToJson) { |
|
56 |
this.add(name, ((SupportToJson)value).toJsonBuilder()); |
|
57 |
} else if (value instanceof String) { |
|
58 |
this.builder.add(name, (String) value); |
|
59 |
} else if (value instanceof Integer) { |
|
60 |
this.builder.add(name, (int) value); |
|
61 |
} else if (value instanceof Long) { |
|
62 |
this.builder.add(name, (long) value); |
|
63 |
} else if (value instanceof BigInteger) { |
|
64 |
this.builder.add(name, (BigInteger) value); |
|
65 |
} else if (value instanceof BigDecimal) { |
|
66 |
this.builder.add(name, (BigDecimal) value); |
|
67 |
} else if (value instanceof Double) { |
|
68 |
this.builder.add(name, (double) value); |
|
69 |
} else if (value instanceof Boolean) { |
|
70 |
this.builder.add(name, (boolean) value); |
|
71 |
} else if (value.getClass().isArray()) { |
|
72 |
this.add(name, (Object[]) value); |
|
73 |
} else if (value instanceof Class) { |
|
74 |
this.add(name, (Class) value); |
|
75 |
|
|
76 |
} else { |
|
77 |
JsonSerializer serializer = manager.getSerializer(value); |
|
78 |
if( serializer!=null ) { |
|
79 |
this.add(name, serializer.toJsonBuilder(value)); |
|
80 |
} else if (value instanceof DynObject) { |
|
81 |
this.add(name, (DynObject) value); |
|
82 |
} else if (value instanceof GetItemByKeyWithGetKeys) { |
|
83 |
this.add(name, (GetItemByKeyWithGetKeys<Object,Object>) value); |
|
84 |
} else if (value instanceof List) { |
|
85 |
this.add(name, (List<Object>) value); |
|
86 |
} else if (value instanceof Set) { |
|
87 |
this.add(name, (Set<Object>) value); |
|
88 |
} else if (value instanceof Map) { |
|
89 |
this.add(name, (Map<String,Object>) value); |
|
90 |
} else if (value instanceof Iterable) { |
|
91 |
this.add(name, (Iterable<Object>) value); |
|
92 |
} else if (value instanceof Iterator) { |
|
93 |
this.add(name, (Iterator<Object>) value); |
|
94 |
} else { |
|
95 |
throw new IllegalArgumentException("Can't serialize to Json an object of class '"+value.getClass().getName()+"'."); |
|
96 |
} |
|
97 |
} |
|
98 |
return this; |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public JsonObjectBuilder add(String name, Object[] value) { |
|
103 |
if (value == null) { |
|
104 |
this.builder.addNull(name); |
|
105 |
return this; |
|
106 |
} |
|
107 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
108 |
arrayBuilder.addAll(value); |
|
109 |
this.builder.add(name, arrayBuilder); |
|
110 |
return this; |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public JsonObjectBuilder add(String name, Iterable value) { |
|
115 |
if (value == null) { |
|
116 |
this.builder.addNull(name); |
|
117 |
return this; |
|
118 |
} |
|
119 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
120 |
if( serializer!=null ) { |
|
121 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
122 |
} else { |
|
123 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
124 |
arrayBuilder.addAll(value); |
|
125 |
this.builder.add(name, arrayBuilder); |
|
126 |
} |
|
127 |
return this; |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public JsonObjectBuilder add(String name, Map value) { |
|
132 |
if (value == null) { |
|
133 |
this.builder.addNull(name); |
|
134 |
return this; |
|
135 |
} |
|
136 |
JsonObjectBuilder objBuilder = new JsonObjectBuilderImpl(); |
|
137 |
objBuilder.addAll(value); |
|
138 |
this.builder.add(name, objBuilder); |
|
139 |
return this; |
|
140 |
} |
|
141 |
|
|
142 |
@Override |
|
143 |
public JsonObjectBuilder add(String name, GetItemByKeyWithGetKeys value) { |
|
144 |
if (value == null) { |
|
145 |
this.builder.addNull(name); |
|
146 |
return this; |
|
147 |
} |
|
148 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
149 |
if( serializer!=null ) { |
|
150 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
151 |
} else { |
|
152 |
JsonObjectBuilder objBuilder = new JsonObjectBuilderImpl(); |
|
153 |
objBuilder.addAll(value); |
|
154 |
this.builder.add(name, objBuilder); |
|
155 |
} |
|
156 |
return this; |
|
157 |
} |
|
158 |
|
|
159 |
@Override |
|
160 |
public JsonObjectBuilder add(String name, GetItemWithSize value) { |
|
161 |
if (value == null) { |
|
162 |
this.builder.addNull(name); |
|
163 |
return this; |
|
164 |
} |
|
165 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
166 |
if( serializer!=null ) { |
|
167 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
168 |
} else { |
|
169 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
170 |
arrayBuilder.addAll(value); |
|
171 |
this.builder.add(name, arrayBuilder); |
|
172 |
} |
|
173 |
return this; |
|
174 |
} |
|
175 |
|
|
176 |
@Override |
|
177 |
public JsonObjectBuilder add(String name, List value) { |
|
178 |
if (value == null) { |
|
179 |
this.builder.addNull(name); |
|
180 |
return this; |
|
181 |
} |
|
182 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
183 |
if( serializer!=null ) { |
|
184 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
185 |
} else { |
|
186 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
187 |
arrayBuilder.addAll(value); |
|
188 |
this.builder.add(name, arrayBuilder); |
|
189 |
} |
|
190 |
return this; |
|
191 |
} |
|
192 |
|
|
193 |
@Override |
|
194 |
public JsonObjectBuilder add(String name, Iterator value) { |
|
195 |
if (value == null) { |
|
196 |
this.builder.addNull(name); |
|
197 |
return this; |
|
198 |
} |
|
199 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
200 |
if( serializer!=null ) { |
|
201 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
202 |
} else { |
|
203 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
204 |
arrayBuilder.addAll(value); |
|
205 |
this.builder.add(name, arrayBuilder); |
|
206 |
} |
|
207 |
return this; |
|
208 |
} |
|
209 |
|
|
210 |
@Override |
|
211 |
public JsonObjectBuilder add(String name, Set value) { |
|
212 |
if (value == null) { |
|
213 |
this.builder.addNull(name); |
|
214 |
return this; |
|
215 |
} |
|
216 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
217 |
if( serializer!=null ) { |
|
218 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
219 |
} else { |
|
220 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
221 |
arrayBuilder.addAll(value); |
|
222 |
this.builder.add(name, arrayBuilder); |
|
223 |
} |
|
224 |
return this; |
|
225 |
} |
|
226 |
|
|
227 |
@Override |
|
228 |
public JsonObjectBuilder add(String name, javax.json.JsonValue value) { |
|
229 |
if (value == null) { |
|
230 |
this.builder.addNull(name); |
|
231 |
return this; |
|
232 |
} |
|
233 |
this.builder.add(name, value); |
|
234 |
return this; |
|
235 |
} |
|
236 |
|
|
237 |
@Override |
|
238 |
public JsonObjectBuilder add(String name, String value) { |
|
239 |
if (value == null) { |
|
240 |
this.builder.addNull(name); |
|
241 |
return this; |
|
242 |
} |
|
243 |
this.builder.add(name, value); |
|
244 |
return this; |
|
245 |
} |
|
246 |
|
|
247 |
@Override |
|
248 |
public JsonObjectBuilder add(String name, BigInteger value) { |
|
249 |
if (value == null) { |
|
250 |
this.builder.addNull(name); |
|
251 |
return this; |
|
252 |
} |
|
253 |
this.builder.add(name, value); |
|
254 |
return this; |
|
255 |
} |
|
256 |
|
|
257 |
@Override |
|
258 |
public JsonObjectBuilder add(String name, BigDecimal value) { |
|
259 |
if (value == null) { |
|
260 |
this.builder.addNull(name); |
|
261 |
return this; |
|
262 |
} |
|
263 |
this.builder.add(name, value); |
|
264 |
return this; |
|
265 |
} |
|
266 |
|
|
267 |
@Override |
|
268 |
public JsonObjectBuilder add(String name, int value) { |
|
269 |
this.builder.add(name, value); |
|
270 |
return this; |
|
271 |
} |
|
272 |
|
|
273 |
@Override |
|
274 |
public JsonObjectBuilder add(String name, long value) { |
|
275 |
this.builder.add(name, value); |
|
276 |
return this; |
|
277 |
} |
|
278 |
|
|
279 |
@Override |
|
280 |
public JsonObjectBuilder add(String name, double value) { |
|
281 |
this.builder.add(name, value); |
|
282 |
return this; |
|
283 |
} |
|
284 |
|
|
285 |
@Override |
|
286 |
public JsonObjectBuilder add(String name, boolean value) { |
|
287 |
this.builder.add(name, value); |
|
288 |
return this; |
|
289 |
} |
|
290 |
|
|
291 |
@Override |
|
292 |
public JsonObjectBuilder add(String name, javax.json.JsonObjectBuilder value) { |
|
293 |
if (value == null) { |
|
294 |
this.builder.addNull(name); |
|
295 |
return this; |
|
296 |
} |
|
297 |
this.builder.add(name, value); |
|
298 |
return this; |
|
299 |
} |
|
300 |
|
|
301 |
@Override |
|
302 |
public JsonObjectBuilder add(String name, Class value) { |
|
303 |
if (value == null) { |
|
304 |
this.builder.addNull(name); |
|
305 |
return this; |
|
306 |
} |
|
307 |
this.builder.add(name, value.getName()); |
|
308 |
return this; |
|
309 |
} |
|
310 |
|
|
311 |
@Override |
|
312 |
public javax.json.JsonObjectBuilder add(String name, javax.json.JsonArrayBuilder value) { |
|
313 |
if (value == null) { |
|
314 |
this.builder.addNull(name); |
|
315 |
return this; |
|
316 |
} |
|
317 |
this.builder.add(name, value); |
|
318 |
return this; |
|
319 |
} |
|
320 |
|
|
321 |
@Override |
|
322 |
public JsonObjectBuilder add_class(Class value) { |
|
323 |
if (value == null) { |
|
324 |
this.builder.addNull(ATTRIBUTE_NAME_CLASS); |
|
325 |
return this; |
|
326 |
} |
|
327 |
this.builder.add(ATTRIBUTE_NAME_CLASS, value.getName()); |
|
328 |
return this; |
|
329 |
} |
|
330 |
|
|
331 |
@Override |
|
332 |
public JsonObjectBuilder add_class(Object value) { |
|
333 |
if( value == null ) { |
|
334 |
this.add_class((Class)null); |
|
335 |
return this; |
|
336 |
} |
|
337 |
this.add_class(value.getClass()); |
|
338 |
return this; |
|
339 |
} |
|
340 |
|
|
341 |
@Override |
|
342 |
public JsonObjectBuilder addAll(GetItemByKeyWithGetKeys values) { |
|
343 |
if (values == null) { |
|
344 |
return this; |
|
345 |
} |
|
346 |
for (Object key : values.getKeys()) { |
|
347 |
if( key == null ) { |
|
348 |
continue; |
|
349 |
} |
|
350 |
this.add((String) key, values.get(key)); |
|
351 |
} |
|
352 |
return this; |
|
353 |
} |
|
354 |
|
|
355 |
@Override |
|
356 |
public JsonObjectBuilder addAll(Map values) { |
|
357 |
if (values == null) { |
|
358 |
return this; |
|
359 |
} |
|
360 |
Set<Map.Entry> entries = values.entrySet(); |
|
361 |
for (Map.Entry entry : entries) { |
|
362 |
if( entry.getKey()==null ) { |
|
363 |
continue; |
|
364 |
} |
|
365 |
this.add((String) entry.getKey(), entry.getValue()); |
|
366 |
} |
|
367 |
return this; |
|
368 |
} |
|
369 |
|
|
370 |
@Override |
|
371 |
public JsonObjectBuilder addAll(JsonObjectBuilder values) { |
|
372 |
return this.addAll(values.build()); |
|
373 |
} |
|
374 |
|
|
375 |
@Override |
|
376 |
public JsonObjectBuilder addAll(JsonObject values) { |
|
377 |
if (values == null) { |
|
378 |
return this; |
|
379 |
} |
|
380 |
for (String name : values.keySet()) { |
|
381 |
if( name==null || name.equals(ATTRIBUTE_NAME_CLASS)) { |
|
382 |
continue; |
|
383 |
} |
|
384 |
this.add(name, values.get(name)); |
|
385 |
} |
|
386 |
return this; |
|
387 |
} |
|
388 |
|
|
389 |
@Override |
|
390 |
public JsonObjectBuilder addAll(DynObject value) { |
|
391 |
DynClass dynClass = value.getDynClass(); |
|
392 |
for (DynField dynField : dynClass.getDynFields()) { |
|
393 |
String name = dynField.getName(); |
|
394 |
this.add(name,value.getDynValue(name)); |
|
395 |
} |
|
396 |
return this; |
|
397 |
} |
|
398 |
|
|
399 |
@Override |
|
400 |
public JsonObjectBuilder addNull(String name) { |
|
401 |
this.builder.addNull(name); |
|
402 |
return this; |
|
403 |
} |
|
404 |
|
|
405 |
@Override |
|
406 |
public javax.json.JsonObject build() { |
|
407 |
return new JsonObjectImpl(this.builder.build()); |
|
408 |
} |
|
409 |
|
|
410 |
@Override |
|
411 |
public String toString() { |
|
412 |
StringWriter sw = new StringWriter(); |
|
413 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
|
414 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
|
415 |
); |
|
416 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { |
|
417 |
jsonWriter.writeObject(this.builder.build()); |
|
418 |
} |
|
419 |
String s = sw.toString(); |
|
420 |
return s; |
|
421 |
} |
|
422 |
|
|
423 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.253/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonArrayImpl.java | ||
---|---|---|
1 |
/* |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2020 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
18 |
* |
|
19 |
* For any additional information, do not hesitate to contact us |
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
21 |
*/ |
|
22 |
|
|
23 |
package org.gvsig.json; |
|
24 |
|
|
25 |
import java.io.StringWriter; |
|
26 |
import java.util.Collection; |
|
27 |
import java.util.Collections; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.List; |
|
30 |
import java.util.ListIterator; |
|
31 |
import javax.json.JsonArray; |
|
32 |
import javax.json.JsonNumber; |
|
33 |
import javax.json.JsonObject; |
|
34 |
import javax.json.JsonString; |
|
35 |
import javax.json.JsonValue; |
|
36 |
import javax.json.JsonWriter; |
|
37 |
import javax.json.JsonWriterFactory; |
|
38 |
import javax.json.stream.JsonGenerator; |
|
39 |
|
|
40 |
/** |
|
41 |
* |
|
42 |
* @author gvSIG Team |
|
43 |
*/ |
|
44 |
public class JsonArrayImpl implements javax.json.JsonArray { |
|
45 |
|
|
46 |
private static class ListIteratorWrapper implements ListIterator<JsonValue> { |
|
47 |
|
|
48 |
private final ListIterator<JsonValue> delegated; |
|
49 |
|
|
50 |
public ListIteratorWrapper(ListIterator<JsonValue> delegated) { |
|
51 |
this.delegated = delegated; |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public boolean hasNext() { |
|
56 |
return this.delegated.hasNext(); |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public JsonValue next() { |
|
61 |
JsonValue value = this.delegated.next(); |
|
62 |
return JsonManagerImpl.wrap(value); |
Also available in: Unified diff