as2js: /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/json.h Source File

json.h
Go to the documentation of this file.
1 #ifndef AS2JS_JSON_H
2 #define AS2JS_JSON_H
3 /* include/as2js/json.h
4 
5 Copyright (c) 2005-2019 Made to Order Software Corp. All Rights Reserved
6 
8 
9 Permission is hereby granted, free of charge, to any
10 person obtaining a copy of this software and
11 associated documentation files (the "Software"), to
12 deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify,
14 merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom
16 the Software is furnished to do so, subject to the
17 following conditions:
18 
19 The above copyright notice and this permission notice
20 shall be included in all copies or substantial
21 portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
24 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
26 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
27 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 
34 */
35 
36 #include "as2js/lexer.h"
37 
38 
39 namespace as2js
40 {
41 
42 // A JSON object is a JavaScript object with field names and
43 // values organized in a tree of Node objects. Names may be
44 // strings or numbers. Values can be any type of literal
45 // including a node representing another list of objects.
46 //
47 // After reading a JSON object, the resulting tree is as
48 // optimized as possible. This means it is likely to just
49 // be "field name": "literal value". However, by default
50 // we authorize values to include complex unresolved
51 // expressions (non-static).
52 //
53 // The JSON class defined below allows you to gather data
54 // from the resulting object painlessly using a chain of
55 // names such as:
56 //
57 // "rc.path"
58 //
59 // To retrieve the path to the resource files from the
60 // global resource JSON data.
61 
62 
63 class JSON
64 {
65 public:
66  typedef std::shared_ptr<JSON> pointer_t;
67 
68  class JSONValue
69  {
70  public:
71  typedef std::shared_ptr<JSONValue> pointer_t;
72  typedef std::vector<JSONValue::pointer_t> array_t;
73  typedef std::map<String, JSONValue::pointer_t> object_t;
74 
75  enum class type_t
76  {
78 
87  };
88 
89  JSONValue(Position const& position); // null
90  JSONValue(Position const& position, Int64 integer);
91  JSONValue(Position const& position, Float64 floating_point);
92  JSONValue(Position const& position, String const& string);
93  JSONValue(Position const& position, bool boolean);
94  JSONValue(Position const& position, array_t const& array);
95  JSONValue(Position const& position, object_t const& object);
96 
97  type_t get_type() const;
98 
99  Int64 get_int64() const;
100  Float64 get_float64() const;
101  String const& get_string() const;
102  array_t const& get_array() const;
103  void set_item(size_t idx, JSONValue::pointer_t value);
104  object_t const& get_object() const;
105  void set_member(String const& name, JSONValue::pointer_t value);
106 
107  Position const& get_position() const;
108 
109  String to_string() const;
110 
111  private:
112  class saving_t
113  {
114  public:
115  saving_t(JSONValue const& value);
116  ~saving_t();
117 
118  private:
120  };
121  friend class saving_t;
122 
123  type_t const f_type; // no need for a default since it is a const it has to be initialized in all constructors
125  bool f_saving = false;
126 
130  array_t f_array = array_t();
131  object_t f_object = object_t();
132  };
133 
134  JSONValue::pointer_t load(String const& filename);
136  bool save(String const& filename, String const& header) const;
137  bool output(Output::pointer_t out, String const& header) const;
138 
139  operator bool () const { return f_value.operator bool(); }
140 
141  void set_value(JSONValue::pointer_t value);
143 
144 private:
146 
149 };
150 
151 
152 
153 }
154 // namespace as2js
155 #endif
156 // #ifndef AS2JS_JSON_H
157 
158 // vim: ts=4 sw=4 et
void set_value(JSONValue::pointer_t value)
Set the value of this JSON object.
Definition: json.cpp:1151
String const & get_string() const
Get the string.
Definition: json.cpp:460
void set_member(String const &name, JSONValue::pointer_t value)
Change the value of an object member.
Definition: json.cpp:610
JSONValue::pointer_t load(String const &filename)
Read a JSON value.
Definition: json.cpp:765
std::shared_ptr< Lexer > pointer_t
Definition: lexer.h:48
std::map< String, JSONValue::pointer_t > object_t
Definition: json.h:73
void set_item(size_t idx, JSONValue::pointer_t value)
Change the value of an array item.
Definition: json.cpp:525
String to_string() const
Get the JSONValue as a string.
Definition: json.cpp:671
std::shared_ptr< Input > pointer_t
Definition: stream.h:152
JSONValue(Position const &position)
Initialize a JSONValue object.
Definition: json.cpp:235
bool output(Output::pointer_t out, String const &header) const
Output this JSON to the specified output.
Definition: json.cpp:1113
std::shared_ptr< JSON > pointer_t
Definition: json.h:66
std::shared_ptr< Node > pointer_t
Definition: node.h:67
Int64 get_int64() const
Get the integer.
Definition: json.cpp:415
Position f_position
Definition: json.h:124
bool save(String const &filename, String const &header) const
Save the JSON in the specified file.
Definition: json.cpp:1069
std::shared_ptr< JSONValue > pointer_t
Definition: json.h:71
JSONValue::pointer_t read_json_value(Node::pointer_t n)
Read only JSON value.
Definition: json.cpp:863
Position const & get_position() const
Get a constant reference to the JSONValue position.
Definition: json.cpp:649
array_t const & get_array() const
Get a reference to this JSONValue array.
Definition: json.cpp:486
std::vector< JSONValue::pointer_t > array_t
Definition: json.h:72
The AlexScript to JavaScript namespace.
Definition: compiler.cpp:37
object_t f_object
Definition: json.h:131
type_t get_type() const
Retrieve the type of this JSONValue object.
Definition: json.cpp:394
std::shared_ptr< Output > pointer_t
Definition: stream.h:256
object_t const & get_object() const
Get a reference to this JSONValue object.
Definition: json.cpp:568
JSONValue::pointer_t get_value() const
Retrieve the value of the JSON object.
Definition: json.cpp:1171
JSONValue::pointer_t f_value
Definition: json.h:148
friend class saving_t
Definition: json.h:121
JSONValue::pointer_t parse(Input::pointer_t in)
Parse a JSON object.
Definition: json.cpp:804
type_t const f_type
Definition: json.h:123
Lexer::pointer_t f_lexer
Definition: json.h:147
Float64 get_float64() const
Get the floating point.
Definition: json.cpp:440

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly