Search in sources :

Example 46 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class MorphoFeaturesSupport method parse.

@Override
public MorphoFeatures parse(ObjectNode jMorpho, AnalysedText at) {
    JsonNode jLemma = jMorpho.path("lemma");
    if (!jLemma.isTextual()) {
        throw new IllegalStateException("Field 'lemma' MUST provide a String value (parsed JSON: " + jMorpho);
    }
    MorphoFeatures morpho = new MorphoFeatures(jLemma.asText());
    JsonNode node = jMorpho.path("case");
    if (node.isArray()) {
        ArrayNode jCases = (ArrayNode) node;
        for (int i = 0; i < jCases.size(); i++) {
            JsonNode member = jCases.get(i);
            if (member.isObject()) {
                ObjectNode jCase = (ObjectNode) member;
                JsonNode tag = jCase.path("tag");
                if (tag.isTextual()) {
                    EnumSet<Case> type = JsonUtils.parseEnum(jCase, "type", Case.class);
                    if (type.isEmpty()) {
                        morpho.addCase(new CaseTag(tag.getTextValue()));
                    } else {
                        morpho.addCase(new CaseTag(tag.getTextValue(), type.iterator().next()));
                    }
                } else {
                    log.warn("Unable to parse CaseTag becuase 'tag' value is " + "missing or is not a String (json: " + jCase.toString() + ")");
                }
            } else {
                log.warn("Unable to parse CaseTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse CaseTags (Json Array expected as value for field 'case' but was " + node);
    }
    if (jMorpho.has("definitness")) {
        for (Definitness d : JsonUtils.parseEnum(jMorpho, "definitness", Definitness.class)) {
            morpho.addDefinitness(d);
        }
    }
    node = jMorpho.path("gender");
    if (node.isArray()) {
        ArrayNode jGenders = (ArrayNode) node;
        for (int i = 0; i < jGenders.size(); i++) {
            JsonNode member = jGenders.get(i);
            if (member.isObject()) {
                ObjectNode jGender = (ObjectNode) member;
                JsonNode tag = jGender.path("tag");
                if (tag.isTextual()) {
                    EnumSet<Gender> type = JsonUtils.parseEnum(jGender, "type", Gender.class);
                    if (type.isEmpty()) {
                        morpho.addGender(new GenderTag(tag.getTextValue()));
                    } else {
                        morpho.addGender(new GenderTag(tag.getTextValue(), type.iterator().next()));
                    }
                } else {
                    log.warn("Unable to parse GenderTag becuase 'tag' value is " + "missing or is not a String (json: " + jGender.toString() + ")");
                }
            } else {
                log.warn("Unable to parse GenderTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse GenderTag (Json Array expected as value for field 'case' but was " + node);
    }
    node = jMorpho.path("number");
    if (node.isArray()) {
        ArrayNode jNumbers = (ArrayNode) node;
        for (int i = 0; i < jNumbers.size(); i++) {
            JsonNode member = jNumbers.get(i);
            if (member.isObject()) {
                ObjectNode jNumber = (ObjectNode) member;
                JsonNode tag = jNumber.path("tag");
                if (tag.isTextual()) {
                    EnumSet<NumberFeature> type = JsonUtils.parseEnum(jNumber, "type", NumberFeature.class);
                    if (type.isEmpty()) {
                        morpho.addNumber(new NumberTag(tag.getTextValue()));
                    } else {
                        morpho.addNumber(new NumberTag(tag.getTextValue(), type.iterator().next()));
                    }
                } else {
                    log.warn("Unable to parse NumberTag becuase 'tag' value is " + "missing or is not a String (json: " + jNumber.toString() + ")");
                }
            } else {
                log.warn("Unable to parse NumberTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse NumberTag (Json Array expected as value for field 'case' but was " + node);
    }
    if (jMorpho.has("person")) {
        for (Person p : JsonUtils.parseEnum(jMorpho, "person", Person.class)) {
            morpho.addPerson(p);
        }
    }
    node = jMorpho.path("pos");
    if (node.isArray()) {
        ArrayNode jPosTags = (ArrayNode) node;
        for (int i = 0; i < jPosTags.size(); i++) {
            JsonNode member = jPosTags.get(i);
            if (member.isObject()) {
                ObjectNode jPosTag = (ObjectNode) member;
                morpho.addPos(getPosTagParser().parse(jPosTag, at));
            } else {
                log.warn("Unable to parse PosTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse PosTag (Json Array expected as value for field 'case' but was " + node);
    }
    node = jMorpho.path("tense");
    if (node.isArray()) {
        ArrayNode jTenses = (ArrayNode) node;
        for (int i = 0; i < jTenses.size(); i++) {
            JsonNode member = jTenses.get(i);
            if (member.isObject()) {
                ObjectNode jTense = (ObjectNode) member;
                JsonNode tag = jTense.path("tag");
                if (tag.isTextual()) {
                    EnumSet<Tense> type = JsonUtils.parseEnum(jTense, "type", Tense.class);
                    if (type.isEmpty()) {
                        morpho.addTense(new TenseTag(tag.getTextValue()));
                    } else {
                        morpho.addTense(new TenseTag(tag.getTextValue(), type.iterator().next()));
                    }
                } else {
                    log.warn("Unable to parse TenseTag becuase 'tag' value is " + "missing or is not a String (json: " + jTense.toString() + ")");
                }
            } else {
                log.warn("Unable to parse TenseTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse TenseTag (Json Array expected as value for field 'case' but was " + node);
    }
    node = jMorpho.path("verb-mood");
    if (node.isArray()) {
        ArrayNode jVerbMoods = (ArrayNode) node;
        for (int i = 0; i < jVerbMoods.size(); i++) {
            JsonNode member = jVerbMoods.get(i);
            if (member.isObject()) {
                ObjectNode jVerbMood = (ObjectNode) member;
                JsonNode tag = jVerbMood.path("tag");
                if (tag.isTextual()) {
                    EnumSet<VerbMood> type = JsonUtils.parseEnum(jVerbMood, "type", VerbMood.class);
                    if (type.isEmpty()) {
                        morpho.addVerbForm(new VerbMoodTag(tag.getTextValue()));
                    } else {
                        morpho.addVerbForm(new VerbMoodTag(tag.getTextValue(), type.iterator().next()));
                    }
                } else {
                    log.warn("Unable to parse VerbMoodTag becuase 'tag' value is " + "missing or is not a String (json: " + jVerbMood.toString() + ")");
                }
            } else {
                log.warn("Unable to parse VerbMoodTag from " + member.toString());
            }
        }
    } else if (!node.isMissingNode()) {
        log.warn("Unable to parse VerbMoodTag (Json Array expected as value for field 'case' but was " + node);
    }
    return morpho;
}
Also used : Tense(org.apache.stanbol.enhancer.nlp.morpho.Tense) CaseTag(org.apache.stanbol.enhancer.nlp.morpho.CaseTag) ObjectNode(org.codehaus.jackson.node.ObjectNode) Definitness(org.apache.stanbol.enhancer.nlp.morpho.Definitness) JsonNode(org.codehaus.jackson.JsonNode) VerbMoodTag(org.apache.stanbol.enhancer.nlp.morpho.VerbMoodTag) Gender(org.apache.stanbol.enhancer.nlp.morpho.Gender) Case(org.apache.stanbol.enhancer.nlp.morpho.Case) NumberFeature(org.apache.stanbol.enhancer.nlp.morpho.NumberFeature) NumberTag(org.apache.stanbol.enhancer.nlp.morpho.NumberTag) VerbMood(org.apache.stanbol.enhancer.nlp.morpho.VerbMood) ArrayNode(org.codehaus.jackson.node.ArrayNode) TenseTag(org.apache.stanbol.enhancer.nlp.morpho.TenseTag) MorphoFeatures(org.apache.stanbol.enhancer.nlp.morpho.MorphoFeatures) Person(org.apache.stanbol.enhancer.nlp.morpho.Person) GenderTag(org.apache.stanbol.enhancer.nlp.morpho.GenderTag)

Example 47 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project YCSB by brianfrankcooper.

the class SeaweedClient method toJson.

protected static String toJson(Map<String, ByteIterator> values) throws IOException {
    ObjectNode node = MAPPER.createObjectNode();
    Map<String, String> stringMap = StringByteIterator.getStringMap(values);
    for (Map.Entry<String, String> pair : stringMap.entrySet()) {
        node.put(pair.getKey(), pair.getValue());
    }
    JsonFactory jsonFactory = new JsonFactory();
    Writer writer = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
    MAPPER.writeTree(jsonGenerator, node);
    return writer.toString();
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) StringWriter(java.io.StringWriter) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) HashMap(java.util.HashMap) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 48 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project exhibitor by soabase.

the class ClusterResource method getStatus.

@Path("state")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getStatus() throws Exception {
    ObjectNode mainNode = JsonNodeFactory.instance.objectNode();
    ObjectNode switchesNode = JsonNodeFactory.instance.objectNode();
    for (ControlPanelTypes type : ControlPanelTypes.values()) {
        switchesNode.put(UIResource.fixName(type), context.getExhibitor().getControlPanelValues().isSet(type));
    }
    mainNode.put("switches", switchesNode);
    MonitorRunningInstance monitorRunningInstance = context.getExhibitor().getMonitorRunningInstance();
    InstanceStateTypes state = monitorRunningInstance.getCurrentInstanceState();
    mainNode.put("state", state.getCode());
    mainNode.put("description", state.getDescription());
    mainNode.put("isLeader", monitorRunningInstance.isCurrentlyLeader());
    return JsonUtil.writeValueAsString(mainNode);
}
Also used : ControlPanelTypes(com.netflix.exhibitor.core.controlpanel.ControlPanelTypes) MonitorRunningInstance(com.netflix.exhibitor.core.state.MonitorRunningInstance) ObjectNode(org.codehaus.jackson.node.ObjectNode) InstanceStateTypes(com.netflix.exhibitor.core.state.InstanceStateTypes) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 49 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project exhibitor by soabase.

the class ExplorerResource method getNodeData.

@GET
@Path("node-data")
@Produces("application/json")
public String getNodeData(@QueryParam("key") String key) throws Exception {
    ObjectNode node = JsonNodeFactory.instance.objectNode();
    try {
        Stat stat = context.getExhibitor().getLocalConnection().checkExists().forPath(key);
        byte[] bytes = context.getExhibitor().getLocalConnection().getData().storingStatIn(stat).forPath(key);
        if (bytes != null) {
            node.put("bytes", bytesToString(bytes));
            node.put("str", new String(bytes, "UTF-8"));
        } else {
            node.put("bytes", "");
            node.put("str", "");
        }
        node.put("stat", reflectToString(stat));
    } catch (KeeperException.NoNodeException dummy) {
        node.put("bytes", "");
        node.put("str", "");
        node.put("stat", "* not found * ");
    } catch (Throwable e) {
        node.put("bytes", "");
        node.put("str", "Exception");
        node.put("stat", e.getMessage());
    }
    return node.toString();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ObjectNode(org.codehaus.jackson.node.ObjectNode) KeeperException(org.apache.zookeeper.KeeperException)

Example 50 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project exhibitor by soabase.

the class UIResource method getBackupConfig.

@Path("backup-config")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getBackupConfig() throws Exception {
    ArrayNode node = JsonNodeFactory.instance.arrayNode();
    if (context.getExhibitor().getBackupManager().isActive()) {
        EncodedConfigParser parser = context.getExhibitor().getBackupManager().getBackupConfigParser();
        List<BackupConfigSpec> configs = context.getExhibitor().getBackupManager().getConfigSpecs();
        for (BackupConfigSpec c : configs) {
            ObjectNode n = JsonNodeFactory.instance.objectNode();
            String value = parser.getValue(c.getKey());
            n.put("key", c.getKey());
            n.put("name", c.getDisplayName());
            n.put("help", c.getHelpText());
            n.put("value", (value != null) ? value : "");
            n.put("type", c.getType().name().toLowerCase().substring(0, 1));
            node.add(n);
        }
    }
    return JsonUtil.writeValueAsString(node);
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) EncodedConfigParser(com.netflix.exhibitor.core.config.EncodedConfigParser) BackupConfigSpec(com.netflix.exhibitor.core.backup.BackupConfigSpec) ArrayNode(org.codehaus.jackson.node.ArrayNode) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ObjectNode (org.codehaus.jackson.node.ObjectNode)97 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)29 ArrayNode (org.codehaus.jackson.node.ArrayNode)29 JsonNode (org.codehaus.jackson.JsonNode)22 GET (javax.ws.rs.GET)21 Path (javax.ws.rs.Path)18 Test (org.junit.Test)16 Produces (javax.ws.rs.Produces)12 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)5 JsonFactory (org.codehaus.jackson.JsonFactory)5 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 Span (org.apache.stanbol.enhancer.nlp.model.Span)4 DatasetImpl (org.eol.globi.service.DatasetImpl)4 Date (java.util.Date)3 TaskDriver (org.apache.helix.task.TaskDriver)3 WorkflowConfig (org.apache.helix.task.WorkflowConfig)3