/* Converts a string containing JSON-formatted data to a Token. Copyright (c) 2012-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.lib.conversions.json; import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ptolemy.actor.lib.conversions.Converter; import ptolemy.data.ArrayToken; import ptolemy.data.BooleanToken; import ptolemy.data.DoubleToken; import ptolemy.data.IntToken; import ptolemy.data.LongToken; import ptolemy.data.ObjectToken; import ptolemy.data.RecordToken; import ptolemy.data.StringToken; import ptolemy.data.Token; import ptolemy.data.type.BaseType; import ptolemy.graph.Inequality; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.NameDuplicationException; /** An actor that converts a string containing JSON-formatted data into a Token. Depending on the top level structure found in the JSON string, it produces either a RecordToken or an ArrayToken on its output port. Nested structures in the JSON data will translate to correspondingly nested structures in the Token.
The JSONObject parser processes values as follows: Delimited values are always parsed as a String. Values that are not delimited are tested in the order noted below. The first test that succeeds determines the type.
Note that JSON allows array elements to have different types, whereas the
ArrayToken
does not. Conversion of such mixed array will result
in an ArrayToken
of which the types of all elements are cast to
the least upper bound of the entire collection.
http://www.json.org/ - a description of the JSON format.
@see TokenToJSON @author Marten Lohstroh, Contributor: Beth Latronico @version $Id: JSONToToken.java 70402 2014-10-23 00:52:20Z cxh $ @since Ptolemy II 10.0 @Pt.ProposedRating Yellow (marten) @Pt.AcceptedRating Red (chx) */ public class JSONToToken extends Converter { /** Construct a JSONToToken actor with the given container and name. * @param container The container. * @param name The name of this actor. * @exception IllegalActionException If the actor cannot be contained * by the proposed container. * @exception NameDuplicationException If the container already has an * actor with this name. */ public JSONToToken(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); input.setTypeEquals(BaseType.STRING); } /////////////////////////////////////////////////////////////////// //// public methods //// /** Read a JSON-formatted String of name/value pairs from the input * and produce a corresponding array or record on the output. * @exception IllegalActionException If the input string does not * contain properly formatted JSON. */ @Override public void fire() throws IllegalActionException { super.fire(); output.send(0, _parseJSON(((StringToken) input.get(0)).stringValue())); } /** Return false if the input port has no token, otherwise return * what the superclass returns (presumably true). * @exception IllegalActionException If there is no director. */ @Override public boolean prefire() throws IllegalActionException { if (!input.hasToken(0)) { return false; } return super.prefire(); } /////////////////////////////////////////////////////////////////// //// protected methods //// /** * Do not establish the usual default type constraints. */ @Override protected Set