Search in sources :

Example 21 with JsonObject

use of com.helger.json.JsonObject in project phoss-directory by phax.

the class PDBusinessEntity method getAsJson.

@Nonnull
public IJsonObject getAsJson() {
    final IJsonObject ret = new JsonObject();
    ret.addJson("name", new JsonArray().addAllMapped(m_aNames, PDName::getAsJson));
    ret.add("countrycode", m_sCountryCode);
    if (hasGeoInfo())
        ret.add("geoinfo", m_sGeoInfo);
    if (m_aIDs.isNotEmpty())
        ret.addJson("id", new JsonArray().addAllMapped(m_aIDs, PDIdentifier::getAsJson));
    if (m_aWebsiteURIs.isNotEmpty())
        ret.addJson("website", new JsonArray().addAll(m_aWebsiteURIs));
    if (m_aContacts.isNotEmpty())
        ret.addJson("contact", new JsonArray().addAllMapped(m_aContacts, PDContact::getAsJson));
    if (hasAdditionalInfo())
        ret.add("additionalinfo", m_sAdditionalInfo);
    if (hasRegistrationDate())
        ret.add("regdate", PDTWebDateHelper.getAsStringXSD(m_aRegistrationDate));
    return ret;
}
Also used : JsonArray(com.helger.json.JsonArray) IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) Nonnull(javax.annotation.Nonnull)

Example 22 with JsonObject

use of com.helger.json.JsonObject in project phoss-directory by phax.

the class PDIdentifier method getAsJson.

@Nonnull
public static IJsonObject getAsJson(@Nullable final String sScheme, @Nullable final String sValue) {
    final IJsonObject ret = new JsonObject();
    ret.add("scheme", sScheme);
    ret.add("value", sValue);
    return ret;
}
Also used : IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) Nonnull(javax.annotation.Nonnull)

Example 23 with JsonObject

use of com.helger.json.JsonObject in project phoss-directory by phax.

the class ExportAllManager method queryAllContainedParticipantsAsJSON.

@Nonnull
public static IJsonObject queryAllContainedParticipantsAsJSON(@Nonnull final EQueryMode eQueryMode) throws IOException {
    final Query aQuery = eQueryMode.getEffectiveQuery(new MatchAllDocsQuery());
    // Query all and group by participant ID
    final ICommonsSortedSet<IParticipantIdentifier> aSet = new CommonsTreeSet<>(Comparator.comparing(IParticipantIdentifier::getURIEncoded));
    PDMetaManager.getStorageMgr().searchAll(aQuery, -1, PDField.PARTICIPANT_ID::getDocValue, aSet::add);
    // XML root
    final IJsonObject aObj = new JsonObject();
    aObj.add("version", 1);
    aObj.add("creationdt", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentZonedDateTimeUTC()));
    aObj.add("count", aSet.size());
    // For all participants
    final IJsonArray aArray = new JsonArray();
    for (final IParticipantIdentifier aParticipantID : aSet) aArray.add(aParticipantID.getURIEncoded());
    aObj.addJson("participants", aArray);
    return aObj;
}
Also used : IJsonArray(com.helger.json.IJsonArray) JsonArray(com.helger.json.JsonArray) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonObject(com.helger.json.IJsonObject) IJsonArray(com.helger.json.IJsonArray) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Example 24 with JsonObject

use of com.helger.json.JsonObject in project phive by phax.

the class PhiveJsonHelper method applyGlobalError.

/**
 * Add one global error to the response. Afterwards no validation results
 * should be added. The layout of the response object is very similar to the
 * one created by
 * {@link #applyValidationResultList(IJsonObject, IValidationExecutorSet, List, Locale, long, MutableInt, MutableInt)}.
 * <br>
 *
 * <pre>
 * {
 *   "success" : boolean,
 *   "interrupted" : boolean,
 *   "mostSevereErrorLevel" : string,
 *   "results" : array {
 *     "success" : string,  // as defined by {@link #getJsonTriState(ETriState)}
 *     "artifactType" : string,
 *     "artifactPath" : string,
 *     "items" : array {
 *       error structure as in {@link #getJsonError(IError, Locale)}
 *     }
 *   },
 *   "durationMS" : number
 * }
 * </pre>
 *
 * @param aResponse
 *        The response JSON object to add to. May not be <code>null</code>.
 * @param sErrorMsg
 *        The error message to use. May not be <code>null</code>.
 * @param nDurationMilliseconds
 *        The duration of the validation in milliseconds. Must be &ge; 0.
 */
public static void applyGlobalError(@Nonnull final IJsonObject aResponse, @Nonnull final String sErrorMsg, @Nonnegative final long nDurationMilliseconds) {
    ValueEnforcer.notNull(aResponse, "Response");
    ValueEnforcer.notNull(sErrorMsg, "ErrorMsg");
    ValueEnforcer.isGE0(nDurationMilliseconds, "DurationMilliseconds");
    final IJsonArray aResultArray = new JsonArray();
    {
        aResultArray.add(new JsonObject().add(JSON_SUCCESS, getJsonTriState(false)).add(JSON_ARTIFACT_TYPE, ARTIFACT_TYPE_INPUT_PARAMETER).add(JSON_ARTIFACT_PATH, ARTIFACT_PATH_NONE).addJson(JSON_ITEMS, new JsonArray(jsonErrorBuilder().errorLevel(EErrorLevel.ERROR).errorText(sErrorMsg).build())));
    }
    aResponse.add(JSON_SUCCESS, false);
    aResponse.add(JSON_INTERRUPTED, false);
    aResponse.add(JSON_MOST_SEVERE_ERROR_LEVEL, getJsonErrorLevel(EErrorLevel.ERROR));
    aResponse.addJson(JSON_RESULTS, aResultArray);
    aResponse.add(JSON_DURATION_MS, nDurationMilliseconds);
}
Also used : JsonArray(com.helger.json.JsonArray) IJsonArray(com.helger.json.IJsonArray) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonArray(com.helger.json.IJsonArray)

Example 25 with JsonObject

use of com.helger.json.JsonObject in project phive by phax.

the class PhiveJsonHelperTest method testGlobalError.

@Test
public void testGlobalError() {
    final IJsonObject aObj = new JsonObject();
    PhiveJsonHelper.applyGlobalError(aObj, "My error", 123);
    final String sJson = aObj.getAsJsonString();
    assertEquals("{\"success\":false," + "\"interrupted\":false," + "\"mostSevereErrorLevel\":\"ERROR\"," + "\"results\":[{\"success\":\"FALSE\",\"artifactType\":\"input-parameter\",\"artifactPath\":\"none\",\"items\":[{\"errorLevel\":\"ERROR\",\"errorText\":\"My error\"}]}]," + "\"durationMS\":123}", sJson);
}
Also used : IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (com.helger.json.JsonObject)40 IJsonObject (com.helger.json.IJsonObject)38 Nonnull (javax.annotation.Nonnull)27 JsonArray (com.helger.json.JsonArray)16 IJsonArray (com.helger.json.IJsonArray)13 Map (java.util.Map)5 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)3 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)3 StopWatch (com.helger.commons.timing.StopWatch)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 ZonedDateTime (java.time.ZonedDateTime)3 Locale (java.util.Locale)3 Nonempty (com.helger.commons.annotation.Nonempty)2 CommonsTreeMap (com.helger.commons.collection.impl.CommonsTreeMap)2 IError (com.helger.commons.error.IError)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IHCNode (com.helger.html.hc.IHCNode)2 JsonWriter (com.helger.json.serialize.JsonWriter)2