JSONObject.java

  1. package org.drip.json.simple;

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

  78. /**
  79.  * <i>JSONObject</i> is an Adaptation of the JSONObject Class from the RFC4627 compliant JSON Simple
  80.  * (https://code.google.com/p/json-simple/).
  81.  *
  82.  *  <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  86.  *      <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>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/json/simple">RFC4627 Compliant JSON Message Object</a></li>
  88.  *  </ul>
  89.  *
  90.  * @author Fang Yidong
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. @SuppressWarnings ("rawtypes") public class JSONObject extends java.util.HashMap implements java.util.Map, JSONAware, JSONStreamAware{
  94.    
  95.     private static final long serialVersionUID = -503443796854799292L;
  96.    
  97.    
  98.     public JSONObject() {
  99.             super();
  100.     }

  101.     /**
  102.      * Allows creation of a JSONObject from a Map. After that, both the
  103.      * generated JSONObject and the Map can be modified independently.
  104.      *
  105.      * @param map Input JSON Map
  106.      */

  107.     @SuppressWarnings ("unchecked") public JSONObject(java.util.Map map) {
  108.             super(map);
  109.     }


  110. /**
  111.  * Encode a map into JSON text and write it to out.
  112.  * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
  113.  *
  114.  * @see org.drip.json.simple.JSONValue#writeJSONString(Object, Writer)
  115.  *
  116.  * @param map Input Map
  117.  * @param out Output Writer
  118.  *
  119.  * @throws java.io.IOException Thrown if the Inputs are Invalid
  120.  */
  121.     public static void writeJSONString(java.util.Map map, java.io.Writer out) throws java.io.IOException {
  122.             if(map == null){
  123.                     out.write("null");
  124.                     return;
  125.             }
  126.            
  127.             boolean first = true;
  128.             java.util.Iterator iter=map.entrySet().iterator();
  129.            
  130.     out.write('{');
  131.             while(iter.hasNext()){
  132.         if(first)
  133.             first = false;
  134.         else
  135.             out.write(',');
  136.         java.util.Map.Entry entry=(java.util.Map.Entry)iter.next();
  137.         out.write('\"');
  138.         out.write(escape(String.valueOf(entry.getKey())));
  139.         out.write('\"');
  140.         out.write(':');
  141.                     JSONValue.writeJSONString(entry.getValue(), out);
  142.             }
  143.             out.write('}');
  144.     }

  145.     public void writeJSONString(java.io.Writer out) throws java.io.IOException{
  146.             writeJSONString(this, out);
  147.     }
  148.    
  149.     /**
  150.      * Convert a map to JSON text. The result is a JSON object.
  151.      * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
  152.      *
  153.      * @see org.drip.json.simple.JSONValue#toJSONString(Object)
  154.      *
  155.      * @param map The JSON Map
  156.      *
  157.      * @return JSON text, or "null" if map is null.
  158.      */
  159.     public static String toJSONString(java.util.Map map){
  160.             if(map == null)
  161.                     return "null";
  162.            
  163.     StringBuffer sb = new StringBuffer();
  164.     boolean first = true;
  165.     java.util.Iterator iter=map.entrySet().iterator();
  166.            
  167.     sb.append('{');
  168.             while(iter.hasNext()){
  169.         if(first)
  170.             first = false;
  171.         else
  172.             sb.append(',');
  173.        
  174.         java.util.Map.Entry entry=(java.util.Map.Entry)iter.next();
  175.                     toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
  176.             }
  177.     sb.append('}');
  178.             return sb.toString();
  179.     }
  180.    
  181.     public String toJSONString(){
  182.             return toJSONString(this);
  183.     }
  184.    
  185.     private static String toJSONString(String key,Object value, StringBuffer sb){
  186.             sb.append('\"');
  187.     if(key == null)
  188.         sb.append("null");
  189.     else
  190.         JSONValue.escape(key, sb);
  191.             sb.append('\"').append(':');
  192.            
  193.             sb.append(JSONValue.toJSONString(value));
  194.            
  195.             return sb.toString();
  196.     }
  197.    
  198.     public String toString(){
  199.             return toJSONString();
  200.     }

  201.     public static String toString(String key,Object value){
  202.     StringBuffer sb = new StringBuffer();
  203.             toJSONString(key, value, sb);
  204.     return sb.toString();
  205.     }
  206.    
  207.     /**
  208.      * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
  209.      * It's the same as JSONValue.escape() only for compatibility here.
  210.      *
  211.      * @see org.drip.json.simple.JSONValue#escape(String)
  212.      *
  213.      * @param s The Input String
  214.      * @return Escaped String
  215.      */
  216.     public static String escape(String s){
  217.             return JSONValue.escape(s);
  218.     }
  219. }