Search in sources :

Example 6 with IJson

use of com.helger.json.IJson in project phoss-smp by phax.

the class SMPBusinessCardManagerJDBC method getJsonAsString.

@Nonnull
public static ICommonsList<String> getJsonAsString(@Nullable final String sJson) {
    final ICommonsList<String> ret = new CommonsArrayList<>();
    final IJson aJson = sJson == null ? null : JsonReader.readFromString(sJson);
    if (aJson != null && aJson.isArray())
        for (final IJson aItem : aJson.getAsArray()) {
            final String sValue = aItem.getAsValue().getAsString();
            ret.add(sValue);
        }
    return ret;
}
Also used : IJson(com.helger.json.IJson) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nonnull(javax.annotation.Nonnull)

Example 7 with IJson

use of com.helger.json.IJson in project ph-web by phax.

the class ResponseHandlerJson method handleResponse.

@Nullable
public IJson handleResponse(@Nonnull final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        throw new ClientProtocolException("Response contains no content");
    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = HttpClientHelper.getCharset(aContentType, m_aFallbackCharset);
    if (m_bDebugMode) {
        // Read all in String
        final String sJson = StringHelper.trim(EntityUtils.toString(aEntity, aCharset));
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Got JSON: <" + sJson + ">");
        final IJson ret = JsonReader.readFromString(sJson);
        if (ret == null)
            throw new IllegalArgumentException("Failed to parse as JSON: " + sJson);
        return ret;
    }
    // Read via reader to avoid duplication in memory
    return JsonReader.builder().source(aEntity.getContent(), aCharset).read();
}
Also used : HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) Charset(java.nio.charset.Charset) IJson(com.helger.json.IJson) ClientProtocolException(org.apache.http.client.ClientProtocolException) Nullable(javax.annotation.Nullable)

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