1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--         Localization, Internationalization, Globalization for Ada        --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2013-2014, 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: 4730 $ $Date: 2014-03-18 17:18:15 +0400 (Tue, 18 Mar 2014) $
43------------------------------------------------------------------------------
44private with Ada.Finalization;
45with Ada.Streams;
46
47with League.JSON.Arrays;
48with League.JSON.Objects;
49with League.Stream_Element_Vectors;
50with League.Strings;
51private with Matreshka.JSON_Documents;
52
53package League.JSON.Documents is
54
55   pragma Preelaborate;
56
57   type JSON_Encoding is
58    (UTF8, UTF16, UTF16LE, UTF16BE, UTF32, UTF32LE, UTF32BE);
59
60   type JSON_Document is tagged private;
61
62   Empty_JSON_Document : constant JSON_Document;
63
64   function From_JSON
65    (Data : League.Stream_Element_Vectors.Stream_Element_Vector)
66       return JSON_Document;
67   function From_JSON
68    (Data : Ada.Streams.Stream_Element_Array) return JSON_Document;
69   --  Parses an encoded JSON document and creates a JSON_Document from it.
70   --  Data can be encoded using UTF-8, UTF-16 and UTF-32 encoding. Encoding
71   --  is detected automatically accroding to RFC-4627.
72
73   function From_JSON
74    (Data : League.Strings.Universal_String) return JSON_Document;
75   --  Parses an encoded JSON document and creates a JSON_Document from it.
76
77   function To_JSON
78    (Self     : JSON_Document'Class;
79     Encoding : JSON_Encoding := UTF8)
80       return League.Stream_Element_Vectors.Stream_Element_Vector;
81   --  Converts the JSON_Document to an encoded JSON document. Encoding can be
82   --  selected by Encoding parameter.
83
84   function To_JSON
85    (Self : JSON_Document'Class) return League.Strings.Universal_String;
86   --  Converts the JSON_Document to an encoded JSON document.
87
88   function Is_Array (Self : JSON_Document'Class) return Boolean;
89   --  Returns true if the document contains an array.
90
91   function Is_Empty (Self : JSON_Document'Class) return Boolean;
92   --  Returns true if the document doesn't contain any data.
93
94   function Is_Object (Self : JSON_Document'Class) return Boolean;
95   --  Returns true if the document contains an object.
96
97   procedure Set_Array
98    (Self  : in out JSON_Document'Class;
99     Value : League.JSON.Arrays.JSON_Array);
100   --  Sets array as the main object of this document.
101
102   procedure Set_Object
103    (Self  : in out JSON_Document'Class;
104     Value : League.JSON.Objects.JSON_Object);
105   --  Sets object as the main object of this document.
106
107   function To_JSON_Array
108    (Self : JSON_Document'Class) return League.JSON.Arrays.JSON_Array;
109   --  Returns the JSON_Array contained in the document.
110   --
111   --  Returns an empty array if the document contains an object.
112
113   function To_JSON_Object
114    (Self : JSON_Document'Class) return League.JSON.Objects.JSON_Object;
115   --  Returns the JSON_Object contained in the document.
116   --
117   --  Returns an empty object if the document contains an array.
118
119private
120
121   type JSON_Document is new Ada.Finalization.Controlled with record
122      Data : Matreshka.JSON_Documents.Shared_JSON_Document_Access
123        := Matreshka.JSON_Documents.Empty_Shared_JSON_Document'Access;
124   end record;
125
126   overriding procedure Adjust (Self : in out JSON_Document);
127
128   overriding procedure Finalize (Self : in out JSON_Document);
129
130   Empty_JSON_Document : constant JSON_Document
131     := (Ada.Finalization.Controlled with
132           Data => Matreshka.JSON_Documents.Empty_Shared_JSON_Document'Access);
133
134end League.JSON.Documents;
135