]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/internal/JsonUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / internal / JsonUtils.java
1 package org.simantics.charts.internal;
2
3 import java.io.IOException;
4 import java.util.Optional;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.RequestProcessor;
8 import org.simantics.db.common.request.UniqueRead;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.layer0.variable.Variables;
12
13 import com.fasterxml.jackson.core.JsonFactory;
14 import com.fasterxml.jackson.core.JsonParser;
15 import com.fasterxml.jackson.core.JsonToken;
16
17 /**
18  * @author Tuukka Lehtonen
19  *
20  */
21 public class JsonUtils {
22
23     public static Optional<Variable> tryParseJsonPropertyVariable(RequestProcessor processor, String json) throws DatabaseException {
24         return processor.syncRequest(new UniqueRead<Optional<Variable>>() {
25             @Override
26             public Optional<Variable> perform(ReadGraph graph) throws DatabaseException {
27                 return tryParseJsonPropertyVariable(graph, json);
28             }
29         });
30     }
31
32     public static Optional<Variable> tryParseJsonPropertyVariable(ReadGraph graph, String json) throws DatabaseException {
33         try (JsonParser jp = new JsonFactory().createParser(json)) {
34             return JsonUtils.readPossibleVariable(graph, jp);
35         } catch (IOException e) {
36             throw new DatabaseException(e);
37         }
38     }
39
40     public static Optional<Variable> readPossibleVariable(ReadGraph graph, JsonParser jp) throws IOException, DatabaseException {
41         Optional<String> uri = readPossibleVariableUri(jp);
42         return uri.isPresent() ? Optional.ofNullable(Variables.getPossibleVariable(graph, uri.get())) : Optional.empty();
43     }
44
45     public static Optional<String> readPossibleVariableUri(JsonParser jp) throws IOException {
46         // Sanity check: verify that we got "Json Object":
47         if (jp.nextToken() != JsonToken.START_OBJECT)
48             throw new IOException("Expected data to start with an Object");
49
50         String uri = null;
51         String type = null;
52         String defaultPropertyUri = null;
53
54         while (jp.nextToken() != JsonToken.END_OBJECT) {
55             String fieldName = jp.getCurrentName();
56             jp.nextToken();
57             if (fieldName.equals("uri")) {
58                 uri = jp.getValueAsString();
59             } else if (fieldName.equals("type")) {
60                 type = jp.getValueAsString();
61             } else if (fieldName.equals("defaultPropertyUri")) {
62                 defaultPropertyUri = jp.getValueAsString();
63             }
64         }
65
66         return Optional.ofNullable("Variable".equals(type) ?
67                 defaultPropertyUri != null ? defaultPropertyUri : uri
68                         : null);
69     }
70
71 }