All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.tractusx.ssi.lib.model.JsonLdObject Maven / Gradle / Ivy

There is a newer version: 0.0.20
Show newest version
/*
 * ******************************************************************************
 * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License, Version 2.0 which is available at
 * https://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * *******************************************************************************
 */

package org.eclipse.tractusx.ssi.lib.model;

import com.apicatalog.jsonld.loader.DocumentLoader;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil;

/** The type Json ld object. */
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public abstract class JsonLdObject extends HashMap {

  /** The constant CONTEXT. */
  public static final String CONTEXT = "@context";

  /** The {@link DocumentLoader} */
  @ToString.Exclude @Getter private DocumentLoader documentLoader;

  /**
   * Instantiates a new Json ld object.
   *
   * @param json the json
   */
  protected JsonLdObject(Map json) {
    super(json);

    try {
      // validate getters
      Objects.requireNonNull(this.getContext());

      // We need to convert context from URI to String.
      // Because we can't serilize URI.
      final List contexts = new ArrayList<>();
      for (URI o : this.getContext()) {
        contexts.add(o.toString());
      }
      this.put(CONTEXT, contexts);

    } catch (Exception e) {
      throw new IllegalArgumentException(
          String.format("Invalid JsonLdObject: %s", SerializeUtil.toJson(json)), e);
    }
  }

  /**
   * Gets context.
   *
   * @return the context
   */
  public List getContext() {
    final Object context = this.get(CONTEXT);
    if (context instanceof String || context instanceof URI) {
      return List.of(SerializeUtil.asURI(context));
    } else if (context instanceof List) {
      final List contexts = new ArrayList<>();
      for (Object o : (List) context) {
        if (o instanceof String || o instanceof URI) {
          contexts.add(SerializeUtil.asURI(o));
        } else {
          throw new IllegalArgumentException(
              String.format(
                  "Context must be of type string or URI. Context Type: %s",
                  context.getClass().getName()));
        }
      }
      return contexts;
    } else {
      throw new IllegalArgumentException(
          String.format(
              "Context must be of type string or list. Context Type: %s",
              context.getClass().getName()));
    }
  }

  /**
   * To json string.
   *
   * @return the string
   */
  public String toJson() {
    return SerializeUtil.toJson(this);
  }

  /**
   * To pretty json string.
   *
   * @return the string
   */
  public String toPrettyJson() {
    return SerializeUtil.toPrettyJson(this);
  }

  /**
   * To json object json object.
   *
   * @return the json object
   */
  public synchronized JsonObject toJsonObject() {
    return Json.createObjectBuilder(this).build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy