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

com.helger.json.mapping.JsonMapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://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.
 */
package com.helger.json.mapping;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.lang.StackTraceHelper;
import com.helger.commons.location.ILocation;
import com.helger.commons.location.SimpleLocation;
import com.helger.commons.state.ETriState;
import com.helger.commons.string.StringHelper;
import com.helger.json.IJsonObject;
import com.helger.json.JsonObject;

/**
 * Contains some JSON mappings to commonly used data types.
 *
 * @author Philip Helger
 * @since 11.0.5
 */
@Immutable
public final class JsonMapper
{
  public static final String JSON_TRISTATE_TRUE = "TRUE";
  public static final String JSON_TRISTATE_FALSE = "FALSE";
  public static final String JSON_TRISTATE_UNDEFINED = "UNDEFINED";

  public static final String JSON_CLASS = "class";
  public static final String JSON_MESSAGE = "message";
  public static final String JSON_STACK_TRACE = "stackTrace";

  public static final String JSON_RESOURCE_ID = "resource";
  public static final String JSON_LINE_NUM = "line";
  public static final String JSON_COLUMN_NUM = "col";

  private JsonMapper ()
  {}

  /**
   * Get the tristate representation of the provided value. Either
   * {@link #JSON_TRISTATE_TRUE} or {@link #JSON_TRISTATE_FALSE}.
   *
   * @param b
   *        boolean value to get converted.
   * @return A non-null JSON value string.
   * @see #getJsonTriState(ETriState)
   */
  @Nonnull
  @Nonempty
  public static String getJsonTriState (final boolean b)
  {
    return b ? JSON_TRISTATE_TRUE : JSON_TRISTATE_FALSE;
  }

  /**
   * Get the tristate representation of the provided value. Either
   * {@link #JSON_TRISTATE_TRUE}, {@link #JSON_TRISTATE_FALSE} or
   * {@link #JSON_TRISTATE_UNDEFINED}.
   *
   * @param eTriState
   *        Tristate value to get converted. May be null.
   * @return A non-null JSON value string.
   * @see #getJsonTriState(boolean)
   */
  @Nullable
  public static String getJsonTriState (@Nullable final ETriState eTriState)
  {
    if (eTriState == null)
      return null;
    if (eTriState.isUndefined ())
      return JSON_TRISTATE_UNDEFINED;
    return getJsonTriState (eTriState.isTrue ());
  }

  /**
   * Convert the provided value into a tristate value. Must be one of
   * {@link #JSON_TRISTATE_TRUE}, {@link #JSON_TRISTATE_FALSE} or
   * {@link #JSON_TRISTATE_UNDEFINED}.
   *
   * @param sTriState
   *        Source value. May be null.
   * @return null if the provided value is unknown.
   */
  @Nullable
  public static ETriState getAsTriState (@Nullable final String sTriState)
  {
    if (JSON_TRISTATE_TRUE.equals (sTriState))
      return ETriState.TRUE;
    if (JSON_TRISTATE_FALSE.equals (sTriState))
      return ETriState.FALSE;
    if (JSON_TRISTATE_UNDEFINED.equals (sTriState))
      return ETriState.UNDEFINED;
    return null;
  }

  /**
   * Get the JSON representation of a stack trace.
* *
   * {
   *   "class" : string,
   *   "message" : string?,
   *   "stackTrace" : string
   * }
   * 
* * @param t * The exception to convert to a JSON object. May be null. * @return null if the parameter is null, the JSON * object otherwise. * @see JsonUnmappedException for a representation after reading */ @Nullable public static IJsonObject getJsonException (@Nullable final Throwable t) { if (t == null) return null; if (t instanceof JsonUnmappedException) return ((JsonUnmappedException) t).getAsJson (); return new JsonObject ().add (JSON_CLASS, t.getClass ().getName ()) .addIfNotNull (JSON_MESSAGE, t.getMessage ()) .add (JSON_STACK_TRACE, StackTraceHelper.getStackAsString (t)); } @Nullable public static JsonUnmappedException getAsUnmappedException (@Nullable final IJsonObject aObj) { if (aObj == null) return null; final String sClassName = aObj.getAsString (JsonMapper.JSON_CLASS); final String sMessage = aObj.getAsString (JsonMapper.JSON_MESSAGE); final ICommonsList aStackTraceLines = StringHelper.getExploded (StackTraceHelper.DEFAULT_LINE_SEPARATOR, aObj.getAsString (JsonMapper.JSON_STACK_TRACE)); if (sClassName == null) return null; return new JsonUnmappedException (sClassName, sMessage, aStackTraceLines); } @Nullable public static IJsonObject getJsonSimpleLocation (@Nullable final ILocation aLocation) { if (aLocation == null || !aLocation.isAnyInformationPresent ()) return null; final IJsonObject ret = new JsonObject (); if (aLocation.hasResourceID ()) ret.add (JSON_RESOURCE_ID, aLocation.getResourceID ()); if (aLocation.hasLineNumber ()) ret.add (JSON_LINE_NUM, aLocation.getLineNumber ()); if (aLocation.hasColumnNumber ()) ret.add (JSON_COLUMN_NUM, aLocation.getColumnNumber ()); return ret; } @Nullable public static ILocation getAsSimpleLocation (@Nullable final IJsonObject aObj) { if (aObj == null) return null; final String sResourceID = aObj.getAsString (JSON_RESOURCE_ID); final int nLineNumber = aObj.getAsInt (JSON_LINE_NUM, -1); final int nColumnNumber = aObj.getAsInt (JSON_COLUMN_NUM, -1); if (StringHelper.hasNoText (sResourceID) && nLineNumber < 0 && nColumnNumber < 0) return null; return new SimpleLocation (sResourceID, nLineNumber, nColumnNumber); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy