svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / json / GeometryJsonSerializer.java @ 46618
History | View | Annotate | Download (2.67 KB)
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.fmap.geom.jts.json; |
24 |
|
25 |
import javax.json.JsonObject; |
26 |
import org.apache.commons.lang3.StringUtils; |
27 |
import org.gvsig.fmap.geom.Geometry; |
28 |
import org.gvsig.fmap.geom.GeometryLocator; |
29 |
import org.gvsig.json.Json; |
30 |
import org.gvsig.json.JsonManager; |
31 |
import org.gvsig.json.JsonObjectBuilder; |
32 |
import org.gvsig.tools.util.IsApplicable; |
33 |
|
34 |
/**
|
35 |
*
|
36 |
* @author gvSIG Team
|
37 |
*/
|
38 |
@SuppressWarnings("UseSpecificCatch") |
39 |
public class GeometryJsonSerializer implements JsonManager.JsonSerializer, IsApplicable { |
40 |
|
41 |
@Override
|
42 |
public Class getObjectClass() { |
43 |
return Geometry.class;
|
44 |
} |
45 |
|
46 |
@Override
|
47 |
public Object toObject(JsonObject json) { |
48 |
try {
|
49 |
String wkt = json.getString("wkt"); |
50 |
Geometry geom = GeometryLocator.getGeometryManager().createFrom(wkt); |
51 |
return geom;
|
52 |
} catch (Exception ex) { |
53 |
throw new IllegalArgumentException("Can't retrieve a Geometry from JsonObject.", ex); |
54 |
} |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public JsonObjectBuilder toJsonBuilder(Object value) { |
59 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
60 |
builder.add_class(Geometry.class); |
61 |
this.addAll(builder, value);
|
62 |
return builder;
|
63 |
} |
64 |
|
65 |
private void addAll(JsonObjectBuilder target, Object source) { |
66 |
Geometry geom = (Geometry) source; |
67 |
target.add("wkt", geom.convertToWKTQuietly());
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public boolean isApplicable(Object... args) { |
72 |
if( args[0] instanceof Geometry) { |
73 |
return true; |
74 |
} |
75 |
if( !(args[0] instanceof JsonObject) ) { |
76 |
return false; |
77 |
} |
78 |
JsonObject json = (JsonObject) args[0];
|
79 |
String className = json.getString(JsonManager.ATTRIBUTE_NAME_CLASS, null); |
80 |
return StringUtils.equalsIgnoreCase(className, Geometry.class.getName());
|
81 |
} |
82 |
|
83 |
} |