Converter.java

  1. package org.drip.json.parser;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  *
  12.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  13.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  14.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  15.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  16.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  17.  *      and computational support.
  18.  *  
  19.  *      https://lakshmidrip.github.io/DROP/
  20.  *  
  21.  *  DROP is composed of three modules:
  22.  *  
  23.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  24.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  25.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  26.  *
  27.  *  DROP Product Core implements libraries for the following:
  28.  *  - Fixed Income Analytics
  29.  *  - Loan Analytics
  30.  *  - Transaction Cost Analytics
  31.  *
  32.  *  DROP Portfolio Core implements libraries for the following:
  33.  *  - Asset Allocation Analytics
  34.  *  - Asset Liability Management Analytics
  35.  *  - Capital Estimation Analytics
  36.  *  - Exposure Analytics
  37.  *  - Margin Analytics
  38.  *  - XVA Analytics
  39.  *
  40.  *  DROP Computational Core implements libraries for the following:
  41.  *  - Algorithm Support
  42.  *  - Computation Support
  43.  *  - Function Analysis
  44.  *  - Model Validation
  45.  *  - Numerical Analysis
  46.  *  - Numerical Optimizer
  47.  *  - Spline Builder
  48.  *  - Statistical Learning
  49.  *
  50.  *  Documentation for DROP is Spread Over:
  51.  *
  52.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  53.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  54.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  55.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  56.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  57.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  58.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  59.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  60.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  61.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  62.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  63.  *
  64.  *  Licensed under the Apache License, Version 2.0 (the "License");
  65.  *      you may not use this file except in compliance with the License.
  66.  *  
  67.  *  You may obtain a copy of the License at
  68.  *      http://www.apache.org/licenses/LICENSE-2.0
  69.  *  
  70.  *  Unless required by applicable law or agreed to in writing, software
  71.  *      distributed under the License is distributed on an "AS IS" BASIS,
  72.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  73.  *  
  74.  *  See the License for the specific language governing permissions and
  75.  *      limitations under the License.
  76.  */

  77. /**
  78.  * <i>TypeConverter</i> transforms the JSON Object to certain Primitive/Simple Data Type Arrays, i.e.,
  79.  * double, integer, String, or JulianDate Arrays.
  80.  *
  81.  *  <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json">RFC-4627 Compliant JSON Encoder/Decoder (Parser)</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json/parser">RFC4627 Compliant JSON Message Parser</a></li>
  87.  *  </ul>
  88.  *
  89.  * @author Lakshmi Krishnamurthy
  90.  */

  91. public class Converter {

  92.     /**
  93.      * Convert the JSON Entry to a String
  94.      *
  95.      * @param json The Object
  96.      * @param strEntryKey The Entry Key
  97.      *
  98.      * @return The String Form of the JSON Entry
  99.      */

  100.     public static final java.lang.String StringEntry (
  101.         final org.drip.json.simple.JSONObject json,
  102.         final java.lang.String strEntryKey)
  103.     {
  104.         if (null == json || !json.containsKey (strEntryKey)) return null;

  105.         java.lang.Object objEntry = json.get (strEntryKey);

  106.         return null == objEntry || !(objEntry instanceof java.lang.String) ? null : (java.lang.String)
  107.             objEntry;
  108.     }

  109.     /**
  110.      * Convert the JSON Entry to a String Array
  111.      *
  112.      * @param json The Object
  113.      * @param strEntryKey The Entry Key
  114.      *
  115.      * @return The String Array From of the JSON Entry
  116.      */

  117.     public static final java.lang.String[] StringArrayEntry (
  118.         final org.drip.json.simple.JSONObject json,
  119.         final java.lang.String strEntryKey)
  120.     {
  121.         if (null == json || !json.containsKey (strEntryKey)) return null;

  122.         java.lang.Object objEntry = json.get (strEntryKey);

  123.         if (null == objEntry || !(objEntry instanceof org.drip.json.simple.JSONArray)) return null;

  124.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objEntry;

  125.         int iNumElement = jsonArray.size();

  126.         if (0 == iNumElement) return null;

  127.         java.lang.String[] astr = new java.lang.String[iNumElement];

  128.         for (int i = 0; i < iNumElement; ++i) {
  129.             java.lang.Object objElement = jsonArray.get (i);

  130.             astr[i] = null == objElement || !(objElement instanceof java.lang.String) ? null :
  131.                 (java.lang.String) objElement;
  132.         }

  133.         return astr;
  134.     }

  135.     /**
  136.      * Convert the JSON Entry to a Date
  137.      *
  138.      * @param json The Object
  139.      * @param strEntryKey The Entry Key
  140.      *
  141.      * @return The Date Form of the JSON Entry
  142.      */

  143.     public static final org.drip.analytics.date.JulianDate DateEntry (
  144.         final org.drip.json.simple.JSONObject json,
  145.         final java.lang.String strEntryKey)
  146.     {
  147.         return org.drip.analytics.date.DateUtil.CreateFromDDMMMYYYY (StringEntry (json, strEntryKey));
  148.     }

  149.     /**
  150.      * Convert the JSON Entry to a Date Array
  151.      *
  152.      * @param json The Object
  153.      * @param strEntryKey The Entry Key
  154.      *
  155.      * @return The Date Array From of the JSON Entry
  156.      */

  157.     public static final org.drip.analytics.date.JulianDate[] DateArrayEntry (
  158.         final org.drip.json.simple.JSONObject json,
  159.         final java.lang.String strEntryKey)
  160.     {
  161.         if (null == json || !json.containsKey (strEntryKey)) return null;

  162.         java.lang.Object objEntry = json.get (strEntryKey);

  163.         if (null == objEntry || !(objEntry instanceof org.drip.json.simple.JSONArray)) return null;

  164.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objEntry;

  165.         int iNumElement = jsonArray.size();

  166.         if (0 == iNumElement) return null;

  167.         org.drip.analytics.date.JulianDate[] adt = new org.drip.analytics.date.JulianDate[iNumElement];

  168.         for (int i = 0; i < iNumElement; ++i) {
  169.             java.lang.Object objElement = jsonArray.get (i);

  170.             if (null == objElement || !(objElement instanceof java.lang.String) || null == (adt[i] =
  171.                 org.drip.analytics.date.DateUtil.CreateFromMDY ((java.lang.String) objElement, "/")))
  172.                 return null;
  173.         }

  174.         return adt;
  175.     }

  176.     /**
  177.      * Convert the JSON Entry to a Double
  178.      *
  179.      * @param json The Object
  180.      * @param strEntryKey The Entry Key
  181.      *
  182.      * @return The Double Form of the JSON Entry
  183.      *
  184.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  185.      */

  186.     public static final double DoubleEntry (
  187.         final org.drip.json.simple.JSONObject json,
  188.         final java.lang.String strEntryKey)
  189.         throws java.lang.Exception
  190.     {
  191.         if (null == json || !json.containsKey (strEntryKey))
  192.             throw new java.lang.Exception ("Converter::DoubleEntry => Invalid Inputs");

  193.         java.lang.Object objEntry = json.get (strEntryKey);

  194.         if (null == objEntry) throw new java.lang.Exception ("Converter::DoubleEntry => Invalid Inputs");

  195.         return java.lang.Double.parseDouble (objEntry.toString().trim());
  196.     }

  197.     /**
  198.      * Convert the JSON Entry to a Double Array
  199.      *
  200.      * @param json The Object
  201.      * @param strEntryKey The Entry Key
  202.      *
  203.      * @return The Double Array From of the JSON Entry
  204.      */

  205.     public static final double[] DoubleArrayEntry (
  206.         final org.drip.json.simple.JSONObject json,
  207.         final java.lang.String strEntryKey)
  208.     {
  209.         if (null == json || !json.containsKey (strEntryKey)) return null;

  210.         java.lang.Object objEntry = json.get (strEntryKey);

  211.         if (null == objEntry || !(objEntry instanceof org.drip.json.simple.JSONArray)) return null;

  212.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objEntry;

  213.         int iNumElement = jsonArray.size();

  214.         if (0 == iNumElement) return null;

  215.         double[] adbl = new double[iNumElement];

  216.         for (int i = 0; i < iNumElement; ++i) {
  217.             java.lang.Object objElement = jsonArray.get (i);

  218.             if (null == objElement) return null;

  219.             try {
  220.                 adbl[i] = java.lang.Double.parseDouble (objElement.toString().trim());
  221.             } catch (java.lang.Exception e) {
  222.                 e.printStackTrace();

  223.                 return null;
  224.             }
  225.         }

  226.         return adbl;
  227.     }

  228.     /**
  229.      * Convert the JSON Entry to a Dual Double Array
  230.      *
  231.      * @param json The Object
  232.      * @param strEntryKey The Entry Key
  233.      *
  234.      * @return The Dual Double Array From of the JSON Entry
  235.      */

  236.     public static final double[][] DualDoubleArrayEntry (
  237.         final org.drip.json.simple.JSONObject json,
  238.         final java.lang.String strEntryKey)
  239.     {
  240.         if (null == json || !json.containsKey (strEntryKey)) return null;

  241.         java.lang.Object objEntry = json.get (strEntryKey);

  242.         if (null == objEntry || !(objEntry instanceof org.drip.json.simple.JSONArray)) return null;

  243.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objEntry;

  244.         int iNumOuterElement = jsonArray.size();

  245.         if (0 == iNumOuterElement) return null;

  246.         double[][] aadbl = new double[iNumOuterElement][];

  247.         for (int i = 0; i < iNumOuterElement; ++i) {
  248.             java.lang.Object objOuterElement = jsonArray.get (i);

  249.             if (null == objOuterElement || !(objOuterElement instanceof org.drip.json.simple.JSONArray))
  250.                 return null;

  251.             org.drip.json.simple.JSONArray jsonOuterArray = (org.drip.json.simple.JSONArray) objOuterElement;

  252.             int iNumInnerElement = jsonOuterArray.size();

  253.             if (0 == iNumInnerElement) return null;

  254.             aadbl[i] = new double[iNumInnerElement];

  255.             try {
  256.                 for (int j = 0; j < iNumInnerElement; ++j) {
  257.                     java.lang.Object objInnerElement = jsonOuterArray.get (j);

  258.                     if (null == objInnerElement) return null;

  259.                     aadbl[i][j] = java.lang.Double.parseDouble (objInnerElement.toString().trim());
  260.                 }
  261.             } catch (java.lang.Exception e) {
  262.                 e.printStackTrace();

  263.                 return null;
  264.             }
  265.         }

  266.         return aadbl;
  267.     }

  268.     /**
  269.      * Convert the JSON Entry to an Integer
  270.      *
  271.      * @param json The Object
  272.      * @param strEntryKey The Entry Key
  273.      *
  274.      * @return The Integer Form of the JSON Entry
  275.      *
  276.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  277.      */

  278.     public static final int IntegerEntry (
  279.         final org.drip.json.simple.JSONObject json,
  280.         final java.lang.String strEntryKey)
  281.         throws java.lang.Exception
  282.     {
  283.         if (null == json || !json.containsKey (strEntryKey))
  284.             throw new java.lang.Exception ("Converter::IntegerEntry => Invalid Inputs");

  285.         java.lang.Object objEntry = json.get (strEntryKey);

  286.         if (null == objEntry) throw new java.lang.Exception ("Converter::IntegerEntry => Invalid Inputs");

  287.         return java.lang.Integer.parseInt (objEntry.toString().trim());
  288.     }

  289.     /**
  290.      * Convert the JSON Entry to an Integer Array
  291.      *
  292.      * @param objJSON The JSON Object
  293.      *
  294.      * @return The Integer Array From of the JSON Entry
  295.      */

  296.     public static final int[] IntegerArrayEntry (
  297.         final java.lang.Object objJSON)
  298.     {
  299.         if (null == objJSON || !(objJSON instanceof org.drip.json.simple.JSONArray)) return null;

  300.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objJSON;

  301.         int iNumElement = jsonArray.size();

  302.         if (0 == iNumElement) return null;

  303.         int[] ai = new int[iNumElement];

  304.         for (int i = 0; i < iNumElement; ++i) {
  305.             java.lang.Object json = jsonArray.get (i);

  306.             if (null == json) return null;

  307.             ai[i] = java.lang.Integer.parseInt (json.toString().trim());
  308.         }

  309.         return ai;
  310.     }

  311.     /**
  312.      * Convert the JSON Entry to an Boolean
  313.      *
  314.      * @param json The Object
  315.      * @param strEntryKey The Entry Key
  316.      *
  317.      * @return The Boolean Form of the JSON Entry
  318.      *
  319.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  320.      */

  321.     public static final boolean BooleanEntry (
  322.         final org.drip.json.simple.JSONObject json,
  323.         final java.lang.String strEntryKey)
  324.         throws java.lang.Exception
  325.     {
  326.         if (null == json || !json.containsKey (strEntryKey))
  327.             throw new java.lang.Exception ("Converter::BooleanEntry => Invalid Inputs");

  328.         java.lang.Object objEntry = json.get (strEntryKey);

  329.         if (null == objEntry) throw new java.lang.Exception ("Converter::BooleanEntry => Invalid Inputs");

  330.         return java.lang.Boolean.parseBoolean (objEntry.toString().trim());
  331.     }

  332.     /**
  333.      * Convert the JSON Entry to a Boolean Array
  334.      *
  335.      * @param objJSON The Object
  336.      *
  337.      * @return The Boolean Array From of the JSON Entry
  338.      */

  339.     public static final boolean[] BooleanArrayEntry (
  340.         final java.lang.Object objJSON)
  341.     {
  342.         if (null == objJSON || !(objJSON instanceof org.drip.json.simple.JSONArray)) return null;

  343.         org.drip.json.simple.JSONArray jsonArray = (org.drip.json.simple.JSONArray) objJSON;

  344.         int iNumElement = jsonArray.size();

  345.         if (0 == iNumElement) return null;

  346.         boolean[] ab = new boolean[iNumElement];

  347.         for (int i = 0; i < iNumElement; ++i) {
  348.             java.lang.Object json = jsonArray.get (i);

  349.             if (null == json) return null;

  350.             ab[i] = java.lang.Boolean.parseBoolean (json.toString().trim());
  351.         }

  352.         return ab;
  353.     }

  354.     /**
  355.      * Construct a JSON Array out of the String Array
  356.      *
  357.      * @param astr The String Array
  358.      *
  359.      * @return The JSON Array Instance
  360.      */

  361.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  362.         final java.lang.String[] astr)
  363.     {
  364.         if (null == astr || 0 == astr.length) return null;

  365.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  366.         for (java.lang.String str : astr)
  367.             jsonArray.add (str);

  368.         return jsonArray;
  369.     }

  370.     /**
  371.      * Construct a JSON Array out of the Integer Array
  372.      *
  373.      * @param ai The Integer Array
  374.      *
  375.      * @return The JSON Array Instance
  376.      */

  377.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  378.         final int[] ai)
  379.     {
  380.         if (null == ai || 0 == ai.length) return null;

  381.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  382.         for (int i : ai)
  383.             jsonArray.add (i);

  384.         return jsonArray;
  385.     }

  386.     /**
  387.      * Construct a JSON Array out of the Double Array
  388.      *
  389.      * @param adbl The Double Array
  390.      *
  391.      * @return The JSON Array Instance
  392.      */

  393.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  394.         final double[] adbl)
  395.     {
  396.         if (null == adbl || 0 == adbl.length) return null;

  397.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  398.         for (double dbl : adbl)
  399.             jsonArray.add (dbl);

  400.         return jsonArray;
  401.     }

  402.     /**
  403.      * Construct a JSON 2D Array out of the 2D Double Array
  404.      *
  405.      * @param aadbl The 2D Double Array
  406.      *
  407.      * @return The JSON 2D Array Instance
  408.      */

  409.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  410.         final double[][] aadbl)
  411.     {
  412.         if (null == aadbl || 0 == aadbl.length) return null;

  413.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  414.         for (double[] adbl : aadbl)
  415.             jsonArray.add (Array (adbl));

  416.         return jsonArray;
  417.     }

  418.     /**
  419.      * Construct a JSON Array out of the Boolean Array
  420.      *
  421.      * @param ab The Boolean Array
  422.      *
  423.      * @return The JSON Array Instance
  424.      */

  425.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  426.         final boolean[] ab)
  427.     {
  428.         if (null == ab || 0 == ab.length) return null;

  429.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  430.         for (boolean b : ab)
  431.             jsonArray.add (b);

  432.         return jsonArray;
  433.     }

  434.     /**
  435.      * Construct a JSON Array out of the JulianDate Array
  436.      *
  437.      * @param adt The JulianDate Array
  438.      *
  439.      * @return The JSON Array Instance
  440.      */

  441.     @SuppressWarnings ("unchecked") public static final org.drip.json.simple.JSONArray Array (
  442.         final org.drip.analytics.date.JulianDate[] adt)
  443.     {
  444.         if (null == adt || 0 == adt.length) return null;

  445.         org.drip.json.simple.JSONArray jsonArray = new org.drip.json.simple.JSONArray();

  446.         for (org.drip.analytics.date.JulianDate dt : adt)
  447.             jsonArray.add (dt);

  448.         return jsonArray;
  449.     }
  450. }