use of com.helger.json.IJson in project phive by phax.
the class PhiveJsonHelper method getAsValidationResultList.
/**
* Try to parse a JSON structure and convert it back to a
* {@link ValidationResultList}.
*
* @param aJson
* The JSON to be read. May be <code>null</code>.
* @param aValidationTypeResolver
* The validation type resolver to be used. May not be
* <code>null</code>.
* @return <code>null</code> in case reverse operation fails.
*/
@Nullable
public static ValidationResultList getAsValidationResultList(@Nullable final IJsonObject aJson, @Nonnull final Function<String, IValidationType> aValidationTypeResolver) {
ValueEnforcer.notNull(aValidationTypeResolver, "ValidationTypeResolver");
if (aJson == null)
return null;
final IJsonArray aResults = aJson.getAsArray(JSON_RESULTS);
if (aResults == null)
return null;
final ValidationResultList ret = new ValidationResultList();
for (final IJson aResult : aResults) {
final IJsonObject aResultObj = aResult.getAsObject();
if (aResultObj != null) {
final String sSuccess = aResultObj.getAsString(JSON_SUCCESS);
final ETriState eSuccess = getAsTriState(sSuccess);
if (eSuccess == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve TriState '" + sSuccess + "'");
continue;
}
final String sValidationType = aResultObj.getAsString(JSON_ARTIFACT_TYPE);
final IValidationType aValidationType = aValidationTypeResolver.apply(sValidationType);
if (aValidationType == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve ValidationType '" + sValidationType + "'");
continue;
}
final String sArtefactPathType = aResultObj.getAsString(JSON_ARTIFACT_PATH_TYPE);
final String sArtefactPath = aResultObj.getAsString(JSON_ARTIFACT_PATH);
final IReadableResource aRes = getAsValidationResource(sArtefactPathType, sArtefactPath);
if (aRes == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve ValidationArtefact '" + sArtefactPathType + "' with path '" + sArtefactPath + "'");
continue;
}
final ValidationArtefact aVA = new ValidationArtefact(aValidationType, aRes);
if (eSuccess.isUndefined()) {
// Ignored level
ret.add(ValidationResult.createIgnoredResult(aVA));
} else {
// We have results
final IJsonArray aItems = aResultObj.getAsArray(JSON_ITEMS);
final ErrorList aErrorList = new ErrorList();
for (final IJson aItem : aItems) {
final IJsonObject aItemObj = aItem.getAsObject();
if (aItemObj != null) {
final IError aError = getAsIError(aItemObj);
aErrorList.add(aError);
}
}
final ValidationResult aVR = new ValidationResult(aVA, aErrorList);
ret.add(aVR);
}
}
}
return ret;
}
use of com.helger.json.IJson in project peppol-commons by phax.
the class BDXR1ExtensionConverter method convert.
/**
* Convert the passed Json string to a list of SMP extensions.
*
* @param sJson
* the Json representation to be converted.
* @return <code>null</code> if the passed string is empty or cannot be
* interpreted as JSON.
*/
@Nullable
public static ICommonsList<ExtensionType> convert(@Nullable final String sJson) {
if (StringHelper.hasText(sJson)) {
// Try to interpret as JSON
final IJson aJson = JsonReader.readFromString(sJson);
if (aJson == null || !aJson.isArray()) {
LOGGER.warn("Error in parsing extension JSON '" + sJson + "'");
} else {
final ICommonsList<ExtensionType> ret = new CommonsArrayList<>();
aJson.getAsArray().forEach(aChild -> {
final IJsonObject aObject = aChild.getAsObject();
final ExtensionType aExt = new ExtensionType();
aExt.setExtensionID(aObject.getAsString(JSON_ID));
aExt.setExtensionName(aObject.getAsString(JSON_NAME));
aExt.setExtensionAgencyID(aObject.getAsString(JSON_AGENCY_ID));
aExt.setExtensionAgencyName(aObject.getAsString(JSON_AGENCY_NAME));
aExt.setExtensionAgencyURI(aObject.getAsString(JSON_AGENCY_URI));
aExt.setExtensionVersionID(aObject.getAsString(JSON_VERSION_ID));
aExt.setExtensionURI(aObject.getAsString(JSON_URI));
aExt.setExtensionReasonCode(aObject.getAsString(JSON_REASON_CODE));
aExt.setExtensionReason(aObject.getAsString(JSON_REASON));
final String sAny = aObject.getAsString(JSON_ANY);
if (StringHelper.hasText(sAny)) {
final Document aDoc = DOMReader.readXMLDOM(sAny);
if (aDoc != null)
aExt.setAny(aDoc.getDocumentElement());
}
ret.add(aExt);
});
return ret;
}
}
return null;
}
use of com.helger.json.IJson in project phoss-directory by phax.
the class PDExtendedBusinessCard method of.
@Nonnull
public static PDExtendedBusinessCard of(@Nonnull final IJsonObject aJson) {
final PDBusinessCard aBC = PDBusinessCard.of(aJson.getAsObject("businesscard"));
final ICommonsList<IDocumentTypeIdentifier> aDocTypes = CommonsArrayList.createFiltered(aJson.getAsArray("doctypes"), (Predicate<IJson>) IJson::isObject, x -> new SimpleDocumentTypeIdentifier(x.getAsObject().getAsString("scheme"), x.getAsObject().getAsString("value")));
return new PDExtendedBusinessCard(aBC, aDocTypes);
}
use of com.helger.json.IJson in project phoss-smp by phax.
the class SMPBusinessCardManagerJDBC method getJsonAsBCI.
@Nonnull
public static ICommonsList<SMPBusinessCardIdentifier> getJsonAsBCI(@Nullable final String sJson) {
final ICommonsList<SMPBusinessCardIdentifier> ret = new CommonsArrayList<>();
final IJson aJson = sJson == null ? null : JsonReader.readFromString(sJson);
if (aJson != null && aJson.isArray())
for (final IJson aItem : aJson.getAsArray()) {
final IJsonObject aItemObject = aItem.getAsObject();
final SMPBusinessCardIdentifier aBCI = new SMPBusinessCardIdentifier(aItemObject.getAsString("id"), aItemObject.getAsString("scheme"), aItemObject.getAsString("value"));
ret.add(aBCI);
}
return ret;
}
use of com.helger.json.IJson in project phoss-smp by phax.
the class SMPBusinessCardManagerJDBC method getJsonAsBCC.
@Nonnull
public static ICommonsList<SMPBusinessCardContact> getJsonAsBCC(@Nullable final String sJson) {
final ICommonsList<SMPBusinessCardContact> ret = new CommonsArrayList<>();
final IJson aJson = sJson == null ? null : JsonReader.readFromString(sJson);
if (aJson != null && aJson.isArray())
for (final IJson aItem : aJson.getAsArray()) {
final IJsonObject aItemObject = aItem.getAsObject();
final SMPBusinessCardContact aBCC = new SMPBusinessCardContact(aItemObject.getAsString("id"), aItemObject.getAsString("type"), aItemObject.getAsString("name"), aItemObject.getAsString("phone"), aItemObject.getAsString("email"));
ret.add(aBCC);
}
return ret;
}
Aggregations