Search in sources :

Example 1 with IJson

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;
}
Also used : IValidationType(com.helger.phive.api.IValidationType) ETriState(com.helger.commons.state.ETriState) ErrorList(com.helger.commons.error.list.ErrorList) IJsonObject(com.helger.json.IJsonObject) ValidationResultList(com.helger.phive.api.result.ValidationResultList) IReadableResource(com.helger.commons.io.resource.IReadableResource) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) IJson(com.helger.json.IJson) IJsonArray(com.helger.json.IJsonArray) IError(com.helger.commons.error.IError) ValidationResult(com.helger.phive.api.result.ValidationResult) Nullable(javax.annotation.Nullable)

Example 2 with IJson

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;
}
Also used : IJsonObject(com.helger.json.IJsonObject) SMPExtensionType(com.helger.xsds.bdxr.smp2.ec.SMPExtensionType) ExtensionType(com.helger.xsds.bdxr.smp1.ExtensionType) IJson(com.helger.json.IJson) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 3 with IJson

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);
}
Also used : PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) SimpleDocumentTypeIdentifier(com.helger.peppolid.simple.doctype.SimpleDocumentTypeIdentifier) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) IJson(com.helger.json.IJson) Nonnull(javax.annotation.Nonnull)

Example 4 with IJson

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;
}
Also used : IJsonObject(com.helger.json.IJsonObject) IJson(com.helger.json.IJson) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) SMPBusinessCardIdentifier(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardIdentifier) Nonnull(javax.annotation.Nonnull)

Example 5 with IJson

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;
}
Also used : SMPBusinessCardContact(com.helger.phoss.smp.domain.businesscard.SMPBusinessCardContact) IJsonObject(com.helger.json.IJsonObject) IJson(com.helger.json.IJson) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nonnull(javax.annotation.Nonnull)

Aggregations

IJson (com.helger.json.IJson)7 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 IJsonObject (com.helger.json.IJsonObject)4 Nonnull (javax.annotation.Nonnull)4 Nullable (javax.annotation.Nullable)3 IError (com.helger.commons.error.IError)1 ErrorList (com.helger.commons.error.list.ErrorList)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 ETriState (com.helger.commons.state.ETriState)1 IJsonArray (com.helger.json.IJsonArray)1 PDBusinessCard (com.helger.pd.businesscard.generic.PDBusinessCard)1 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)1 SimpleDocumentTypeIdentifier (com.helger.peppolid.simple.doctype.SimpleDocumentTypeIdentifier)1 IValidationType (com.helger.phive.api.IValidationType)1 ValidationArtefact (com.helger.phive.api.artefact.ValidationArtefact)1 ValidationResult (com.helger.phive.api.result.ValidationResult)1 ValidationResultList (com.helger.phive.api.result.ValidationResultList)1 SMPBusinessCardContact (com.helger.phoss.smp.domain.businesscard.SMPBusinessCardContact)1 SMPBusinessCardIdentifier (com.helger.phoss.smp.domain.businesscard.SMPBusinessCardIdentifier)1 ExtensionType (com.helger.xsds.bdxr.smp1.ExtensionType)1