xref: /aosp_15_r20/external/geojson-jackson/README.md (revision 0a3f43d92ad5792c5b4b498ed882d515197e6f75)
1*0a3f43d9SNeil FullerGeoJson POJOs for Jackson
2*0a3f43d9SNeil Fuller=========================
3*0a3f43d9SNeil Fuller
4*0a3f43d9SNeil FullerA small package of all GeoJson POJOs (Plain Old Java Objects) for serializing and
5*0a3f43d9SNeil Fullerdeserializing of objects via JSON Jackson Parser.
6*0a3f43d9SNeil Fuller
7*0a3f43d9SNeil FullerUsage
8*0a3f43d9SNeil Fuller-----
9*0a3f43d9SNeil Fuller
10*0a3f43d9SNeil FullerIf you know what kind of object you expect from a GeoJson file you can directly read it like this:
11*0a3f43d9SNeil Fuller
12*0a3f43d9SNeil Fuller
13*0a3f43d9SNeil Fuller```java
14*0a3f43d9SNeil FullerFeatureCollection featureCollection =
15*0a3f43d9SNeil Fuller	new ObjectMapper().readValue(inputStream, FeatureCollection.class);
16*0a3f43d9SNeil Fuller```
17*0a3f43d9SNeil Fuller
18*0a3f43d9SNeil FullerIf you want to read any GeoJson file read the value as GeoJsonObject and then test for the contents via instanceOf:
19*0a3f43d9SNeil Fuller
20*0a3f43d9SNeil Fuller```java
21*0a3f43d9SNeil FullerGeoJsonObject object = new ObjectMapper().readValue(inputStream, GeoJsonObject.class);
22*0a3f43d9SNeil Fullerif (object instanceof Polygon) {
23*0a3f43d9SNeil Fuller	...
24*0a3f43d9SNeil Fuller} else if (object instanceof Feature) {
25*0a3f43d9SNeil Fuller	...
26*0a3f43d9SNeil Fuller}
27*0a3f43d9SNeil Fuller```
28*0a3f43d9SNeil Fullerand so on.
29*0a3f43d9SNeil Fuller
30*0a3f43d9SNeil FullerOr you can use the GeoJsonObjectVisitor to visit the right method:
31*0a3f43d9SNeil Fuller
32*0a3f43d9SNeil Fuller```java
33*0a3f43d9SNeil FullerGeoJsonObject object = new ObjectMapper().readValue(inputStream, GeoJsonObject.class);
34*0a3f43d9SNeil Fullerobject.accept(visitor);
35*0a3f43d9SNeil Fuller```
36*0a3f43d9SNeil Fuller
37*0a3f43d9SNeil Fuller
38*0a3f43d9SNeil FullerWriting Json is even easier. You just have to create the GeoJson objects and pass them to the Jackson ObjectMapper.
39*0a3f43d9SNeil Fuller
40*0a3f43d9SNeil Fuller```java
41*0a3f43d9SNeil FullerFeatureCollection featureCollection = new FeatureCollection();
42*0a3f43d9SNeil FullerfeatureCollection.add(new Feature());
43*0a3f43d9SNeil Fuller
44*0a3f43d9SNeil FullerString json= new ObjectMapper().writeValueAsString(featureCollection);
45*0a3f43d9SNeil Fuller```
46*0a3f43d9SNeil Fuller
47*0a3f43d9SNeil FullerMaven Central
48*0a3f43d9SNeil Fuller-------------
49*0a3f43d9SNeil Fuller
50*0a3f43d9SNeil FullerYou can find the library in the Maven Central Repository.
51*0a3f43d9SNeil Fuller
52*0a3f43d9SNeil Fuller```xml
53*0a3f43d9SNeil Fuller<dependency>
54*0a3f43d9SNeil Fuller <groupId>de.grundid.opendatalab</groupId>
55*0a3f43d9SNeil Fuller <artifactId>geojson-jackson</artifactId>
56*0a3f43d9SNeil Fuller <version>1.8.1</version>
57*0a3f43d9SNeil Fuller</dependency>
58*0a3f43d9SNeil Fuller```
59*0a3f43d9SNeil Fuller
60