use of org.codehaus.jackson.JsonNode 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;
}
use of org.codehaus.jackson.JsonNode in project stanbol by apache.
the class DependencyRelationSupport method parse.
@Override
public DependencyRelation parse(ObjectNode jDependencyRelation, AnalysedText at) {
JsonNode tag = jDependencyRelation.path(RELATION_TYPE_TAG);
if (!tag.isTextual()) {
throw new IllegalStateException("Unable to parse GrammaticalRelationTag. The value of the " + "'tag' field MUST have a textual value (json: " + jDependencyRelation + ")");
}
GrammaticalRelation grammaticalRelation = GrammaticalRelation.class.getEnumConstants()[jDependencyRelation.path(RELATION_STANBOL_TYPE_TAG).asInt()];
GrammaticalRelationTag gramRelTag = new GrammaticalRelationTag(tag.getTextValue(), grammaticalRelation);
JsonNode isDependent = jDependencyRelation.path(RELATION_IS_DEPENDENT_TAG);
if (!isDependent.isBoolean()) {
throw new IllegalStateException("Field 'isDependent' must have a true/false format");
}
Span partnerSpan = null;
String typeString = jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG).getTextValue();
if (!typeString.equals(ROOT_TAG)) {
SpanTypeEnum spanType = SpanTypeEnum.valueOf(jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG).getTextValue());
int spanStart = jDependencyRelation.path(RELATION_PARTNER_START_TAG).asInt();
int spanEnd = jDependencyRelation.path(RELATION_PARTNER_END_TAG).asInt();
switch(spanType) {
case Chunk:
partnerSpan = at.addChunk(spanStart, spanEnd);
break;
// break;
case Token:
partnerSpan = at.addToken(spanStart, spanEnd);
break;
}
}
return new DependencyRelation(gramRelTag, isDependent.asBoolean(), partnerSpan);
}
use of org.codehaus.jackson.JsonNode in project stanbol by apache.
the class PhraseTagSupport method parse.
@Override
public PhraseTag parse(ObjectNode jValue, AnalysedText at) {
JsonNode tag = jValue.path("tag");
if (!tag.isTextual()) {
throw new IllegalStateException("Unable to parse PhraseTag. The value of the " + "'tag' field MUST have a textual value (json: " + jValue + ")");
}
JsonNode jCat = jValue.path("lc");
LexicalCategory lc = null;
if (jCat.isTextual()) {
try {
lc = LexicalCategory.valueOf(jCat.getTextValue());
} catch (IllegalArgumentException e) {
log.warn("Unable to parse category for PhraseTag from '" + jCat.getTextValue() + "' (will create with tag only)!", e);
}
} else if (jCat.isInt()) {
lc = LexicalCategory.values()[jCat.getIntValue()];
} else if (!jCat.isMissingNode()) {
log.warn("Unable to parse category for PhraseTag from " + jCat + "(will create with tag only)");
}
return new PhraseTag(tag.getTextValue(), lc);
}
use of org.codehaus.jackson.JsonNode in project dhis2-core by dhis2.
the class TestUtils method getCreatedUid.
public static String getCreatedUid(String responseJson) throws IOException {
org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
JsonNode node = mapper.readTree(responseJson);
return node.get("response").get("uid").asText();
}
use of org.codehaus.jackson.JsonNode in project dianping-open-sdk by dianping.
the class DemoDealAPI method requestDealIds.
/**
* 功能描述:获取所有城市的DealID
* <p>
* 前置条件:
* <p>
* 方法影响:
* <p>
* Author xiaopeng.li, Aug 12, 2013
*
* @since dianping-java-samples 1.0
* @param appKey
* @param secret
* @param dealCities
* @return
* @throws IOException
* @throws JsonProcessingException
*/
public static Map<String, List<String>> requestDealIds(String appKey, String secret, List<String> dealCities) throws JsonProcessingException, IOException {
Map<String, List<String>> dealIds = new HashMap<String, List<String>>();
String apiUrl = "http://api.dianping.com/v1/deal/get_all_id_list";
Map<String, String> paramMap = new HashMap<String, String>();
for (String city : dealCities) {
paramMap.put("city", city);
// paramMap.put("category", "美食");
String jsonResult = DemoApiTool.requestApi(apiUrl, appKey, secret, paramMap);
JsonNode tree = JSON_MAPPER.readTree(jsonResult);
JsonNode idsNode = tree.get("id_list");
List<String> ids = new ArrayList<String>();
for (Iterator<JsonNode> iterator = idsNode.getElements(); iterator.hasNext(); ) {
JsonNode node = iterator.next();
ids.add(node.getTextValue());
}
dealIds.put(city, ids);
sleepAWhile();
}
return dealIds;
}
Aggregations