LCOV - code coverage report
Current view: top level - include/as2js - json.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 2 100.0 %
Date: 2014-11-22 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #ifndef AS2JS_JSON_H
       2             : #define AS2JS_JSON_H
       3             : /* json.h -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       4             : 
       5             : /*
       6             : 
       7             : Copyright (c) 2005-2014 Made to Order Software Corp.
       8             : 
       9             : http://snapwebsites.org/project/as2js
      10             : 
      11             : Permission is hereby granted, free of charge, to any
      12             : person obtaining a copy of this software and
      13             : associated documentation files (the "Software"), to
      14             : deal in the Software without restriction, including
      15             : without limitation the rights to use, copy, modify,
      16             : merge, publish, distribute, sublicense, and/or sell
      17             : copies of the Software, and to permit persons to whom
      18             : the Software is furnished to do so, subject to the
      19             : following conditions:
      20             : 
      21             : The above copyright notice and this permission notice
      22             : shall be included in all copies or substantial
      23             : portions of the Software.
      24             : 
      25             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      26             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      27             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      28             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      29             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      30             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      31             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      32             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      33             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      34             : SOFTWARE.
      35             : 
      36             : */
      37             : 
      38             : #include    "as2js/lexer.h"
      39             : 
      40             : #include    <controlled_vars/controlled_vars_auto_enum_init.h>
      41             : 
      42             : 
      43             : namespace as2js
      44             : {
      45             : 
      46             : // A JSON object is a JavaScript object with field names and
      47             : // values organized in a tree of Node objects. Names may be
      48             : // strings or numbers. Values can be any type of literal
      49             : // including a node representing another list of objects.
      50             : //
      51             : // After reading a JSON object, the resulting tree is as
      52             : // optimized as possible. This means it is likely to just
      53             : // be "field name": "literal value". However, by default
      54             : // we authorize values to include complex unresolved
      55             : // expressions (non-static).
      56             : //
      57             : // The JSON class defined below allows you to gather data
      58             : // from the resulting object painlessly using a chain of
      59             : // names such as:
      60             : //
      61             : // "rc.path"
      62             : //
      63             : // To retrieve the path to the resource files from the
      64             : // global resource JSON data.
      65             : 
      66             : 
      67      225559 : class JSON
      68             : {
      69             : public:
      70             :     typedef std::shared_ptr<JSON>       pointer_t;
      71             : 
      72       94846 :     class JSONValue
      73             :     {
      74             :     public:
      75             :         typedef std::shared_ptr<JSONValue>              pointer_t;
      76             :         typedef std::vector<JSONValue::pointer_t>       array_t;
      77             :         typedef std::map<String, JSONValue::pointer_t>  object_t;
      78             : 
      79             :         enum class type_t
      80             :         {
      81             :             JSON_TYPE_UNKNOWN,
      82             : 
      83             :             JSON_TYPE_ARRAY,
      84             :             JSON_TYPE_FALSE,
      85             :             JSON_TYPE_FLOAT64,
      86             :             JSON_TYPE_INT64,
      87             :             JSON_TYPE_NULL,
      88             :             JSON_TYPE_OBJECT,
      89             :             JSON_TYPE_STRING,
      90             :             JSON_TYPE_TRUE
      91             :         };
      92             :         typedef controlled_vars::limited_auto_enum_init<type_t, type_t::JSON_TYPE_UNKNOWN, type_t::JSON_TYPE_TRUE, type_t::JSON_TYPE_UNKNOWN> safe_type_t;
      93             : 
      94             :                             JSONValue(Position const& position);  // null
      95             :                             JSONValue(Position const& position, Int64 integer);
      96             :                             JSONValue(Position const& position, Float64 floating_point);
      97             :                             JSONValue(Position const& position, String const& string);
      98             :                             JSONValue(Position const& position, bool boolean);
      99             :                             JSONValue(Position const& position, array_t const& array);
     100             :                             JSONValue(Position const& position, object_t const& object);
     101             : 
     102             :         type_t              get_type() const;
     103             : 
     104             :         Int64               get_int64() const;
     105             :         Float64             get_float64() const;
     106             :         String const&       get_string() const;
     107             :         array_t const&      get_array() const;
     108             :         void                set_item(size_t idx, JSONValue::pointer_t value);
     109             :         object_t const&     get_object() const;
     110             :         void                set_member(String const& name, JSONValue::pointer_t value);
     111             : 
     112             :         Position const&     get_position() const;
     113             : 
     114             :         String              to_string() const;
     115             : 
     116             :     private:
     117             :         class saving_t
     118             :         {
     119             :         public:
     120             :             saving_t(JSONValue const& value);
     121             :             ~saving_t();
     122             : 
     123             :         private:
     124             :             JSONValue&          f_value;
     125             :         };
     126             :         friend class saving_t;
     127             : 
     128             :         safe_type_t const           f_type;
     129             :         Position                    f_position;
     130             :         controlled_vars::fbool_t    f_saving;
     131             : 
     132             :         Int64                       f_integer;
     133             :         Float64                     f_float;
     134             :         String                      f_string;
     135             :         array_t                     f_array;
     136             :         object_t                    f_object;
     137             :     };
     138             : 
     139             :     JSONValue::pointer_t    load(String const& filename);
     140             :     JSONValue::pointer_t    parse(Input::pointer_t in);
     141             :     bool                    save(String const& filename, String const& header) const;
     142             :     bool                    output(Output::pointer_t out, String const& header) const;
     143             : 
     144             :                             operator bool () const { return f_value.operator bool(); }
     145             : 
     146             :     void                    set_value(JSONValue::pointer_t value);
     147             :     JSONValue::pointer_t    get_value() const;
     148             : 
     149             : private:
     150             :     JSONValue::pointer_t    read_json_value(Node::pointer_t n);
     151             : 
     152             :     Lexer::pointer_t        f_lexer;
     153             :     JSONValue::pointer_t    f_value;
     154             : };
     155             : 
     156             : 
     157             : 
     158             : }
     159             : // namespace as2js
     160             : #endif
     161             : // #ifndef AS2JS_JSON_H
     162             : 
     163             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10