• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.geojson.jackson;
2 
3 import com.fasterxml.jackson.core.JsonParser;
4 import com.fasterxml.jackson.core.JsonToken;
5 import com.fasterxml.jackson.databind.DeserializationContext;
6 import com.fasterxml.jackson.databind.JsonDeserializer;
7 import org.geojson.LngLatAlt;
8 
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 public class LngLatAltDeserializer extends JsonDeserializer<LngLatAlt> {
14 
15     @Override
deserialize(JsonParser jp, DeserializationContext ctxt)16     public LngLatAlt deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
17         if (jp.isExpectedStartArrayToken()) {
18             return deserializeArray(jp, ctxt);
19         }
20         throw ctxt.mappingException(LngLatAlt.class);
21     }
22 
deserializeArray(JsonParser jp, DeserializationContext ctxt)23     protected LngLatAlt deserializeArray(JsonParser jp, DeserializationContext ctxt) throws IOException {
24         LngLatAlt node = new LngLatAlt();
25         node.setLongitude(extractDouble(jp, ctxt, false));
26         node.setLatitude(extractDouble(jp, ctxt, false));
27         node.setAltitude(extractDouble(jp, ctxt, true));
28         List<Double> additionalElementsList = new ArrayList<Double>();
29         while (jp.hasCurrentToken() && jp.getCurrentToken() != JsonToken.END_ARRAY) {
30             double element = extractDouble(jp, ctxt, true);
31             if (!Double.isNaN(element)) {
32                 additionalElementsList.add(element);
33             }
34         }
35         double[] additionalElements = new double[additionalElementsList.size()];
36         for (int i = 0; i < additionalElements.length; i++) {
37             additionalElements[i] = additionalElementsList.get(i);
38         }
39         node.setAdditionalElements(additionalElements);
40         return node;
41     }
42 
extractDouble(JsonParser jp, DeserializationContext ctxt, boolean optional)43     private double extractDouble(JsonParser jp, DeserializationContext ctxt, boolean optional) throws IOException {
44         JsonToken token = jp.nextToken();
45         if (token == null) {
46             if (optional)
47                 return Double.NaN;
48             else
49                 throw ctxt.mappingException("Unexpected end-of-input when binding data into LngLatAlt");
50         } else {
51             switch (token) {
52                 case END_ARRAY:
53                     if (optional)
54                         return Double.NaN;
55                     else
56                         throw ctxt.mappingException("Unexpected end-of-input when binding data into LngLatAlt");
57                 case VALUE_NUMBER_FLOAT:
58                     return jp.getDoubleValue();
59                 case VALUE_NUMBER_INT:
60                     return jp.getLongValue();
61                 case VALUE_STRING:
62                     return jp.getValueAsDouble();
63                 default:
64                     throw ctxt.mappingException(
65                             "Unexpected token (" + token.name() + ") when binding data into LngLatAlt");
66             }
67         }
68     }
69 }
70