1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--         Localization, Internationalization, Globalization for Ada        --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2013, Vadim Godunko <vgodunko@gmail.com>                     --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 3899 $ $Date: 2013-04-25 18:26:20 +0400 (Thu, 25 Apr 2013) $
43------------------------------------------------------------------------------
44private with Ada.Finalization;
45
46with League.Holders;
47with League.JSON.Arrays;
48with League.JSON.Objects;
49with League.Strings;
50private with Matreshka.JSON_Types;
51
52package League.JSON.Values is
53
54   pragma Preelaborate;
55
56   type JSON_Value_Kinds is
57    (Empty_Value,
58     Boolean_Value,
59     Number_Value,
60     String_Value,
61     Array_Value,
62     Object_Value,
63     Null_Value);
64
65   type JSON_Value is tagged private;
66   pragma Preelaborable_Initialization (JSON_Value);
67
68   Empty_JSON_Value : constant JSON_Value;
69
70   Null_JSON_Value : constant JSON_Value;
71
72   function To_JSON_Value (Value : Boolean) return JSON_Value;
73   function To_JSON_Value
74    (Value : League.Holders.Universal_Float) return JSON_Value;
75   function To_JSON_Value
76    (Value : League.Holders.Universal_Integer) return JSON_Value;
77   function To_JSON_Value
78    (Value : League.Strings.Universal_String) return JSON_Value;
79   function To_JSON_Value (Value : League.Holders.Holder) return JSON_Value;
80
81   function Is_Array (Self : JSON_Value'Class) return Boolean;
82   --  Returns true if the value contains an array.
83
84   function Is_Boolean (Self : JSON_Value'Class) return Boolean;
85   --  Returns true if the value contains a boolean.
86
87   function Is_Empty (Self : JSON_Value'Class) return Boolean;
88   --  Returns true if the value is empty. This can happen in certain error
89   --  cases as e.g. accessing a non existing key in a JSON_Object.
90
91   function Is_Float_Number (Self : JSON_Value'Class) return Boolean;
92   --  Returns true if the value contains a float number.
93
94   function Is_Integer_Number (Self : JSON_Value'Class) return Boolean;
95   --  Returns true if the value contains an integer number.
96
97   function Is_Null (Self : JSON_Value'Class) return Boolean;
98   --  Returns true if the value is null.
99
100   function Is_Number (Self : JSON_Value'Class) return Boolean;
101   --  Returns true if the value contains a number.
102
103   function Is_Object (Self : JSON_Value'Class) return Boolean;
104   --  Returns true if the value contains an object.
105
106   function Is_String (Self : JSON_Value'Class) return Boolean;
107   --  Returns true if the value contains a string.
108
109   function Kind (Self : JSON_Value'Class) return JSON_Value_Kinds;
110   --  Returns type of the value.
111
112   function To_Array
113    (Self    : JSON_Value'Class;
114     Default : League.JSON.Arrays.JSON_Array
115       := League.JSON.Arrays.Empty_JSON_Array)
116       return League.JSON.Arrays.JSON_Array;
117   --  Converts the value to an array and returns it.
118   --
119   --  If type of value is not Type_Array, the Default will be returned.
120
121   function To_Boolean
122    (Self : JSON_Value'Class; Default : Boolean := False) return Boolean;
123   --  Converts the value to a bool and returns it.
124   --
125   --  If type of value is not Type_Boolean, the Default will be returned.
126
127   function To_Float
128    (Self    : JSON_Value'Class;
129     Default : League.Holders.Universal_Float := 0.0)
130       return League.Holders.Universal_Float;
131   --  Converts the value to a float and returns it.
132   --
133   --  If type of value is not Type_Number, the Default will be returned.
134
135   function To_Holder (Self : JSON_Value'Class) return League.Holders.Holder;
136   --  Converts the value to a holder.
137   --
138   --  XXX: Can be implemented for 'simple' types only.
139
140   function To_Integer
141    (Self    : JSON_Value'Class;
142     Default : League.Holders.Universal_Integer := 0)
143       return League.Holders.Universal_Integer;
144   --  Converts the value to an integer and returns it.
145   --
146   --  If type of value is not Type_Number, the Default will be returned.
147
148   function To_Object
149    (Self    : JSON_Value'Class;
150     Default : League.JSON.Objects.JSON_Object
151       := League.JSON.Objects.Empty_JSON_Object)
152       return League.JSON.Objects.JSON_Object;
153   --  Converts the value to an object and returns it.
154   --
155   --  If type of value is not Type_Object, the Default will be returned.
156
157   function To_String
158    (Self    : JSON_Value'Class;
159     Default : League.Strings.Universal_String
160       := League.Strings.Empty_Universal_String)
161       return League.Strings.Universal_String;
162   --  Converts the value to a string and returns it.
163   --
164   --  If type of value is not Type_String, the Default will be returned.
165
166private
167
168   type JSON_Value is new Ada.Finalization.Controlled with record
169      Data : Matreshka.JSON_Types.Shared_JSON_Value_Access
170        := Matreshka.JSON_Types.Empty_Shared_JSON_Value'Access;
171   end record;
172
173   overriding procedure Adjust (Self : in out JSON_Value);
174
175   overriding procedure Finalize (Self : in out JSON_Value);
176
177   Empty_JSON_Value : constant JSON_Value
178     := (Ada.Finalization.Controlled with
179           Data => Matreshka.JSON_Types.Empty_Shared_JSON_Value'Access);
180
181   Null_JSON_Value  : constant JSON_Value
182     := (Ada.Finalization.Controlled with
183           Data => Matreshka.JSON_Types.Null_Shared_JSON_Value'Access);
184
185end League.JSON.Values;
186